Skip to content

Commit 95be315

Browse files
Set query parameter for array of primitive types (#929)
* Set query parameter for array of primitive types * Update PR number in changelog * refactor param_type method to handle array params. --------- Co-authored-by: Eugene Lim <limzhiweieugene@gmail.com>
1 parent e7856de commit 95be315

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

55
* [#927](https://github.com/ruby-grape/grape-swagger/pull/927): Set default parameter location based on consumes - [@spaceraccoon](https://github.com/spaceraccoon)
6+
* [#929](https://github.com/ruby-grape/grape-swagger/pull/929): Set query parameter for array of primitive types - [@spaceraccoon](https://github.com/spaceraccoon)
67
* Your contribution here.
78

89
#### Fixes

lib/grape-swagger/doc_methods/data_type.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ def primitives
7575
PRIMITIVE_MAPPINGS.keys.map(&:downcase)
7676
end
7777

78+
def query_array_primitive?(type)
79+
query_array_primitives.include?(type.to_s.downcase)
80+
end
81+
82+
def query_array_primitives
83+
primitives << 'string'
84+
end
85+
7886
def mapping(value)
7987
PRIMITIVE_MAPPINGS[value] || 'string'
8088
end

lib/grape-swagger/doc_methods/parse_params.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,17 @@ def document_add_extensions(settings)
7070

7171
def document_array_param(value_type, definitions)
7272
if value_type[:documentation].present?
73-
param_type = value_type[:documentation][:param_type] || value_type[:documentation][:in]
7473
doc_type = value_type[:documentation][:type]
7574
type = DataType.mapping(doc_type) if doc_type && !DataType.request_primitive?(doc_type)
7675
collection_format = value_type[:documentation][:collectionFormat]
7776
end
7877

79-
param_type ||= value_type[:param_type]
80-
8178
array_items = parse_array_item(
8279
definitions,
8380
type,
8481
value_type
8582
)
8683

87-
@parsed_param[:in] = param_type || 'formData'
8884
@parsed_param[:items] = array_items
8985
@parsed_param[:type] = 'array'
9086
@parsed_param[:collectionFormat] = collection_format if DataType.collections.include?(collection_format)
@@ -150,7 +146,7 @@ def document_example(settings)
150146

151147
def param_type(value_type, consumes)
152148
param_type = value_type[:param_type] || value_type[:in]
153-
if value_type[:path].include?("{#{value_type[:param_name]}}")
149+
if !value_type[:is_array] && value_type[:path].include?("{#{value_type[:param_name]}}")
154150
'path'
155151
elsif param_type
156152
param_type
@@ -160,6 +156,8 @@ def param_type(value_type, consumes)
160156
else
161157
'body'
162158
end
159+
elsif value_type[:is_array] && !DataType.query_array_primitive?(value_type[:data_type])
160+
'formData'
163161
else
164162
'query'
165163
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe '#883 Group Params as Array' do
6+
let(:app) do
7+
Class.new(Grape::API) do
8+
namespace :issue_883 do
9+
params do
10+
requires :array_of_string, type: [String]
11+
requires :array_of_integer, type: [Integer]
12+
end
13+
get '/get_primitive_array_parameters' do
14+
'accepts array query parameters of primitive value types'
15+
end
16+
17+
params do
18+
requires :array_of, type: Array, documentation: { type: 'link', is_array: true }
19+
end
20+
get '/get_object_array_parameters' do
21+
'does not accept array query parameters of object value types'
22+
end
23+
end
24+
add_swagger_documentation
25+
end
26+
end
27+
28+
describe 'retrieves the documentation for typed group range parameters' do
29+
subject do
30+
get '/swagger_doc'
31+
JSON.parse(last_response.body)
32+
end
33+
34+
specify do
35+
expect(subject['paths']['/issue_883/get_primitive_array_parameters']['get']['parameters']).to eql(
36+
[
37+
{ 'in' => 'query', 'name' => 'array_of_string', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true },
38+
{ 'in' => 'query', 'name' => 'array_of_integer', 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32' }, 'required' => true }
39+
]
40+
)
41+
expect(subject['paths']['/issue_883/get_object_array_parameters']['get']['parameters']).to eql(
42+
[{ 'in' => 'formData', 'items' => { 'type' => 'string' }, 'name' => 'array_of', 'required' => true, 'type' => 'array' }]
43+
)
44+
end
45+
end
46+
end

spec/swagger_v2/params_array_collection_format_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def app
5252
specify do
5353
expect(subject['paths']['/array_of_strings_without_collection_format']['get']['parameters']).to eql(
5454
[
55-
{ 'in' => 'formData', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'description' => 'array in csv collection format' }
55+
{ 'in' => 'query', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'description' => 'array in csv collection format' }
5656
]
5757
)
5858
end
@@ -67,7 +67,7 @@ def app
6767
specify do
6868
expect(subject['paths']['/array_of_strings_multi_collection_format']['get']['parameters']).to eql(
6969
[
70-
{ 'in' => 'formData', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'collectionFormat' => 'multi', 'description' => 'array in multi collection format' }
70+
{ 'in' => 'query', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'collectionFormat' => 'multi', 'description' => 'array in multi collection format' }
7171
]
7272
)
7373
end
@@ -82,7 +82,7 @@ def app
8282
specify do
8383
expect(subject['paths']['/array_of_strings_brackets_collection_format']['get']['parameters']).to eql(
8484
[
85-
{ 'in' => 'formData', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'collectionFormat' => 'brackets', 'description' => 'array in brackets collection format' }
85+
{ 'in' => 'query', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'collectionFormat' => 'brackets', 'description' => 'array in brackets collection format' }
8686
]
8787
)
8888
end
@@ -97,7 +97,7 @@ def app
9797
specify do
9898
expect(subject['paths']['/array_of_strings_invalid_collection_format']['get']['parameters']).to eql(
9999
[
100-
{ 'in' => 'formData', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false }
100+
{ 'in' => 'query', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false }
101101
]
102102
)
103103
end

0 commit comments

Comments
 (0)