diff --git a/lib/committee/schema_validator/open_api_3/operation_wrapper.rb b/lib/committee/schema_validator/open_api_3/operation_wrapper.rb index f5ba9422..794e27a2 100644 --- a/lib/committee/schema_validator/open_api_3/operation_wrapper.rb +++ b/lib/committee/schema_validator/open_api_3/operation_wrapper.rb @@ -90,8 +90,12 @@ def build_openapi_parser_path_option(validator_option) end # @return [OpenAPIParser::SchemaValidator::Options] - def build_openapi_parser_post_option(validator_option) - coerce_value = validator_option.coerce_form_params + def build_openapi_parser_post_option(validator_option, content_type) + coerce_value = if content_type =~ %r{application/(?:.*\+)?json} + false + else + validator_option.coerce_form_params + end datetime_coerce_class = validator_option.coerce_date_times ? DateTime : nil validate_header = validator_option.check_header OpenAPIParser::SchemaValidator::Options.new(coerce_value: coerce_value, @@ -120,7 +124,7 @@ def validate_post_request_params(params, headers, validator_option) content_type = headers['Content-Type'].to_s.split(";").first.to_s # bad performance because when we coerce value, same check - schema_validator_options = build_openapi_parser_post_option(validator_option) + schema_validator_options = build_openapi_parser_post_option(validator_option, content_type) request_operation.validate_request_parameter(params, headers, schema_validator_options) request_operation.validate_request_body(content_type, params, schema_validator_options) rescue => e diff --git a/test/schema_validator/open_api_3/operation_wrapper_test.rb b/test/schema_validator/open_api_3/operation_wrapper_test.rb index 119d8365..b4424b4b 100644 --- a/test/schema_validator/open_api_3/operation_wrapper_test.rb +++ b/test/schema_validator/open_api_3/operation_wrapper_test.rb @@ -10,8 +10,10 @@ @path = '/validate' @method = 'post' - # TODO: delete when 5.0.0 released because default value changed options = {} + options[:coerce_form_params] = true + + # TODO: delete when 5.0.0 released because default value changed options[:parse_response_by_content_type] = true if options[:parse_response_by_content_type] == nil @validator_option = Committee::SchemaValidator::Option.new(options, open_api_3_schema, :open_api_3) @@ -61,6 +63,21 @@ def operation_object assert_kind_of(OpenAPIParser::OpenAPIError, e.original_error) end + it 'coercing for application/xml' do + header = { 'Content-Type' => 'application/xml' } + operation_object.validate_request_params({'integer' => '1'}, header, @validator_option) + assert true + end + + it 'no coercing for application/json' do + e = assert_raises(Committee::InvalidRequest) { + operation_object.validate_request_params({'integer' => '1'}, HEADER, @validator_option) + } + + assert_match(/expected integer, but received String: 1/i, e.message) + assert_kind_of(OpenAPIParser::OpenAPIError, e.original_error) + end + it 'support put method' do @method = "put" operation_object.validate_request_params({"string" => "str"}, HEADER, @validator_option)