committee 3.0.0.beta3 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4af30e34beff4126b7d69135a887fd8e984741f0700be148eaad1109b7b96511
4
- data.tar.gz: 98075cfbcf0207f6d4278eaef4d6fb9d13681648748d768a35e637c45e96be5b
3
+ metadata.gz: a4f07f61485c78ec77763069cef3c82cbb3682768d5fa8b6d87383c78be3bf29
4
+ data.tar.gz: e6620a92bec74e0624f5c43591b552b5533551d66007ca24baa032e615d96551
5
5
  SHA512:
6
- metadata.gz: 3f8c7471aaba3b7df58bb4c5e2f5b6311dfd91d2e6d8fd28848b715f4f2d59a418c275a76ecdbce34c1aa5222c1a2c18198ffcd815588f81f6d319ff09e1e1f5
7
- data.tar.gz: dbc206cf90277dad16b2611fd489889c229d27616c0eecbdcf9ed59e494c588b1d0e3eba31e98e8da44417c13d15c6566fe28d9cb2e1df11cc552a0553c1dca5
6
+ metadata.gz: 82cf11b46ef546e5b47270829b9acded9d85c871b00eb914308c80044d1991fb89c8dac82ea3b0ab6cf8a92230d50ecc348c3f489d3306dce2876f727f7ff41a
7
+ data.tar.gz: d62d6a02b5885f25755fe88028fe8431111edcaa1d3dd2885dc7c1d5ed136676e89af8b5d7cc2a06c5846a058ec018f14460b1be934b65a49a9192e6c5eac8eb
@@ -68,6 +68,11 @@ module Committee
68
68
  raise "needs implementation"
69
69
  end
70
70
 
71
+ # Use GET request body to request parameter (request body merge to parameter)
72
+ def default_allow_get_body
73
+ raise "needs implementation"
74
+ end
75
+
71
76
  # Whether parameters in a request's path will be considered and coerced
72
77
  # by default.
73
78
  def default_path_params
@@ -9,6 +9,10 @@ module Committee::Drivers
9
9
  false
10
10
  end
11
11
 
12
+ def default_allow_get_body
13
+ true
14
+ end
15
+
12
16
  # Whether parameters in a request's path will be considered and coerced by
13
17
  # default.
14
18
  def default_path_params
@@ -9,6 +9,10 @@ module Committee::Drivers
9
9
  true
10
10
  end
11
11
 
12
+ def default_allow_get_body
13
+ true
14
+ end
15
+
12
16
  # Whether parameters in a request's path will be considered and coerced by
13
17
  # default.
14
18
  def default_path_params
@@ -9,6 +9,10 @@ module Committee::Drivers
9
9
  true
10
10
  end
11
11
 
12
+ def default_allow_get_body
13
+ false
14
+ end
15
+
12
16
  # Whether parameters in a request's path will be considered and coerced by
13
17
  # default.
14
18
  def default_path_params
@@ -4,6 +4,7 @@ module Committee
4
4
  @request = request
5
5
 
6
6
  @allow_form_params = options[:allow_form_params]
7
+ @allow_get_body = options[:allow_get_body]
7
8
  @allow_query_params = options[:allow_query_params]
8
9
  @coerce_form_params = options[:coerce_form_params]
9
10
  @optimistic_json = options[:optimistic_json]
@@ -70,20 +71,21 @@ module Committee
70
71
  end
71
72
 
72
73
  def parse_json
73
- if (body = @request.body.read).length != 0
74
- @request.body.rewind
75
- hash = JSON.parse(body)
76
- # We want a hash specifically. '42', 42, and [42] will all be
77
- # decoded properly, but we can't use them here.
78
- if !hash.is_a?(Hash)
79
- raise BadRequest,
80
- "Invalid JSON input. Require object with parameters as keys."
81
- end
82
- indifferent_params(hash)
74
+ return nil if @request.request_method == "GET" && !@allow_get_body
75
+
76
+ body = @request.body.read
83
77
  # if request body is empty, we just have empty params
84
- else
85
- nil
78
+ return nil if body.length == 0
79
+
80
+ @request.body.rewind
81
+ hash = JSON.parse(body)
82
+ # We want a hash specifically. '42', 42, and [42] will all be
83
+ # decoded properly, but we can't use them here.
84
+ if !hash.is_a?(Hash)
85
+ raise BadRequest,
86
+ "Invalid JSON input. Require object with parameters as keys."
86
87
  end
88
+ indifferent_params(hash)
87
89
  end
88
90
 
89
91
  def headers
@@ -66,6 +66,7 @@ class Committee::SchemaValidator
66
66
  request.env[validator_option.params_key], request.env[validator_option.headers_key] = Committee::RequestUnpacker.new(
67
67
  request,
68
68
  allow_form_params: validator_option.allow_form_params,
69
+ allow_get_body: validator_option.allow_get_body,
69
70
  allow_query_params: validator_option.allow_query_params,
70
71
  coerce_form_params: validator_option.coerce_form_params,
71
72
  optimistic_json: validator_option.optimistic_json,
@@ -65,6 +65,7 @@ class Committee::SchemaValidator
65
65
  request.env[validator_option.params_key], request.env[validator_option.headers_key] = Committee::RequestUnpacker.new(
66
66
  request,
67
67
  allow_form_params: validator_option.allow_form_params,
68
+ allow_get_body: validator_option.allow_get_body,
68
69
  allow_query_params: validator_option.allow_query_params,
69
70
  coerce_form_params: validator_option.coerce_form_params,
70
71
  optimistic_json: validator_option.optimistic_json,
@@ -1,28 +1,45 @@
1
1
  class Committee::SchemaValidator
2
2
  class Option
3
- attr_reader :coerce_recursive, :params_key, :allow_form_params, :allow_query_params, :optimistic_json, :coerce_form_params, :headers_key, :coerce_query_params, :coerce_path_params, :check_content_type, :check_header, :coerce_date_times, :prefix, :validate_success_only
3
+ # Boolean Options
4
+ attr_reader :allow_form_params,
5
+ :allow_get_body,
6
+ :allow_query_params,
7
+ :check_content_type,
8
+ :check_header,
9
+ :coerce_date_times,
10
+ :coerce_form_params,
11
+ :coerce_path_params,
12
+ :coerce_query_params,
13
+ :coerce_recursive,
14
+ :optimistic_json,
15
+ :validate_success_only
16
+
17
+ # Non-boolean options:
18
+ attr_reader :headers_key,
19
+ :params_key,
20
+ :prefix
4
21
 
5
22
  def initialize(options, schema, schema_type)
23
+ # Non-boolean options
6
24
  @headers_key = options[:headers_key] || "committee.headers"
7
25
  @params_key = options[:params_key] || "committee.params"
8
-
9
26
  @prefix = options[:prefix]
10
27
 
11
- # response option
12
- @validate_success_only = options.fetch(:validate_success_only, schema.driver.default_validate_success_only)
13
-
14
- @coerce_recursive = options.fetch(:coerce_recursive, true)
28
+ # Boolean options and have a common value by default
15
29
  @allow_form_params = options.fetch(:allow_form_params, true)
16
30
  @allow_query_params = options.fetch(:allow_query_params, true)
31
+ @check_content_type = options.fetch(:check_content_type, true)
32
+ @check_header = options.fetch(:check_header, true)
33
+ @coerce_recursive = options.fetch(:coerce_recursive, true)
17
34
  @optimistic_json = options.fetch(:optimistic_json, false)
18
35
 
19
- @coerce_form_params = options.fetch(:coerce_form_params, schema.driver.default_coerce_form_params)
36
+ # Boolean options and have a different value by default
37
+ @allow_get_body = options.fetch(:allow_get_body, schema.driver.default_allow_get_body)
20
38
  @coerce_date_times = options.fetch(:coerce_date_times, schema.driver.default_coerce_date_times)
21
- @coerce_query_params = options.fetch(:coerce_query_params, schema.driver.default_query_params)
39
+ @coerce_form_params = options.fetch(:coerce_form_params, schema.driver.default_coerce_form_params)
22
40
  @coerce_path_params = options.fetch(:coerce_path_params, schema.driver.default_path_params)
23
-
24
- @check_content_type = options.fetch(:check_content_type, true)
25
- @check_header = options.fetch(:check_header, true)
41
+ @coerce_query_params = options.fetch(:coerce_query_params, schema.driver.default_query_params)
42
+ @validate_success_only = options.fetch(:validate_success_only, schema.driver.default_validate_success_only)
26
43
  end
27
44
  end
28
45
  end
@@ -93,6 +93,7 @@ end
93
93
 
94
94
  describe Committee::Drivers::Driver do
95
95
  DRIVER_METHODS = {
96
+ :default_allow_get_body => [],
96
97
  :default_coerce_form_params => [],
97
98
  :default_path_params => [],
98
99
  :default_query_params => [],
@@ -42,7 +42,25 @@ describe Committee::Middleware::RequestValidation do
42
42
  assert_equal 200, last_response.status
43
43
  end
44
44
 
45
- it "passes given a datetime and with coerce_date_times enabled on GET endpoint with request body(old behavior)" do
45
+ it "passes given a valid parameter on GET endpoint with request body and allow_get_body=true" do
46
+ params = { "data" => "abc" }
47
+
48
+ @app = new_rack_app(schema: open_api_3_schema, allow_get_body: true)
49
+
50
+ get "/get_endpoint_with_requered_parameter", { no_problem: true }, { input: params.to_json }
51
+ assert_equal 200, last_response.status
52
+ end
53
+
54
+ it "errors given valid parameter on GET endpoint with request body and allow_get_body=false" do
55
+ params = { "data" => "abc" }
56
+
57
+ @app = new_rack_app(schema: open_api_3_schema, allow_get_body: false)
58
+
59
+ get "/get_endpoint_with_requered_parameter", { no_problem: true }, { input: params.to_json }
60
+ assert_equal 400, last_response.status
61
+ end
62
+
63
+ it "passes given a datetime and with coerce_date_times enabled on GET endpoint with request body" do
46
64
  params = { "datetime_string" => "2016-04-01T16:00:00.000+09:00" }
47
65
 
48
66
  check_parameter = lambda { |env|
@@ -50,9 +68,12 @@ describe Committee::Middleware::RequestValidation do
50
68
  [200, {}, []]
51
69
  }
52
70
 
53
- @app = new_rack_app_with_lambda(check_parameter, schema: open_api_3_schema, coerce_date_times: true)
71
+ @app = new_rack_app_with_lambda(check_parameter,
72
+ schema: open_api_3_schema,
73
+ coerce_date_times: true,
74
+ allow_get_body: true)
54
75
 
55
- get "/get_body_test", { no_problem: true }, { input: params.to_json }
76
+ get "/string_params_coercer", { no_problem: true }, { input: params.to_json }
56
77
  assert_equal 200, last_response.status
57
78
  end
58
79
 
@@ -234,4 +234,26 @@ describe Committee::RequestUnpacker do
234
234
  _, headers = Committee::RequestUnpacker.new(request, { allow_header_params: true }).call
235
235
  assert_equal({ "FOO-BAR" => "some header value" }, headers)
236
236
  end
237
+
238
+ it "includes request body when`use_get_body` is true" do
239
+ env = {
240
+ "rack.input" => StringIO.new('{"x":1, "y":2}'),
241
+ "REQUEST_METHOD" => "GET",
242
+ "QUERY_STRING"=>"data=value&x=aaa",
243
+ }
244
+ request = Rack::Request.new(env)
245
+ params, _ = Committee::RequestUnpacker.new(request, { allow_query_params: true, allow_get_body: true }).call
246
+ assert_equal({ 'data' => 'value', 'x' => 1, 'y' => 2 }, params)
247
+ end
248
+
249
+ it "doesn't include request body when `use_get_body` is false" do
250
+ env = {
251
+ "rack.input" => StringIO.new('{"x":1, "y":2}'),
252
+ "REQUEST_METHOD" => "GET",
253
+ "QUERY_STRING"=>"data=value&x=aaa",
254
+ }
255
+ request = Rack::Request.new(env)
256
+ params, _ = Committee::RequestUnpacker.new(request, { allow_query_params: true, use_get_body: false }).call
257
+ assert_equal({ 'data' => 'value', 'x' => 'aaa' }, params)
258
+ end
237
259
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: committee
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.beta3
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-01-25 00:00:00.000000000 Z
12
+ date: 2019-01-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json_schema
@@ -237,9 +237,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
237
237
  version: 2.3.0
238
238
  required_rubygems_version: !ruby/object:Gem::Requirement
239
239
  requirements:
240
- - - ">"
240
+ - - ">="
241
241
  - !ruby/object:Gem::Version
242
- version: 1.3.1
242
+ version: '0'
243
243
  requirements: []
244
244
  rubygems_version: 3.0.1
245
245
  signing_key: