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

Fix multiple calls to body_params returning correct value #298

Merged
merged 2 commits into from
Dec 28, 2012
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
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
====================

* [#296](https://github.com/intridea/grape/issues/296): Fix: ArgumentError with default error formatter - [@dblock](https://github.com/dblock).
* [#298](https://github.com/intridea/grape/pull/298): Fix: Subsequent calls to `body_params` would fail due to read on an IO. - [@justinmcp](https://github.com/justinmcp).
* Your contribution here.

0.2.3 (24/12/2012)
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ def declared(params, options = {})
# Pull out request body params if the content type matches and we're on a POST or PUT
def body_params
if ['POST', 'PUT'].include?(request.request_method.to_s.upcase) && request.content_length.to_i > 0
return case env['CONTENT_TYPE']
return @body_params ||=
case env['CONTENT_TYPE']
when 'application/json'
MultiJson.decode(request.body.read)
when 'application/xml'
Expand Down
7 changes: 7 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ def app; subject end
end
post '/omitted_params', MultiJson.encode(:user => 'Blah'), {'CONTENT_TYPE' => 'application/json'}
end

it 'should return an equivalent hash on subsequenst calls' do
subject.post '/two_times' do
body_params.should == body_params
end
post '/two_times', MultiJson.encode(:user => 'Bobby T.'), {'CONTENT_TYPE' => 'application/json'}
end
end
end

Expand Down