committee 4.99.1 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/committee/drivers/hyper_schema/schema.rb +2 -2
  3. data/lib/committee/drivers/open_api_2/driver.rb +1 -1
  4. data/lib/committee/drivers/open_api_2/parameter_schema_builder.rb +1 -1
  5. data/lib/committee/drivers/open_api_2/schema.rb +2 -2
  6. data/lib/committee/drivers/open_api_3/schema.rb +2 -2
  7. data/lib/committee/drivers/schema.rb +1 -1
  8. data/lib/committee/drivers.rb +21 -9
  9. data/lib/committee/middleware/base.rb +6 -5
  10. data/lib/committee/middleware/request_validation.rb +1 -8
  11. data/lib/committee/middleware/response_validation.rb +13 -14
  12. data/lib/committee/request_unpacker.rb +1 -1
  13. data/lib/committee/schema_validator/hyper_schema/response_validator.rb +8 -2
  14. data/lib/committee/schema_validator/open_api_3/operation_wrapper.rb +36 -33
  15. data/lib/committee/schema_validator/open_api_3/request_validator.rb +11 -2
  16. data/lib/committee/schema_validator/open_api_3.rb +23 -18
  17. data/lib/committee/schema_validator/option.rb +4 -21
  18. data/lib/committee/schema_validator.rb +1 -1
  19. data/lib/committee/test/methods.rb +3 -8
  20. data/lib/committee/version.rb +5 -0
  21. data/lib/committee.rb +9 -2
  22. data/test/committee_test.rb +28 -2
  23. data/test/drivers/open_api_3/driver_test.rb +1 -1
  24. data/test/drivers_test.rb +21 -8
  25. data/test/middleware/base_test.rb +6 -13
  26. data/test/middleware/request_validation_open_api_3_test.rb +38 -42
  27. data/test/middleware/request_validation_test.rb +1 -28
  28. data/test/middleware/response_validation_open_api_3_test.rb +46 -3
  29. data/test/middleware/response_validation_test.rb +6 -25
  30. data/test/schema_validator/hyper_schema/response_validator_test.rb +10 -0
  31. data/test/schema_validator/hyper_schema/router_test.rb +2 -2
  32. data/test/schema_validator/hyper_schema/string_params_coercer_test.rb +1 -1
  33. data/test/schema_validator/open_api_3/operation_wrapper_test.rb +56 -12
  34. data/test/schema_validator/open_api_3/request_validator_test.rb +27 -0
  35. data/test/schema_validator/open_api_3/response_validator_test.rb +15 -1
  36. data/test/test/methods_new_version_test.rb +1 -1
  37. data/test/test/methods_test.rb +11 -31
  38. data/test/test_helper.rb +14 -4
  39. metadata +5 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d369a9be7988a151dbd3b7d9f0065cc821ffcdc8e2543b95355b22fb1f93f6b8
4
- data.tar.gz: 356fa03d1f3405544d7251302f012f48c0587c31c09b60f2509bdf7776e0c95c
3
+ metadata.gz: 284c8c1198255435e959dcee2b92cb25c3d80dd5fe6d4622b8077a69525712ed
4
+ data.tar.gz: b306ec0ad5e628d9cc7b6382781c4b710c99bedb127a7000edadf9d0e0b8fa84
5
5
  SHA512:
6
- metadata.gz: 6c821b9f35b1eb20ebea532e8cbc901295596136ccdd680c34ee6eed48da8858c2a22a754a52812cc66de43598f90515ec8642753cb4d3e46390dfd3f840b6a4
7
- data.tar.gz: 6b720c3e1e2c25e7f8682ba4b8db3879d552a943ec7739d6686baa62b5c4985f609643ed43db67b135025d76e7bde7ff767629ae57b5da27b82107dc6a325629
6
+ metadata.gz: 687d9a47d2a17786313bde939339d464a8d775795458b09ad7d7784bfa337f848a9ffabe240e9f39173c4466589c026f64a8eede768e7abfff4b01b2672339f5
7
+ data.tar.gz: 4ee7781341de9ebafae28d01d98c0f72d54067c425f3ee10b221c4eb88da85e79b36853bbda4818b20348177f8d975103cc5be694ff7e8252a6634a856853b7c
@@ -12,8 +12,8 @@ module Committee
12
12
 
13
13
  attr_reader :validator_option
14
14
 
15
- def build_router(options, is_request)
16
- @validator_option = Committee::SchemaValidator::Option.new(options, self, :hyper_schema, is_request)
15
+ def build_router(options)
16
+ @validator_option = Committee::SchemaValidator::Option.new(options, self, :hyper_schema)
17
17
  Committee::SchemaValidator::HyperSchema::Router.new(self, @validator_option)
18
18
  end
19
19
  end
@@ -59,7 +59,7 @@ module Committee
59
59
  schema.base_path = data['basePath'] || ''
60
60
 
61
61
  # Arbitrarily choose the first media type found in these arrays. This
62
- # appraoch could probably stand to be improved, but at least users will
62
+ # approach could probably stand to be improved, but at least users will
63
63
  # for now have the option of turning media type validation off if they so
64
64
  # choose.
65
65
  schema.consumes = data['consumes'].first
@@ -62,7 +62,7 @@ module Committee
62
62
  # And same idea: despite parameters not being schemas, the items
63
63
  # key (if preset) is actually a schema that defines each item of an
64
64
  # array type, so we can just reflect that directly onto our
65
- # artifical schema.
65
+ # artificial schema.
66
66
  if param_data["type"] == "array" && param_data["items"]
67
67
  param_schema.items = param_data["items"]
68
68
  end
@@ -16,8 +16,8 @@ module Committee
16
16
  attr_accessor :routes
17
17
  attr_reader :validator_option
18
18
 
19
- def build_router(options, is_request)
20
- @validator_option = Committee::SchemaValidator::Option.new(options, self, :hyper_schema, is_request)
19
+ def build_router(options)
20
+ @validator_option = Committee::SchemaValidator::Option.new(options, self, :hyper_schema)
21
21
  Committee::SchemaValidator::HyperSchema::Router.new(self, @validator_option)
22
22
  end
23
23
  end
@@ -23,8 +23,8 @@ module Committee
23
23
  @driver
24
24
  end
25
25
 
26
- def build_router(options, is_request)
27
- @validator_option = Committee::SchemaValidator::Option.new(options, self, :open_api_3, is_request)
26
+ def build_router(options)
27
+ @validator_option = Committee::SchemaValidator::Option.new(options, self, :open_api_3)
28
28
  Committee::SchemaValidator::OpenAPI3::Router.new(self, @validator_option)
29
29
  end
30
30
 
@@ -10,7 +10,7 @@ module Committee
10
10
  raise "needs implementation"
11
11
  end
12
12
 
13
- def build_router(options, is_request)
13
+ def build_router(options)
14
14
  raise "needs implementation"
15
15
  end
16
16
 
@@ -20,26 +20,27 @@ module Committee
20
20
  # load and build drive from JSON file
21
21
  # @param [String] schema_path
22
22
  # @return [Committee::Driver]
23
- def self.load_from_json(schema_path)
24
- load_from_data(JSON.parse(File.read(schema_path)), schema_path)
23
+ def self.load_from_json(schema_path, parser_options: {})
24
+ load_from_data(JSON.parse(File.read(schema_path)), schema_path, parser_options: parser_options)
25
25
  end
26
26
 
27
27
  # load and build drive from YAML file
28
28
  # @param [String] schema_path
29
29
  # @return [Committee::Driver]
30
- def self.load_from_yaml(schema_path)
31
- load_from_data(YAML.load_file(schema_path), schema_path)
30
+ def self.load_from_yaml(schema_path, parser_options: {})
31
+ data = YAML.respond_to?(:unsafe_load_file) ? YAML.unsafe_load_file(schema_path) : YAML.load_file(schema_path)
32
+ load_from_data(data, schema_path, parser_options: parser_options)
32
33
  end
33
34
 
34
35
  # load and build drive from file
35
36
  # @param [String] schema_path
36
37
  # @return [Committee::Driver]
37
- def self.load_from_file(schema_path)
38
+ def self.load_from_file(schema_path, parser_options: {})
38
39
  case File.extname(schema_path)
39
40
  when '.json'
40
- load_from_json(schema_path)
41
+ load_from_json(schema_path, parser_options: parser_options)
41
42
  when '.yaml', '.yml'
42
- load_from_yaml(schema_path)
43
+ load_from_yaml(schema_path, parser_options: parser_options)
43
44
  else
44
45
  raise "Committee only supports the following file extensions: '.json', '.yaml', '.yml'"
45
46
  end
@@ -48,9 +49,19 @@ module Committee
48
49
  # load and build drive from Hash object
49
50
  # @param [Hash] hash
50
51
  # @return [Committee::Driver]
51
- def self.load_from_data(hash, schema_path = nil)
52
+ def self.load_from_data(hash, schema_path = nil, parser_options: {})
52
53
  if hash['openapi']&.start_with?('3.0.')
53
- openapi = OpenAPIParser.parse_with_filepath(hash, schema_path)
54
+ # From the next major version, we want to ensure `{ strict_reference_validation: true }`
55
+ # as a parser option here, but since it may break existing implementations, just warn
56
+ # if it is not explicitly set. See: https://github.com/interagent/committee/issues/343#issuecomment-997400329
57
+ opts = parser_options.dup
58
+
59
+ Committee.warn_deprecated_until_6(!opts.key?(:strict_reference_validation), 'openapi_parser will default to strict reference validation ' +
60
+ 'from next version. Pass config `strict_reference_validation: true` (or false, if you must) ' +
61
+ 'to quiet this warning.')
62
+ opts[:strict_reference_validation] ||= false
63
+
64
+ openapi = OpenAPIParser.parse_with_filepath(hash, schema_path, opts)
54
65
  return Committee::Drivers::OpenAPI3::Driver.new.parse(openapi)
55
66
  end
56
67
 
@@ -60,6 +71,7 @@ module Committee
60
71
  Committee::Drivers::HyperSchema::Driver.new
61
72
  end
62
73
 
74
+ # TODO: in the future, pass `opts` here and allow optionality in other drivers?
63
75
  driver.parse(hash)
64
76
  end
65
77
  end
@@ -13,7 +13,7 @@ module Committee
13
13
  @raise = options[:raise]
14
14
  @schema = self.class.get_schema(options)
15
15
 
16
- @router = @schema.build_router(options, is_request: Committee::Middleware::RequestValidation == self.class)
16
+ @router = @schema.build_router(options)
17
17
  @accept_request_filter = options[:accept_request_filter] || -> (_) { true }
18
18
  end
19
19
 
@@ -30,11 +30,12 @@ module Committee
30
30
  class << self
31
31
  def get_schema(options)
32
32
  schema = options[:schema]
33
- unless schema
34
- schema = Committee::Drivers::load_from_file(options[:schema_path]) if options[:schema_path]
35
-
36
- raise(ArgumentError, "Committee: need option `schema` or `schema_path`") unless schema
33
+ if !schema && options[:schema_path]
34
+ # In the future, we could have `parser_options` as an exposed config?
35
+ parser_options = options.key?(:strict_reference_validation) ? { strict_reference_validation: options[:strict_reference_validation] } : {}
36
+ schema = Committee::Drivers::load_from_file(options[:schema_path], parser_options: parser_options)
37
37
  end
38
+ raise(ArgumentError, "Committee: need option `schema` or `schema_path`") unless schema
38
39
 
39
40
  # Expect the type we want by now. If we don't have it, the user passed
40
41
  # something else non-standard in.
@@ -34,14 +34,7 @@ module Committee
34
34
  private
35
35
 
36
36
  def handle_exception(e, env)
37
- return unless @error_handler
38
-
39
- if @error_handler.arity > 1
40
- @error_handler.call(e, env)
41
- else
42
- Committee.warn_deprecated('Using `error_handler.call(exception)` is deprecated and will be change to `error_handler.call(exception, request.env)` in next major version.')
43
- @error_handler.call(e)
44
- end
37
+ @error_handler.call(e, env) if @error_handler
45
38
  end
46
39
  end
47
40
  end
@@ -7,10 +7,7 @@ module Committee
7
7
 
8
8
  def initialize(app, options = {})
9
9
  super
10
-
11
- unless options[:strict].nil?
12
- Committee.warn_deprecated("Committee: Committee::Middleware::ResponseValidation doesn't support strict option now but we'll support this option. This change break backward compatibility so please remove strict option from ResponseValidation")
13
- end
10
+ @strict = options[:strict]
14
11
  @validate_success_only = @schema.validator_option.validate_success_only
15
12
  end
16
13
 
@@ -19,7 +16,7 @@ module Committee
19
16
 
20
17
  begin
21
18
  v = build_schema_validator(request)
22
- v.response_validate(status, headers, response) if v.link_exist? && self.class.validate?(status, validate_success_only)
19
+ v.response_validate(status, headers, response, @strict) if v.link_exist? && self.class.validate?(status, validate_success_only)
23
20
 
24
21
  rescue Committee::InvalidResponse
25
22
  handle_exception($!, request.env)
@@ -38,21 +35,23 @@ module Committee
38
35
 
39
36
  class << self
40
37
  def validate?(status, validate_success_only)
41
- status != 204 && (!validate_success_only || (200...300).include?(status))
38
+ case status
39
+ when 204
40
+ false
41
+ when 200..299
42
+ true
43
+ when 304
44
+ false
45
+ else
46
+ !validate_success_only
47
+ end
42
48
  end
43
49
  end
44
50
 
45
51
  private
46
52
 
47
53
  def handle_exception(e, env)
48
- return unless @error_handler
49
-
50
- if @error_handler.arity > 1
51
- @error_handler.call(e, env)
52
- else
53
- Committee.warn_deprecated('Using `error_handler.call(exception)` is deprecated and will be change to `error_handler.call(exception, request.env)` in next major version.')
54
- @error_handler.call(e)
55
- end
54
+ @error_handler.call(e, env) if @error_handler
56
55
  end
57
56
  end
58
57
  end
@@ -27,7 +27,7 @@ module Committee
27
27
  @optimistic_json = options[:optimistic_json]
28
28
  end
29
29
 
30
- # reutrn params and is_form_params
30
+ # return params and is_form_params
31
31
  def unpack_request_params(request)
32
32
  # if Content-Type is empty or JSON, and there was a request body, try to
33
33
  # interpret it as JSON
@@ -14,7 +14,7 @@ module Committee
14
14
  end
15
15
 
16
16
  def call(status, headers, data)
17
- unless status == 204 # 204 No Content
17
+ unless [204, 304].include?(status) # 204 No Content or 304 Not Modified
18
18
  response = Rack::Response.new(data, status, headers)
19
19
  check_content_type!(response)
20
20
  end
@@ -48,9 +48,15 @@ module Committee
48
48
  private
49
49
 
50
50
  def response_media_type(response)
51
- response.content_type.to_s.split(";").first.to_s
51
+ if response.respond_to?(:media_type)
52
+ response.media_type.to_s
53
+ else
54
+ # for rack compatibility. In rack v 1.5.0, Rack::Response doesn't have media_type
55
+ response.content_type.to_s.split(";").first.to_s
56
+ end
52
57
  end
53
58
 
59
+
54
60
  def check_content_type!(response)
55
61
  if @link.media_type
56
62
  unless Rack::Mime.match?(response_media_type(response), @link.media_type)
@@ -39,18 +39,12 @@ module Committee
39
39
  raise Committee::InvalidResponse.new(e.message, original_error: e)
40
40
  end
41
41
 
42
- def validate_request_params(params, headers, validator_option)
42
+ def validate_request_params(path_params, query_params, body_params, headers, validator_option)
43
43
  ret, err = case request_operation.http_method
44
- when 'get'
45
- validate_get_request_params(params, headers, validator_option)
46
- when 'post'
47
- validate_post_request_params(params, headers, validator_option)
48
- when 'put'
49
- validate_post_request_params(params, headers, validator_option)
50
- when 'patch'
51
- validate_post_request_params(params, headers, validator_option)
52
- when 'delete'
53
- validate_get_request_params(params, headers, validator_option)
44
+ when 'get', 'delete', 'head'
45
+ validate_get_request_params(path_params, query_params, headers, validator_option)
46
+ when 'post', 'put', 'patch', 'options'
47
+ validate_post_request_params(path_params, query_params, body_params, headers, validator_option)
54
48
  else
55
49
  raise "Committee OpenAPI3 not support #{request_operation.http_method} method"
56
50
  end
@@ -58,6 +52,10 @@ module Committee
58
52
  ret
59
53
  end
60
54
 
55
+ def optional_body?
56
+ !request_operation.operation_object&.request_body&.required
57
+ end
58
+
61
59
  def valid_request_content_type?(content_type)
62
60
  if (request_body = request_operation.operation_object&.request_body)
63
61
  !request_body.select_media_type(content_type).nil?
@@ -80,28 +78,17 @@ module Committee
80
78
  # @return [OpenAPIParser::RequestOperation]
81
79
 
82
80
  # @return [OpenAPIParser::SchemaValidator::Options]
83
- def build_openapi_parser_path_option(validator_option)
84
- coerce_value = validator_option.coerce_path_params
85
- datetime_coerce_class = validator_option.coerce_date_times ? DateTime : nil
86
- validate_header = validator_option.check_header
87
- OpenAPIParser::SchemaValidator::Options.new(coerce_value: coerce_value,
88
- datetime_coerce_class: datetime_coerce_class,
89
- validate_header: validate_header)
81
+ def build_openapi_parser_body_option(validator_option)
82
+ build_openapi_parser_option(validator_option, validator_option.coerce_form_params)
90
83
  end
91
84
 
92
85
  # @return [OpenAPIParser::SchemaValidator::Options]
93
- def build_openapi_parser_post_option(validator_option)
94
- coerce_value = validator_option.coerce_form_params
95
- datetime_coerce_class = validator_option.coerce_date_times ? DateTime : nil
96
- validate_header = validator_option.check_header
97
- OpenAPIParser::SchemaValidator::Options.new(coerce_value: coerce_value,
98
- datetime_coerce_class: datetime_coerce_class,
99
- validate_header: validate_header)
86
+ def build_openapi_parser_path_option(validator_option)
87
+ build_openapi_parser_option(validator_option, validator_option.coerce_query_params)
100
88
  end
101
89
 
102
90
  # @return [OpenAPIParser::SchemaValidator::Options]
103
- def build_openapi_parser_get_option(validator_option)
104
- coerce_value = validator_option.coerce_query_params
91
+ def build_openapi_parser_option(validator_option, coerce_value)
105
92
  datetime_coerce_class = validator_option.coerce_date_times ? DateTime : nil
106
93
  validate_header = validator_option.check_header
107
94
  OpenAPIParser::SchemaValidator::Options.new(coerce_value: coerce_value,
@@ -109,24 +96,40 @@ module Committee
109
96
  validate_header: validate_header)
110
97
  end
111
98
 
112
- def validate_get_request_params(params, headers, validator_option)
99
+ def validate_get_request_params(path_params, query_params, headers, validator_option)
113
100
  # bad performance because when we coerce value, same check
114
- request_operation.validate_request_parameter(params, headers, build_openapi_parser_get_option(validator_option))
101
+ validate_path_and_query_params(path_params, query_params, headers, validator_option)
115
102
  rescue OpenAPIParser::OpenAPIError => e
116
103
  raise Committee::InvalidRequest.new(e.message, original_error: e)
117
104
  end
118
105
 
119
- def validate_post_request_params(params, headers, validator_option)
106
+ def validate_post_request_params(path_params, query_params, body_params, headers, validator_option)
120
107
  content_type = headers['Content-Type'].to_s.split(";").first.to_s
121
108
 
122
109
  # bad performance because when we coerce value, same check
123
- schema_validator_options = build_openapi_parser_post_option(validator_option)
124
- request_operation.validate_request_parameter(params, headers, schema_validator_options)
125
- request_operation.validate_request_body(content_type, params, schema_validator_options)
110
+ validate_path_and_query_params(path_params, query_params, headers, validator_option)
111
+ request_operation.validate_request_body(content_type, body_params, build_openapi_parser_body_option(validator_option))
126
112
  rescue => e
127
113
  raise Committee::InvalidRequest.new(e.message, original_error: e)
128
114
  end
129
115
 
116
+ def validate_path_and_query_params(path_params, query_params, headers, validator_option)
117
+ # it's currently impossible to validate path params and query params separately
118
+ # so we have to resort to this workaround
119
+
120
+ path_keys = path_params.keys.to_set
121
+ query_keys = query_params.keys.to_set
122
+
123
+ merged_params = query_params.merge(path_params)
124
+
125
+ request_operation.validate_request_parameter(merged_params, headers, build_openapi_parser_path_option(validator_option))
126
+
127
+ merged_params.each do |k, v|
128
+ path_params[k] = v if path_keys.include?(k)
129
+ query_params[k] = v if query_keys.include?(k)
130
+ end
131
+ end
132
+
130
133
  def response_validate_options(strict, check_header)
131
134
  ::OpenAPIParser::SchemaValidator::ResponseValidateOptions.new(strict: strict, validate_header: check_header)
132
135
  end
@@ -11,11 +11,11 @@ module Committee
11
11
  @validator_option = validator_option
12
12
  end
13
13
 
14
- def call(request, params, headers)
14
+ def call(request, path_params, query_params, body_params, headers)
15
15
  content_type = ::Committee::SchemaValidator.request_media_type(request)
16
16
  check_content_type(request, content_type) if @validator_option.check_content_type
17
17
 
18
- @operation_object.validate_request_params(params, headers, @validator_option)
18
+ @operation_object.validate_request_params(path_params, query_params, body_params, headers, @validator_option)
19
19
  end
20
20
 
21
21
  private
@@ -24,6 +24,7 @@ module Committee
24
24
  # support post, put, patch only
25
25
  return true unless request.post? || request.put? || request.patch?
26
26
  return true if @operation_object.valid_request_content_type?(content_type)
27
+ return true if @operation_object.optional_body? && empty_request?(request)
27
28
 
28
29
  message = if valid_content_types.size > 1
29
30
  types = valid_content_types.map {|x| %{"#{x}"} }.join(', ')
@@ -37,6 +38,14 @@ module Committee
37
38
  def valid_content_types
38
39
  @operation_object&.request_content_types
39
40
  end
41
+
42
+ def empty_request?(request)
43
+ return true if !request.body
44
+
45
+ data = request.body.read
46
+ request.body.rewind
47
+ data.empty?
48
+ end
40
49
  end
41
50
  end
42
51
  end
@@ -17,7 +17,7 @@ module Committee
17
17
  request_unpack(request)
18
18
  request_schema_validation(request)
19
19
 
20
- copy_coerced_data_to_query_hash(request)
20
+ copy_coerced_data_to_params(request)
21
21
  end
22
22
 
23
23
  def response_validate(status, headers, response, test_method = false)
@@ -34,6 +34,7 @@ module Committee
34
34
  full_body
35
35
  end
36
36
 
37
+ # TODO: refactoring name
37
38
  strict = test_method
38
39
  Committee::SchemaValidator::OpenAPI3::ResponseValidator.
39
40
  new(@operation_object, validator_option).
@@ -57,7 +58,19 @@ module Committee
57
58
  return unless @operation_object
58
59
 
59
60
  validator = Committee::SchemaValidator::OpenAPI3::RequestValidator.new(@operation_object, validator_option: validator_option)
60
- validator.call(request, request.env[validator_option.params_key], header(request))
61
+ validator.call(request, path_params(request), query_params(request), body_params(request), header(request))
62
+ end
63
+
64
+ def path_params(request)
65
+ request.env[validator_option.path_hash_key]
66
+ end
67
+
68
+ def query_params(request)
69
+ request.env[validator_option.query_hash_key]
70
+ end
71
+
72
+ def body_params(request)
73
+ request.env[validator_option.request_body_hash_key]
61
74
  end
62
75
 
63
76
  def header(request)
@@ -79,30 +92,22 @@ module Committee
79
92
  request.env[validator_option.path_hash_key] = coerce_path_params
80
93
 
81
94
  query_param = unpacker.unpack_query_params(request)
95
+ query_param.merge!(request_param) if request.get? && validator_option.allow_get_body
96
+ request.env[validator_option.query_hash_key] = query_param
97
+ end
82
98
 
99
+ def copy_coerced_data_to_params(request)
83
100
  order = if validator_option.parameter_overwite_by_rails_rule
84
101
  # (high priority) path_hash_key -> query_param -> request_body_hash
85
- [request.env[validator_option.request_body_hash_key], query_param, request.env[validator_option.path_hash_key]]
102
+ [validator_option.request_body_hash_key, validator_option.query_hash_key, validator_option.path_hash_key]
86
103
  else
87
104
  # (high priority) path_hash_key -> request_body_hash -> query_param
88
- [query_param, request.env[validator_option.request_body_hash_key], request.env[validator_option.path_hash_key]]
105
+ [validator_option.query_hash_key, validator_option.request_body_hash_key, validator_option.path_hash_key]
89
106
  end
90
107
 
91
108
  request.env[validator_option.params_key] = Committee::Utils.indifferent_hash
92
- order.each do |param|
93
- request.env[validator_option.params_key].merge!(Committee::Utils.deep_copy(param))
94
- end
95
- end
96
-
97
- def copy_coerced_data_to_query_hash(request)
98
- return if request.env["rack.request.query_hash"].nil? || request.env["rack.request.query_hash"].empty?
99
-
100
- query_hash_key = @validator_option.query_hash_key
101
- return unless query_hash_key
102
-
103
- request.env[query_hash_key] = {} unless request.env[query_hash_key]
104
- request.env["rack.request.query_hash"].keys.each do |k|
105
- request.env[query_hash_key][k] = request.env[validator_option.params_key][k]
109
+ order.each do |key|
110
+ request.env[validator_option.params_key].merge!(Committee::Utils.deep_copy(request.env[key]))
106
111
  end
107
112
  end
108
113
  end
@@ -27,16 +27,11 @@ module Committee
27
27
  :path_hash_key,
28
28
  :prefix
29
29
 
30
- def initialize(options, schema, schema_type, is_request)
30
+ def initialize(options, schema, schema_type)
31
31
  # Non-boolean options
32
32
  @headers_key = options[:headers_key] || "committee.headers"
33
33
  @params_key = options[:params_key] || "committee.params"
34
- @query_hash_key = if options[:query_hash_key].nil?
35
- Committee.warn_deprecated('Committee: please set query_hash_key = "rack.request.query_hash" because we\'ll change default value to "committee.query_hash" in next major version.')
36
- 'rack.request.query_hash'
37
- else
38
- options.fetch(:query_hash_key)
39
- end
34
+ @query_hash_key = options[:query_hash_key] || "committee.query_hash"
40
35
  @path_hash_key = options[:path_hash_key] || "committee.path_hash"
41
36
  @request_body_hash_key = options[:request_body_hash_key] || "committee.request_body_hash"
42
37
 
@@ -49,20 +44,8 @@ module Committee
49
44
  @check_header = options.fetch(:check_header, true)
50
45
  @coerce_recursive = options.fetch(:coerce_recursive, true)
51
46
  @optimistic_json = options.fetch(:optimistic_json, false)
52
- @parse_response_by_content_type = if options[:parse_response_by_content_type].nil?
53
- if !is_request
54
- Committee.warn_deprecated('Committee: please set parse_response_by_content_type = false because we\'ll change default value in next major version.')
55
- end
56
- false
57
- else
58
- options.fetch(:parse_response_by_content_type)
59
- end
60
- @parameter_overwite_by_rails_rule = if options[:parameter_overwite_by_rails_rule].nil?
61
- Committee.warn_deprecated('Committee: please set parameter_overwite_by_rails_rule = false because we\'ll change default value to "true" in next major version.')
62
- false
63
- else
64
- options.fetch(:parameter_overwite_by_rails_rule)
65
- end
47
+ @parse_response_by_content_type = options.fetch(:parse_response_by_content_type, true)
48
+ @parameter_overwite_by_rails_rule = options.fetch(:parameter_overwite_by_rails_rule, true)
66
49
 
67
50
  # Boolean options and have a different value by default
68
51
  @allow_get_body = options.fetch(:allow_get_body, schema.driver.default_allow_get_body)
@@ -4,7 +4,7 @@ module Committee
4
4
  module SchemaValidator
5
5
  class << self
6
6
  def request_media_type(request)
7
- request.content_type.to_s.split(";").first.to_s
7
+ request.media_type.to_s
8
8
  end
9
9
 
10
10
  # @param [String] prefix
@@ -26,7 +26,7 @@ module Committee
26
26
  status, headers, body = response_data
27
27
 
28
28
  if expected_status.nil?
29
- Committee.warn_deprecated('Pass expected response status code to check it against the corresponding schema explicitly.')
29
+ Committee.need_good_option('Pass expected response status code to check it against the corresponding schema explicitly.')
30
30
  elsif expected_status != status
31
31
  response = "Expected `#{expected_status}` status code, but it was `#{status}`."
32
32
  raise Committee::InvalidResponse.new(response)
@@ -61,7 +61,7 @@ module Committee
61
61
  end
62
62
 
63
63
  def router
64
- @router ||= schema.build_router(committee_options, true)
64
+ @router ||= schema.build_router(committee_options)
65
65
  end
66
66
 
67
67
  def schema_validator
@@ -77,12 +77,7 @@ module Committee
77
77
  end
78
78
 
79
79
  def old_behavior
80
- old_assert_behavior = committee_options.fetch(:old_assert_behavior, nil)
81
- if old_assert_behavior.nil?
82
- Committee.warn_deprecated('Now assert_schema_conform check response schema only. but we will change check request and response in future major version. so if you want to conform response only, please use assert_response_schema_confirm, or you can suppress this message and keep old behavior by setting old_assert_behavior=true.')
83
- old_assert_behavior = true
84
- end
85
- old_assert_behavior
80
+ committee_options.fetch(:old_assert_behavior, false)
86
81
  end
87
82
  end
88
83
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Committee
4
+ VERSION = '5.0.0'.freeze
5
+ end
data/lib/committee.rb CHANGED
@@ -6,6 +6,8 @@ require "json_schema"
6
6
  require "rack"
7
7
  require 'openapi_parser'
8
8
 
9
+ require_relative "committee/version"
10
+
9
11
  module Committee
10
12
  def self.debug?
11
13
  ENV["COMMITTEE_DEBUG"]
@@ -15,8 +17,13 @@ module Committee
15
17
  $stderr.puts(message) if debug?
16
18
  end
17
19
 
18
- def self.warn_deprecated(message)
19
- warn("[DEPRECATION] #{message}")
20
+ def self.need_good_option(message)
21
+ warn(message)
22
+ end
23
+
24
+ def self.warn_deprecated_until_6(cond, message)
25
+ raise "remove deprecated!" unless Committee::VERSION.start_with?("5")
26
+ warn("[DEPRECATION] #{message}") if cond
20
27
  end
21
28
  end
22
29