Skip to content

Allow JSON formatter to return a blank body #618

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

Closed
wants to merge 3 commits into from
Closed
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 @@ -10,6 +10,7 @@ Next Release

* [#614](https://github.com/intridea/grape/pull/614): Params with `nil` value are now refused by `RegexpValidator` - [@dm1try](https://github.com/dm1try).
* [#494](https://github.com/intridea/grape/issues/494): Fixed performance issue with requests carrying a large payload - [@dblock](https://github.com/dblock).
* [#618](https://github.com/intridea/grape/pull/618): When body is `nil` or `''`, make the response a blank body even for JSON - [@mbleigh](https://github.com/mbleigh)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing a period at the end :)


0.7.0 (4/2/2013)
=================
Expand Down
1 change: 1 addition & 0 deletions lib/grape/formatter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Formatter
module Json
class << self
def call(object, env)
return '' if object == '' || object.nil?
return object.to_json if object.respond_to?(:to_json)
MultiJson.dump(object)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,11 @@ def subject.enable_root_route!
it "allows a(n) #{object.class} json object in params" do
subject.format :json
subject.send(verb) do
env['api.request.body']
{ val: env['api.request.body'] }
end
send verb, '/', MultiJson.dump(object), 'CONTENT_TYPE' => 'application/json'
last_response.status.should == (verb == :post ? 201 : 200)
last_response.body.should eql MultiJson.dump(object)
last_response.body.should eql MultiJson.dump(val: object)
last_request.params.should eql Hash.new
end
it "stores input in api.request.input" do
Expand Down
5 changes: 5 additions & 0 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def to_json
subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json').last.each { |b| b.should == '"bar"' }
end

it 'returns an empty body instead of "" when the body is a blank string or nil' do
@body = ''
subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json').last.each { |b| b.should == '' }
end

it 'calls #to_json if the content type is jsonapi' do
@body = { 'foos' => [{ 'bar' => 'baz' }] }
@body.instance_eval do
Expand Down