Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/committee/request_unpacker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initialize(options = {})
def unpack_request_params(request)
# if Content-Type is empty or JSON, and there was a request body, try to
# interpret it as JSON
json_body = !request.media_type || request.media_type =~ %r{application/(?:.*\+)?json}
json_body = !request.media_type || Committee::SchemaValidator.json_media_type?(request.media_type)
params = if json_body
parse_json(request)
elsif @optimistic_json
Expand Down
2 changes: 1 addition & 1 deletion lib/committee/test/except_parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def restore

def json_content_type?
mt = @request.media_type
mt.nil? || mt.match?(%r{application/(?:.*\+)?json})
mt.nil? || Committee::SchemaValidator.json_media_type?(mt)
end

def form_content_type?
Expand Down
20 changes: 20 additions & 0 deletions test/middleware/request_validation_open_api_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ def app
end

describe "raw request bodies" do
['application/json-seq', 'application/json-seq; charset=utf-8'].each do |content_type|
it "validates #{content_type} as a raw body and preserves it for the application" do
body = "\x1e{\"x\":\"y\"}\n\x1e{\"a\":\"b\"}\n"
document = open_api_3_data
document['paths']['/characters']['post']['requestBody']['content'] = { 'application/json-seq' => { 'schema' => { 'type' => 'string' } } }
schema = Committee::Drivers.load_from_data(document, open_api_3_schema_path, parser_options: { strict_reference_validation: true })
check_body = lambda { |env|
assert_equal body, env['committee.request_body_hash']
assert_equal body, env['rack.input'].read
[200, {}, []]
}
@app = new_rack_app_with_lambda(check_body, schema: schema)

header 'Content-Type', content_type
post '/characters', body

assert_equal 200, last_response.status, last_response.body
end
end

it "validates text and preserves the body for the application" do
check_body = lambda { |env|
assert_equal "hello", env['committee.request_body_hash']
Expand Down
10 changes: 10 additions & 0 deletions test/request_unpacker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
assert_equal([{}, false], unpacker.unpack_request_params(request))
end

describe "JSON media type boundaries" do
%w[application/json-seq application/problem+json-seq x-application/json].each do |content_type|
it "does not unpack JSON on #{content_type}" do
request = Rack::Request.new({ "CONTENT_TYPE" => content_type, "rack.input" => StringIO.new('{"x":"y"}') })

assert_equal [{}, false], Committee::RequestUnpacker.new.unpack_request_params(request)
end
end
end

it "doesn't unpack JSON under other Content-Types" do
%w[application/x-www-form-urlencoded multipart/form-data].each do |content_type|
env = { "CONTENT_TYPE" => content_type, "rack.input" => StringIO.new('{"x":"y"}'), }
Expand Down
13 changes: 13 additions & 0 deletions test/test/methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,19 @@ def response_data
end
end

it "leaves application/json-seq bodies unchanged when excepting named fields" do
body = '{ "existing": "value" }'
request = Rack::Request.new(Rack::MockRequest.env_for('/characters', method: 'POST', input: body, 'CONTENT_TYPE' => 'application/json-seq'))
handler = Committee::Test::ExceptParameter.new(request, @committee_options)

handler.apply(body: ['required_string'])

assert_equal body, request.body.read
request.body.rewind
handler.restore
assert_equal body, request.body.read
end

describe "with schema_path option" do
before do
@committee_options = { schema_path: open_api_3_schema_path }
Expand Down