-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathparams_array_spec.rb
More file actions
293 lines (255 loc) · 10.6 KB
/
params_array_spec.rb
File metadata and controls
293 lines (255 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# frozen_string_literal: true
require 'spec_helper'
describe 'Group Params as Array' do
include_context "#{MODEL_PARSER} swagger example"
[true, false].each do |array_use_braces|
context "when array_use_braces option is set to #{array_use_braces}" do
let(:braces) { array_use_braces ? '[]' : '' }
let(:app) do
Class.new(Grape::API) do
format :json
desc 'groups' do
consumes ['application/x-www-form-urlencoded']
end
params do
requires :required_group, type: Array do
requires :required_param_1
requires :required_param_2
end
end
post '/groups' do
{ 'declared_params' => declared(params) }
end
desc 'type_given' do
consumes ['application/x-www-form-urlencoded']
end
params do
requires :typed_group, type: Array do
requires :id, type: Integer, desc: 'integer given'
requires :name, type: String, desc: 'string given'
optional :email, type: String, desc: 'email given'
optional :others, type: Integer, values: [1, 2, 3]
end
end
post '/type_given' do
{ 'declared_params' => declared(params) }
end
# as body parameters it would be interpreted a bit different,
# cause it could not be distinguished anymore, so this would be translated to one array,
# see also next example for the difference
desc 'array_of_type' do
consumes ['application/x-www-form-urlencoded']
end
params do
requires :array_of_string, type: Array[String], documentation: { param_type: 'body', desc: 'nested array of strings', example: %w[a b] }
requires :array_of_integer, type: Array[Integer], documentation: { param_type: 'body', desc: 'nested array of integers' }
end
post '/array_of_type' do
{ 'declared_params' => declared(params) }
end
desc 'object_and_array' do
consumes ['application/x-www-form-urlencoded']
end
params do
requires :array_of_string, type: Array[String], documentation: { param_type: 'body', desc: 'array of strings' }
requires :integer_value, type: Integer, documentation: { param_type: 'body', desc: 'integer value' }
end
post '/object_and_array' do
{ 'declared_params' => declared(params) }
end
desc 'array_of_type_in_form' do
consumes ['application/x-www-form-urlencoded']
end
params do
requires :array_of_string, type: Array[String]
requires :array_of_integer, type: Array[Integer]
end
post '/array_of_type_in_form' do
{ 'declared_params' => declared(params) }
end
desc 'array_of_entities' do
consumes ['application/x-www-form-urlencoded']
end
params do
requires :array_of_entities, type: Array[Entities::ApiError]
end
post '/array_of_entities' do
{ 'declared_params' => declared(params) }
end
helpers do
params :common_params do
requires :array_of_string, type: Array[String]
requires :array_of_integer, type: Array[Integer]
end
end
params do
use :common_params
end
get '/endpoint_with_common_params' do
{ 'declared_params' => declared(params) }
end
params do
use :common_params
end
post '/endpoint_with_common_params' do
{ 'declared_params' => declared(params) }
end
add_swagger_documentation array_use_braces: array_use_braces
end
end
describe 'retrieves the documentation for grouped parameters' do
subject do
get '/swagger_doc/groups'
JSON.parse(last_response.body)
end
specify do
expect(subject['paths']['/groups']['post']['parameters']).to eql(
[
{ 'in' => 'formData', 'name' => "required_group#{braces}[required_param_1]", 'required' => true, 'type' => 'array', 'items' => { 'type' => 'string' } },
{ 'in' => 'formData', 'name' => "required_group#{braces}[required_param_2]", 'required' => true, 'type' => 'array', 'items' => { 'type' => 'string' } }
]
)
end
end
describe 'retrieves the documentation for typed group parameters' do
subject do
get '/swagger_doc/type_given'
JSON.parse(last_response.body)
end
specify do
expect(subject['paths']['/type_given']['post']['parameters']).to eql(
[
{ 'in' => 'formData', 'name' => "typed_group#{braces}[id]", 'description' => 'integer given', 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32' }, 'required' => true },
{ 'in' => 'formData', 'name' => "typed_group#{braces}[name]", 'description' => 'string given', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true },
{ 'in' => 'formData', 'name' => "typed_group#{braces}[email]", 'description' => 'email given', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false },
{ 'in' => 'formData', 'name' => "typed_group#{braces}[others]", 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32', 'enum' => [1, 2, 3] }, 'required' => false }
]
)
end
end
describe 'retrieves the documentation for parameters that are arrays of primitive types' do
subject do
get '/swagger_doc/array_of_type'
JSON.parse(last_response.body)
end
specify do
expect(subject['definitions']['postArrayOfType']).to eql(
'type' => 'object',
'description' => 'array_of_type',
'properties' => {
'array_of_string' => {
'items' => { 'type' => 'string' }, 'type' => 'array', 'description' => 'nested array of strings', 'example' => %w[a b]
},
'array_of_integer' => {
'items' => { 'type' => 'integer', 'format' => 'int32' }, 'type' => 'array', 'description' => 'nested array of integers'
}
},
'required' => %w[array_of_string array_of_integer]
)
end
end
describe 'documentation for simple and array parameters' do
subject do
get '/swagger_doc/object_and_array'
JSON.parse(last_response.body)
end
specify do
expect(subject['definitions']['postObjectAndArray']['type']).to eql 'object'
expect(subject['definitions']['postObjectAndArray']['properties']).to eql(
'array_of_string' => {
'type' => 'array',
'description' => 'array of strings',
'items' => {
'type' => 'string'
}
},
'integer_value' => {
'type' => 'integer', 'format' => 'int32', 'description' => 'integer value'
}
)
end
end
describe 'retrieves the documentation for typed group parameters' do
subject do
get '/swagger_doc/array_of_type_in_form'
JSON.parse(last_response.body)
end
specify do
expect(subject['paths']['/array_of_type_in_form']['post']['parameters']).to eql(
[
{ 'in' => 'formData', 'name' => "array_of_string#{braces}", 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true },
{ 'in' => 'formData', 'name' => "array_of_integer#{braces}", 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32' }, 'required' => true }
]
)
end
end
describe 'documentation for entity array parameters' do
let(:parameters) do
[
{
'in' => 'formData',
'name' => "array_of_entities#{braces}",
'type' => 'array',
'items' => {
'$ref' => '#/definitions/ApiError'
},
'required' => true
}
]
end
subject do
get '/swagger_doc/array_of_entities'
JSON.parse(last_response.body)
end
specify do
expect(subject['definitions']['ApiError']).not_to be_blank
expect(subject['paths']['/array_of_entities']['post']['parameters']).to eql(parameters)
end
end
describe 'retrieves the documentation for parameters shared between GET and POST endpoints' do
subject do
get '/swagger_doc/endpoint_with_common_params'
JSON.parse(last_response.body)
end
describe 'for non-body parameters' do
specify do
expect(subject['paths']['/endpoint_with_common_params']['get']['parameters']).to eql(
[
{ 'in' => 'query', 'name' => "array_of_string#{braces}", 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true },
{ 'in' => 'query', 'name' => "array_of_integer#{braces}", 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32' }, 'required' => true }
]
)
end
end
describe 'for body parameters' do
specify do
expect(subject['paths']['/endpoint_with_common_params']['post']['parameters']).to eql(
[
{ 'in' => 'body', 'name' => 'postEndpointWithCommonParams', 'schema' => { '$ref' => '#/definitions/postEndpointWithCommonParams' }, 'required' => true }
]
)
expect(subject['definitions']['postEndpointWithCommonParams']).to eql(
'type' => 'object',
'properties' => {
'array_of_string' => { # no braces, even if array_use_braces is true
'type' => 'array',
'items' => {
'type' => 'string'
}
},
'array_of_integer' => { # no braces, even if array_use_braces is true
'type' => 'array',
'items' => {
'type' => 'integer',
'format' => 'int32'
}
}
},
'required' => %w[array_of_string array_of_integer]
)
end
end
end
end
end
end