Skip to content

Commit e4b3778

Browse files
Fix issue where array params are sometimes omitted when array_use_braces is true
1 parent e0282ab commit e4b3778

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

lib/grape-swagger/doc_methods/parse_params.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def call(param, settings, path, route, definitions, consumes) # rubocop:disable
1717
in: param_type(value_type, consumes),
1818
name: settings[:full_name] || param
1919
}
20+
fix_name_of_body_array_param(value_type)
2021

2122
# optional properties
2223
document_description(settings)
@@ -35,6 +36,12 @@ def call(param, settings, path, route, definitions, consumes) # rubocop:disable
3536

3637
private
3738

39+
def fix_name_of_body_array_param(value_type)
40+
return unless @parsed_param[:in] == 'body' && value_type[:is_array]
41+
42+
@parsed_param[:name] = @parsed_param[:name].delete_suffix('[]')
43+
end
44+
3845
def document_description(settings)
3946
description = settings[:desc] || settings[:description]
4047
@parsed_param[:description] = description if description

spec/swagger_v2/params_array_spec.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@
9595
{ 'declared_params' => declared(params) }
9696
end
9797

98+
helpers do
99+
params :common_params do
100+
requires :array_of_string, type: Array[String]
101+
requires :array_of_integer, type: Array[Integer]
102+
end
103+
end
104+
105+
params do
106+
use :common_params
107+
end
108+
get '/endpoint_with_common_params' do
109+
{ 'declared_params' => declared(params) }
110+
end
111+
112+
params do
113+
use :common_params
114+
end
115+
post '/endpoint_with_common_params' do
116+
{ 'declared_params' => declared(params) }
117+
end
118+
98119
add_swagger_documentation array_use_braces: array_use_braces
99120
end
100121
end
@@ -220,6 +241,53 @@
220241
expect(subject['paths']['/array_of_entities']['post']['parameters']).to eql(parameters)
221242
end
222243
end
244+
245+
describe 'retrieves the documentation for parameters shared between GET and POST endpoints' do
246+
subject do
247+
get '/swagger_doc/endpoint_with_common_params'
248+
JSON.parse(last_response.body)
249+
end
250+
251+
describe 'for non-body parameters' do
252+
specify do
253+
expect(subject['paths']['/endpoint_with_common_params']['get']['parameters']).to eql(
254+
[
255+
{ 'in' => 'query', 'name' => "array_of_string#{braces}", 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true },
256+
{ 'in' => 'query', 'name' => "array_of_integer#{braces}", 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32' }, 'required' => true }
257+
]
258+
)
259+
end
260+
end
261+
262+
describe 'for body parameters' do
263+
specify do
264+
expect(subject['paths']['/endpoint_with_common_params']['post']['parameters']).to eql(
265+
[
266+
{ 'in' => 'body', 'name' => 'postEndpointWithCommonParams', 'schema' => { '$ref' => '#/definitions/postEndpointWithCommonParams' }, 'required' => true }
267+
]
268+
)
269+
expect(subject['definitions']['postEndpointWithCommonParams']).to eql(
270+
'type' => 'object',
271+
'properties' => {
272+
'array_of_string' => { # no braces, even if array_use_braces is true
273+
'type' => 'array',
274+
'items' => {
275+
'type' => 'string'
276+
}
277+
},
278+
'array_of_integer' => { # no braces, even if array_use_braces is true
279+
'type' => 'array',
280+
'items' => {
281+
'type' => 'integer',
282+
'format' => 'int32'
283+
}
284+
}
285+
},
286+
'required' => %w[array_of_string array_of_integer]
287+
)
288+
end
289+
end
290+
end
223291
end
224292
end
225293
end

0 commit comments

Comments
 (0)