Skip to content

Commit 647e0cb

Browse files
committed
Bypass formatting for statuses with no entity-body.
1 parent 20f1967 commit 647e0cb

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
* Your contribution here.
77

8+
* [#1190](https://github.com/ruby-grape/grape/putt/1190): Bypass formatting for statuses with no entity-body - [@tylerdooling](https://github.com/tylerdooling).
89
* [#1188](https://github.com/ruby-grape/grape/putt/1188): Allow parameters with more than one type - [@dslh](https://github.com/dslh).
910
* [#1179](https://github.com/ruby-grape/grape/pull/1179): Allow all RFC6838 valid characters in header vendor - [@suan](https://github.com/suan).
1011
* [#1170](https://github.com/ruby-grape/grape/pull/1170): Allow dashes and periods in header vendor - [@suan](https://github.com/suan).

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,11 @@ Built-in formatters are the following.
19761976
* `:serializable_hash`: use object's `serializable_hash` when available, otherwise fallback to `:json`
19771977
* `:binary`: data will be returned "as is"
19781978

1979+
Response statuses that indicate no content as defined by [Rack](https://github.com/rack)
1980+
[here](https://github.com/rack/rack/blob/master/lib/rack/utils.rb#L567)
1981+
will bypass serialization and the body entity - though there should be none -
1982+
will not be modified.
1983+
19791984
### JSONP
19801985

19811986
Grape supports JSONP via [Rack::JSONP](https://github.com/rack/rack-contrib), part of the

UPGRADING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ Identical endpoints with different versions now work correctly. A regression int
1515

1616
See [#1114](https://github.com/ruby-grape/grape/pull/1114) for more information.
1717

18+
#### Bypasses formatters when status code indicates no content
19+
20+
To be consistent with rack and it's handling of standard responses
21+
associated with no content, both default and custom formatters will now
22+
be bypassed when processing responses for status codes defined [by rack](https://github.com/rack/rack/blob/master/lib/rack/utils.rb#L567)
23+
24+
See [#1190](https://github.com/ruby-grape/grape/pull/1190) for more information.
25+
1826
### Upgrading to >= 0.12.0
1927

2028
#### Changes in middleware

lib/grape/middleware/formatter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def before
2020

2121
def after
2222
status, headers, bodies = *@app_response
23+
return @app_response if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status)
2324

2425
if bodies.is_a?(Grape::Util::FileResponse)
2526
headers = ensure_content_type(headers)

spec/grape/middleware/formatter_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@ def to_xml
201201
end
202202
end
203203

204+
context 'no content responses' do
205+
let(:no_content_response) { ->(status) { [status, {}, ['']] } }
206+
207+
Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.each do |status|
208+
it "does not modify a #{status} response" do
209+
expected_response = no_content_response[status]
210+
allow(app).to receive(:call).and_return(expected_response)
211+
expect(subject.call({})).to eq(expected_response)
212+
end
213+
end
214+
end
215+
204216
context 'input' do
205217
%w(POST PATCH PUT DELETE).each do |method|
206218
['application/json', 'application/json; charset=utf-8'].each do |content_type|

0 commit comments

Comments
 (0)