Skip to content

Commit 35ca182

Browse files
committed
Do not include the default success codes to the responses if http_codes define one already
1 parent 6dac1ae commit 35ca182

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

.rubocop_todo.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Metrics/BlockLength:
1818
# Offense count: 3
1919
# Configuration parameters: CountComments.
2020
Metrics/ClassLength:
21-
Max: 275
21+
Max: 280
2222

2323
# Offense count: 12
2424
Metrics/CyclomaticComplexity:

CHANGELOG.md

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

1111
* [#631](https://github.com/ruby-grape/grape-swagger/pull/631): Fix order of mounts with overrides - [@adie](https://github.com/adie).
1212
* [#267](https://github.com/ruby-grape/grape-swagger/pull/634): Fix mounting APIs in route_param namespaces - [@milgner](https://github.com/milgner), [@wojciechka](https://github.com/wojciechka).
13+
* [#641](https://github.com/ruby-grape/grape-swagger/pull/641): Exclude default success code if http_codes define one already - [@anakinj](https://github.com/anakinj).
1314

1415
* Your contribution here.
1516

lib/grape-swagger/endpoint.rb

+10-4
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ def params_object(route, path)
191191
end
192192

193193
def response_object(route)
194-
codes = (route.http_codes || route.options[:failure] || [])
195-
196-
codes = apply_success_codes(route) + codes
194+
codes = http_codes_from_route(route)
197195
codes.map! { |x| x.is_a?(Array) ? { code: x[0], message: x[1], model: x[2] } : x }
198196

199197
codes.each_with_object({}) do |value, memo|
@@ -222,7 +220,15 @@ def response_object(route)
222220
end
223221
end
224222

225-
def apply_success_codes(route)
223+
def http_codes_from_route(route)
224+
if route.http_codes.is_a?(Array) && route.http_codes.any? { |code| code[:code].between?(200, 299) }
225+
route.http_codes.clone
226+
else
227+
success_codes_from_route(route) + (route.http_codes || route.options[:failure] || [])
228+
end
229+
end
230+
231+
def success_codes_from_route(route)
226232
default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym]
227233
if @entity.is_a?(Hash)
228234
default_code[:code] = @entity[:code] if @entity[:code].present?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'http status code behaivours' do
6+
include_context "#{MODEL_PARSER} swagger example"
7+
8+
subject do
9+
get '/swagger_doc'
10+
JSON.parse(last_response.body)
11+
end
12+
13+
context 'when non-default success codes are deifined' do
14+
let(:app) do
15+
Class.new(Grape::API) do
16+
desc 'Has explicit http_codes defined' do
17+
http_codes [{ code: 202, message: 'We got it!' },
18+
{ code: 204, message: 'Or returned no content' }]
19+
end
20+
21+
post '/accepting_endpoint' do
22+
'We got the message!'
23+
end
24+
add_swagger_documentation
25+
end
26+
end
27+
28+
it 'only includes the defined http_codes' do
29+
expect(subject['paths']['/accepting_endpoint']['post']['responses'].keys.sort).to eq(%w[202 204].sort)
30+
end
31+
end
32+
33+
context 'when no success codes defined' do
34+
let(:app) do
35+
Class.new(Grape::API) do
36+
desc 'Has explicit http_codes defined' do
37+
http_codes [{ code: 400, message: 'Error!' },
38+
{ code: 404, message: 'Not found' }]
39+
end
40+
41+
post '/accepting_endpoint' do
42+
'We got the message!'
43+
end
44+
add_swagger_documentation
45+
end
46+
end
47+
48+
it 'adds the success code to the response' do
49+
expect(subject['paths']['/accepting_endpoint']['post']['responses'].keys.sort).to eq(%w[201 400 404].sort)
50+
end
51+
end
52+
end

0 commit comments

Comments
 (0)