default_format not applied if content-type not supplied
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 10k
- Forks
- 1.2k
- Avg merge
- 14h 38m
- Merged PRs (30d)
- 92
Description
Follow-up on old issue https://github.com/ruby-grape/grape/issues/407
format and default_format are set to :json yet when content-type is not supplied it doesn't parse correctly:
curl --include \
--request POST \
--data-binary "{
\"email\": \"test@test.com\",
\"password\": \"password\"
}" \
'localhost:3000/api/v1/login'
results in:
#<Hashie::Mash {
"email": "test@test.com",
"password": "password"
}=nil>
The test doesn't handle when content type isn't supplied at all, it's given as a blank string:
it 'parses data in default format' do
subject.post '/data' do
{ x: params[:x] }
end
post '/data', '{"x":42}', 'CONTENT_TYPE' => ''
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('{"x":42}')
end
When I remove that content type and post without it, it breaks without even making it to #read_rack_input and test fails.
These are both false in #read_body_input so it returns here:
(!request.form_data? || !request.media_type)
content_type/media_type default to "application/x-www-form-urlencoded" when content-type isn't supplied. And since request.media_type is "application/x-www-form-urlencoded" instead of nil, it wouldn't use the default_format here either even if we made it to this point:
from grape/lib/grape/middleware/formatter.rb #read_rack_input
fmt = mime_types[request.media_type] if request.media_type
fmt ||= options[:default_format]
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the curl request without a Content-Type header and compare it with the existing default-format test. Read grape/lib/grape/middleware/formatter.rb, especially #read_body_input and #read_rack_input, to trace the request media type and default format handling. Done means JSON data without a Content-Type is parsed using default_format.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100