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 +4 -4
- data/lib/committee/drivers.rb +5 -0
- data/lib/committee/drivers/hyper_schema.rb +4 -0
- data/lib/committee/drivers/open_api_2.rb +4 -0
- data/lib/committee/drivers/open_api_3.rb +4 -0
- data/lib/committee/request_unpacker.rb +14 -12
- data/lib/committee/schema_validator/hyper_schema.rb +1 -0
- data/lib/committee/schema_validator/open_api_3.rb +1 -0
- data/lib/committee/schema_validator/option.rb +28 -11
- data/test/drivers_test.rb +1 -0
- data/test/middleware/request_validation_open_api_3_test.rb +24 -3
- data/test/request_unpacker_test.rb +22 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4f07f61485c78ec77763069cef3c82cbb3682768d5fa8b6d87383c78be3bf29
|
4
|
+
data.tar.gz: e6620a92bec74e0624f5c43591b552b5533551d66007ca24baa032e615d96551
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82cf11b46ef546e5b47270829b9acded9d85c871b00eb914308c80044d1991fb89c8dac82ea3b0ab6cf8a92230d50ecc348c3f489d3306dce2876f727f7ff41a
|
7
|
+
data.tar.gz: d62d6a02b5885f25755fe88028fe8431111edcaa1d3dd2885dc7c1d5ed136676e89af8b5d7cc2a06c5846a058ec018f14460b1be934b65a49a9192e6c5eac8eb
|
data/lib/committee/drivers.rb
CHANGED
@@ -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
|
@@ -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
|
-
|
74
|
-
|
75
|
-
|
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
|
-
|
85
|
-
|
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
|
-
|
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
|
-
#
|
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
|
-
|
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
|
-
@
|
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
|
-
@
|
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
|
data/test/drivers_test.rb
CHANGED
@@ -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
|
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,
|
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 "/
|
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
|
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-
|
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:
|
242
|
+
version: '0'
|
243
243
|
requirements: []
|
244
244
|
rubygems_version: 3.0.1
|
245
245
|
signing_key:
|