File tree 4 files changed +64
-5
lines changed
4 files changed +64
-5
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ Metrics/BlockLength:
18
18
# Offense count: 3
19
19
# Configuration parameters: CountComments.
20
20
Metrics/ClassLength :
21
- Max : 275
21
+ Max : 280
22
22
23
23
# Offense count: 12
24
24
Metrics/CyclomaticComplexity :
Original file line number Diff line number Diff line change 10
10
11
11
* [ #631 ] ( https://github.com/ruby-grape/grape-swagger/pull/631 ) : Fix order of mounts with overrides - [ @adie ] ( https://github.com/adie ) .
12
12
* [ #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 ) .
13
14
14
15
* Your contribution here.
15
16
Original file line number Diff line number Diff line change @@ -191,9 +191,7 @@ def params_object(route, path)
191
191
end
192
192
193
193
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 )
197
195
codes . map! { |x | x . is_a? ( Array ) ? { code : x [ 0 ] , message : x [ 1 ] , model : x [ 2 ] } : x }
198
196
199
197
codes . each_with_object ( { } ) do |value , memo |
@@ -222,7 +220,15 @@ def response_object(route)
222
220
end
223
221
end
224
222
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 )
226
232
default_code = GrapeSwagger ::DocMethods ::StatusCodes . get [ route . request_method . downcase . to_sym ]
227
233
if @entity . is_a? ( Hash )
228
234
default_code [ :code ] = @entity [ :code ] if @entity [ :code ] . present?
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments