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

Detect content type correctly if raised any low level errors #1427

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#### Fixes

* [#1427](https://github.com/ruby-grape/grape/pull/1427): Handling an invalid query string when trying to detect the response format - [@iCEAGE](https://github.com/iCEAGE).
* [#1405](https://github.com/ruby-grape/grape/pull/1405): Fix priority of `rescue_from` clauses applying - [@hedgesky](https://github.com/hedgesky).
* [#1365](https://github.com/ruby-grape/grape/pull/1365): Fix finding exception handler in error middleware - [@ktimothy](https://github.com/ktimothy).
* [#1380](https://github.com/ruby-grape/grape/pull/1380): Fix `allow_blank: false` for `Time` attributes with valid values causes `NoMethodError` - [@ipkes](https://github.com/ipkes).
Expand Down
6 changes: 5 additions & 1 deletion lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ def format_from_extension
end

def format_from_params
fmt = Rack::Utils.parse_nested_query(env[Grape::Http::Headers::QUERY_STRING])[Grape::Http::Headers::FORMAT]
fmt = begin
Rack::Utils.parse_nested_query(env[Grape::Http::Headers::QUERY_STRING])[Grape::Http::Headers::FORMAT]
rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError
nil
end
# avoid symbol memory leak on an unknown format
return fmt.to_sym if content_type_for(fmt)
fmt
Expand Down
18 changes: 18 additions & 0 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ def to_xml
expect(subject.env['api.format']).to eq(:json)
end

it 'uses the requested format with invalid parameter type if provided in headers' do
_, headers, = subject.call(
'PATH_INFO' => '/info',
'QUERY_STRING' => 'id=12&id[]=12',
'HTTP_ACCEPT' => 'application/json'
)
expect(headers['Content-type']).to eq('application/json')
end

it 'uses the requested format with invalid byte sequence in UTF-8 if provided in headers' do
_, headers, = subject.call(
'PATH_INFO' => '/info',
'QUERY_STRING' => 'foo%81E=1',
'HTTP_ACCEPT' => 'application/json'
)
expect(headers['Content-type']).to eq('application/json')
end

it 'handles quality rankings mixed with nothing' do
subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json,application/xml; q=1.0')
expect(subject.env['api.format']).to eq(:xml)
Expand Down