Skip to content

Commit 7f6dc0d

Browse files
committed
Fix handling of HTTP status codes from routes
1 parent 3057a2c commit 7f6dc0d

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

.rubocop_todo.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Metrics/AbcSize:
2121
# Offense count: 3
2222
# Configuration parameters: CountComments.
2323
Metrics/ClassLength:
24-
Max: 283
24+
Max: 287
2525

2626
# Offense count: 10
2727
Metrics/CyclomaticComplexity:

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#### Fixes
1111

1212
* [#664](https://github.com/ruby-grape/grape-swagger/pull/662): Removed all references to obsolete `hide_format` parameter - [@jonmchan](https://github.com/jonmchan).
13+
* [#669](https://github.com/ruby-grape/grape-swagger/pull/669): Fix handling of http status codes from routes - [@milgner](https://github.com/milgner).
1314

1415
### 0.28.0 (February 3, 2018)
1516

lib/grape-swagger/endpoint.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,13 @@ def response_object(route)
224224
end
225225
end
226226

227+
def success_code?(code)
228+
status = code.is_a?(Array) ? code.first : code[:code]
229+
status.between?(200, 299)
230+
end
231+
227232
def http_codes_from_route(route)
228-
if route.http_codes.is_a?(Array) && route.http_codes.any? { |code| code[:code].between?(200, 299) }
233+
if route.http_codes.is_a?(Array) && route.http_codes.any? { |code| success_code?(code) }
229234
route.http_codes.clone
230235
else
231236
success_codes_from_route(route) + (route.http_codes || route.options[:failure] || [])

spec/swagger_v2/api_swagger_v2_status_codes_spec.rb

+22-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
require 'spec_helper'
44

5-
describe 'http status code behaivours' do
5+
describe 'http status code behaviours' do
66
include_context "#{MODEL_PARSER} swagger example"
77

88
subject do
99
get '/swagger_doc'
1010
JSON.parse(last_response.body)
1111
end
1212

13-
context 'when non-default success codes are deifined' do
13+
context 'when non-default success codes are defined' do
1414
let(:app) do
1515
Class.new(Grape::API) do
1616
desc 'Has explicit success http_codes defined' do
@@ -31,6 +31,26 @@
3131
end
3232
end
3333

34+
context 'when success and failures are defined' do
35+
let(:app) do
36+
Class.new(Grape::API) do
37+
desc 'Has explicit success http_codes defined' do
38+
success code: 202, model: Entities::UseResponse, message: 'a changed status code'
39+
failure [[400, 'Bad Request']]
40+
end
41+
42+
post '/accepting_endpoint' do
43+
'We got the message!'
44+
end
45+
add_swagger_documentation
46+
end
47+
end
48+
49+
it 'only includes the defined http codes' do
50+
expect(subject['paths']['/accepting_endpoint']['post']['responses'].keys.sort).to eq(%w[202 400].sort)
51+
end
52+
end
53+
3454
context 'when no success codes defined' do
3555
let(:app) do
3656
Class.new(Grape::API) do

0 commit comments

Comments
 (0)