Skip to content

Commit 2ccb32b

Browse files
rczjnspeter scholz
authored and
peter scholz
committed
Added support for set the 'collectionFormat' of arrays (#504)
1 parent 054c54f commit 2ccb32b

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Features
44

5+
* [#504](https://github.com/ruby-grape/grape-swagger/pull/504): Added support for set the 'collectionFormat' of arrays - [@rczjns](https://github.com/rczjns).
56
* [#502](https://github.com/ruby-grape/grape-swagger/pull/502): Adds specs for rake tasks - [@LeFnord](https://github.com/LeFnord).
67
* [#501](https://github.com/ruby-grape/grape-swagger/pull/501): Adds getting of a specified resource for Rake Tasks - [@LeFnord](https://github.com/LeFnord).
78
* [#500](https://github.com/ruby-grape/grape-swagger/pull/500): Adds Rake tasks to get and validate OAPI/Swagger documentation - [@LeFnord](https://github.com/LeFnord).

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,39 @@ end
550550
}
551551
```
552552
553+
#### Collection format of arrays
554+
555+
You can set the collection format of an array, using the documentation hash.
556+
557+
Collection format determines the format of the array if type array is used. Possible values are:
558+
* csv - comma separated values foo,bar.
559+
* ssv - space separated values foo bar.
560+
* tsv - tab separated values foo\tbar.
561+
* pipes - pipe separated values foo|bar.
562+
* multi - corresponds to multiple parameter instances instead of multiple values for a single instance foo=bar&foo=baz. This is valid only for parameters in "query" or "formData".
563+
564+
```ruby
565+
params do
566+
requires :statuses, type: Array[String], documentation: { collectionFormat: 'multi' }
567+
end
568+
post :act do
569+
...
570+
end
571+
```
572+
573+
```json
574+
{
575+
"in": "formData",
576+
"name": "statuses",
577+
"type": "array",
578+
"items": {
579+
"type": "string"
580+
},
581+
"collectionFormat": "multi",
582+
"required": true
583+
}
584+
```
585+
553586
#### Multi types
554587
555588
By default when you set multi types, the first type is selected as swagger type

lib/grape-swagger/doc_methods/parse_params.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def document_array_param(value_type)
6666
param_type = value_type[:documentation][:param_type]
6767
doc_type = value_type[:documentation][:type]
6868
type = GrapeSwagger::DocMethods::DataType.mapping(doc_type) if doc_type && !DataType.request_primitive?(doc_type)
69+
collection_format = value_type[:documentation][:collectionFormat]
6970
end
7071

7172
array_items = {
@@ -76,6 +77,7 @@ def document_array_param(value_type)
7677
@parsed_param[:in] = param_type || 'formData'
7778
@parsed_param[:items] = array_items
7879
@parsed_param[:type] = 'array'
80+
@parsed_param[:collectionFormat] = collection_format if %w(csv ssv tsv pipes multi).include?(collection_format)
7981
end
8082
end
8183

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
require 'spec_helper'
2+
3+
describe 'Group Array Params, using collection format' do
4+
def app
5+
Class.new(Grape::API) do
6+
format :json
7+
8+
params do
9+
optional :array_of_strings, type: Array[String], desc: 'array in csv collection format'
10+
end
11+
12+
get '/array_of_strings_without_collection_format' do
13+
{ 'declared_params' => declared(params) }
14+
end
15+
16+
params do
17+
optional :array_of_strings, type: Array[String], desc: 'array in multi collection format', documentation: { collectionFormat: 'multi' }
18+
end
19+
20+
get '/array_of_strings_multi_collection_format' do
21+
{ 'declared_params' => declared(params) }
22+
end
23+
24+
params do
25+
optional :array_of_strings, type: Array[String], documentation: { collectionFormat: 'foo' }
26+
end
27+
28+
get '/array_of_strings_invalid_collection_format' do
29+
{ 'declared_params' => declared(params) }
30+
end
31+
32+
add_swagger_documentation
33+
end
34+
end
35+
36+
describe 'documentation for array parameter in default csv collectionFormat' do
37+
subject do
38+
get '/swagger_doc/array_of_strings_without_collection_format'
39+
JSON.parse(last_response.body)
40+
end
41+
42+
specify do
43+
expect(subject['paths']['/array_of_strings_without_collection_format']['get']['parameters']).to eql(
44+
[
45+
{ 'in' => 'formData', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'description' => 'array in csv collection format' }
46+
]
47+
)
48+
end
49+
end
50+
51+
describe 'documentation for array parameters in multi collectionFormat set from documentation' do
52+
subject do
53+
get '/swagger_doc/array_of_strings_multi_collection_format'
54+
JSON.parse(last_response.body)
55+
end
56+
57+
specify do
58+
expect(subject['paths']['/array_of_strings_multi_collection_format']['get']['parameters']).to eql(
59+
[
60+
{ 'in' => 'formData', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'collectionFormat' => 'multi', 'description' => 'array in multi collection format' }
61+
]
62+
)
63+
end
64+
end
65+
66+
describe 'documentation for array parameters with collectionFormat set to invalid option' do
67+
subject do
68+
get '/swagger_doc/array_of_strings_invalid_collection_format'
69+
JSON.parse(last_response.body)
70+
end
71+
72+
specify do
73+
expect(subject['paths']['/array_of_strings_invalid_collection_format']['get']['parameters']).to eql(
74+
[
75+
{ 'in' => 'formData', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false }
76+
]
77+
)
78+
end
79+
end
80+
end

0 commit comments

Comments
 (0)