Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable coerece_form_params option in the case of application/json #301

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/committee/schema_validator/open_api_3/operation_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion test/schema_validator/open_api_3/operation_wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down