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

Change HTTP DELETE status code and body logic, use nil for HTTP 204 #1774

Open
wants to merge 5 commits 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* [#1776](https://github.com/ruby-grape/grape/pull/1776): Validate response returned by the exception handler - [@darren987469](https://github.com/darren987469).
* [#1787](https://github.com/ruby-grape/grape/pull/1787): Add documented but not implemented ability to `.insert` a middleware in the stack - [@michaellennox](https://github.com/michaellennox).
* [#1788](https://github.com/ruby-grape/grape/pull/1788): Fix route requirements bug - [@darren987469](https://github.com/darren987469), [@darrellnash](https://github.com/darrellnash).
* [#1774](https://github.com/ruby-grape/grape/pull/1774): Change HTTP delete status code and response body logic. Fix WEBrick compatibility - [@basjanssen](https://github.com/basjanssen).
Copy link
Member

Choose a reason for hiding this comment

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

Lets clarify this a little bit. How about this:

Returning an empty Hash or Array will no longer return 204 from HTTP DELETE

What's the story with WEBrick?


### 1.1.0 (8/4/2018)

Expand Down Expand Up @@ -667,4 +668,4 @@

### 0.1.0 (11/13/2010)

* Initial public release - [@mbleigh](https://github.com/mbleigh).
* Initial public release - [@mbleigh](https://github.com/mbleigh).
4 changes: 4 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Upgrading Grape
===============
### Upgrading to >= ?
Copy link
Member

Choose a reason for hiding this comment

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

Since this is a change in API I would make this 1.2, so bump that version part of this PR.

The section below becomes "Upgrading to >= 1.2".

The explanation can be clearer.

Instead of "Currently" say "Since version 1.2 returning ...".

There's also confusion here between body false and body nil. I would say "using body false or body nil everywhere.

Match the title to Returning an empty Hash or Array will no longer return 204 from HTTP DELETE.

Add a blank line after ;)

#### Changes in HTTP Delete Response Body and HTTP status code
Previously, using `body false` or returning an empty Hash/Array would result in a empty response body and HTTP 204 (No content).
Currently, returning an empty Hash or Array will return the corresponding json/xml string and HTTP 200. Calling `body nil` or setting the response body to `nil` or using `return_no_content` will return an empty response body and HTTP 204.

### Upgrading to >= 1.1.1

Expand Down
25 changes: 15 additions & 10 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ def status(status = nil)
when Grape::Http::Headers::POST
201
when Grape::Http::Headers::DELETE
if @body.present?
200
else
if @body.nil?
204
else
200
end
else
200
Expand Down Expand Up @@ -222,12 +222,17 @@ def cookies
# end
#
# GET /body # => "Body"
def body(value = nil)
if value
@body = value
elsif value == false
@body = ''
status 204
def body(*value)
raise ArgumentError 'you can only set a body with one argument' if value.length > 1
Copy link
Member

Choose a reason for hiding this comment

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

I think the text is not required, just raise ArgumentError.

What if value is nil, what's nil.length?

This needs a test.

You can also turn this into a separate PR if you want.


if value.length == 1
value = value.first
if value.nil?
@body = ''
status 204
else
@body = value
end
else
@body
end
Expand All @@ -244,7 +249,7 @@ def body(value = nil)
# DELETE /12 # => 204 No Content, ""
def return_no_content
status 204
body false
body nil
end

# Allows you to define the response as a file-like object.
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 @@ -3585,10 +3585,10 @@ def before
end

context 'body' do
context 'false' do
context 'nil' do
before do
subject.get '/blank' do
body false
body nil
end
end
it 'returns blank body' do
Expand Down
43 changes: 36 additions & 7 deletions spec/grape/dsl/inside_route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,40 @@ def initialize
expect(subject.status).to eq 204
end

it 'defaults to 200 on DELETE with a body present' do
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
subject.body 'content here'
expect(subject).to receive(:request).and_return(request)
expect(subject.status).to eq 200
describe 'what is regarded as content on DELETE' do
it 'regards a string as content' do
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
subject.body 'content here'
expect(subject).to receive(:request).and_return(request)
expect(subject.status).to eq 200
end

it 'regards an empty string as content' do
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
subject.body ''
expect(subject).to receive(:request).and_return(request)
expect(subject.status).to eq 200
end

it 'regards an empty hash as content' do
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
subject.body({}) # Use brackets, because it's a block otherwise
expect(subject).to receive(:request).and_return(request)
expect(subject.status).to eq 200
end

it 'regards an empty array as content' do
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
subject.body []
expect(subject).to receive(:request).and_return(request)
expect(subject.status).to eq 200
end

it 'regards a nil as no content' do
Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
subject.body nil
expect(subject.status).to eq 204
end
end

it 'returns status set' do
Expand Down Expand Up @@ -184,9 +213,9 @@ def initialize
end
end

describe 'false' do
describe 'nil' do
before do
subject.body false
subject.body nil
end

it 'sets status to 204' do
Expand Down
10 changes: 5 additions & 5 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1320,18 +1320,18 @@ def memoized
end
end

describe 'delete 204, with nil has return value (no explicit body)' do
describe 'delete 200, with empty string as return value' do
it 'responds to /example delete method' do
subject.delete(:example) { nil }
subject.delete(:example) { '' }
delete '/example'
expect(last_response.status).to eql 204
expect(last_response.status).to eql 200
expect(last_response.body).to be_empty
end
end

describe 'delete 204, with empty array has return value (no explicit body)' do
describe 'delete 204, with nil as return value (no explicit body)' do
it 'responds to /example delete method' do
subject.delete(:example) { '' }
subject.delete(:example) { nil }
delete '/example'
expect(last_response.status).to eql 204
expect(last_response.body).to be_empty
Expand Down