Skip to content

Commit 132f232

Browse files
committed
fix #726 - Throw an error for content types not specified via format and fallback to default_format only if content-type is not specified.
1 parent 92c173b commit 132f232

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [#1564](https://github.com/ruby-grape/grape/pull/1564): Fix declared params bug with nested namespaces - [@bmarini](https://github.com/bmarini).
1919
* [#1567](https://github.com/ruby-grape/grape/pull/1567): Fix values validator when value is empty array and apply except to input array - [@jlfaber](https://github.com/jlfaber).
2020
* [#1569](https://github.com/ruby-grape/grape/pull/1569), [#1511](https://github.com/ruby-grape/grape/issues/1511): Upgrade mustermann-grape to 1.0.0 - [@namusyaka](https://github.com/namusyaka).
21+
* [#1589](https://github.com/ruby-grape/grape/pull/1589): [#726](https://github.com/ruby-grape/grape/issues/726): Use default_format when Content-type is missing and respond with 406 when Content-type is invalid - [@inclooder](https://github.com/inclooder).
2122
* Your contribution here.
2223

2324
### 0.19.1 (1/9/2017)

lib/grape/middleware/formatter.rb

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,31 +90,30 @@ def read_body_input
9090

9191
# store parsed input in env['api.request.body']
9292
def read_rack_input(body)
93-
fmt = mime_types[request.media_type] if request.media_type
94-
fmt ||= options[:default_format]
95-
if content_type_for(fmt)
96-
parser = Grape::Parser.parser_for fmt, options
97-
if parser
98-
begin
99-
body = (env[Grape::Env::API_REQUEST_BODY] = parser.call(body, env))
100-
if body.is_a?(Hash)
101-
env[Grape::Env::RACK_REQUEST_FORM_HASH] = if env[Grape::Env::RACK_REQUEST_FORM_HASH]
102-
env[Grape::Env::RACK_REQUEST_FORM_HASH].merge(body)
103-
else
104-
body
105-
end
106-
env[Grape::Env::RACK_REQUEST_FORM_INPUT] = env[Grape::Env::RACK_INPUT]
107-
end
108-
rescue Grape::Exceptions::Base => e
109-
raise e
110-
rescue StandardError => e
111-
throw :error, status: 400, message: e.message
93+
fmt = request.media_type ? mime_types[request.media_type] : options[:default_format]
94+
95+
unless content_type_for(fmt)
96+
throw :error, status: 406, message: "The requested content-type '#{request.media_type}' is not supported."
97+
end
98+
parser = Grape::Parser.parser_for fmt, options
99+
if parser
100+
begin
101+
body = (env[Grape::Env::API_REQUEST_BODY] = parser.call(body, env))
102+
if body.is_a?(Hash)
103+
env[Grape::Env::RACK_REQUEST_FORM_HASH] = if env[Grape::Env::RACK_REQUEST_FORM_HASH]
104+
env[Grape::Env::RACK_REQUEST_FORM_HASH].merge(body)
105+
else
106+
body
107+
end
108+
env[Grape::Env::RACK_REQUEST_FORM_INPUT] = env[Grape::Env::RACK_INPUT]
112109
end
113-
else
114-
env[Grape::Env::API_REQUEST_BODY] = body
110+
rescue Grape::Exceptions::Base => e
111+
raise e
112+
rescue StandardError => e
113+
throw :error, status: 400, message: e.message
115114
end
116115
else
117-
throw :error, status: 406, message: "The requested content-type '#{request.media_type}' is not supported."
116+
env[Grape::Env::API_REQUEST_BODY] = body
118117
end
119118
end
120119

spec/grape/endpoint_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,18 @@ def app
843843
expect(last_response.body).to eq('{"error":"The requested content-type \'application/xml\' is not supported."}')
844844
end
845845

846+
it 'does not accept text/plain in JSON format if application/json is specified as content type' do
847+
subject.format :json
848+
subject.default_format :json
849+
subject.put '/request_body' do
850+
params[:user]
851+
end
852+
put '/request_body', MultiJson.dump(user: 'Bob'), 'CONTENT_TYPE' => 'text/plain'
853+
854+
expect(last_response.status).to eq(406)
855+
expect(last_response.body).to eq('{"error":"The requested content-type \'text/plain\' is not supported."}')
856+
end
857+
846858
context 'content type with params' do
847859
before do
848860
subject.format :json

0 commit comments

Comments
 (0)