Skip to content

Commit c7170da

Browse files
blakenumbata
authored andcommitted
Fix array parameter type (not custom types for now)
1 parent c266c5b commit c7170da

File tree

3 files changed

+82
-41
lines changed

3 files changed

+82
-41
lines changed

lib/grape-swagger/openapi_3/doc_methods/parse_params.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def document_array_param(value_type, definitions)
7676
if definitions[value_type[:data_type]]
7777
array_items['$ref'] = "#/definitions/#{@parsed_param[:type]}"
7878
else
79-
array_items[:type] = type || @parsed_param[:type] == 'array' ? 'string' : @parsed_param[:type]
79+
array_items[:type] = type || @parsed_param[:schema][:type] == 'array' ? 'string' : @parsed_param[:schema][:type]
8080
end
8181
array_items[:format] = @parsed_param.delete(:format) if @parsed_param[:format]
8282

@@ -88,7 +88,7 @@ def document_array_param(value_type, definitions)
8888

8989
@parsed_param[:in] = param_type || 'formData'
9090
@parsed_param[:items] = array_items
91-
@parsed_param[:type] = 'array'
91+
@parsed_param[:schema][:type] = 'array'
9292
@parsed_param[:collectionFormat] = collection_format if DataType.collections.include?(collection_format)
9393
end
9494

lib/grape-swagger/openapi_3/endpoint.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,10 @@ def response_body_object(_, _, parameters)
206206
parameters = {
207207
'content' => parameters.group_by { |p| p[:in] }.map do |_k, v|
208208
properties = v.map { |value| [value[:name], value.except(:name, :in, :required, :schema).merge(value[:schema])] }.to_h
209-
required_values = v.select { |param| param[:required] }
210-
[
211-
'application/x-www-form-urlencoded',
212-
{ 'schema' => {
213-
'type' => 'object',
214-
'required' => required_values.map { |required| required[:name] },
215-
'properties' => properties
216-
} }
217-
]
209+
required_values = v.select { |param| param[:required] }.map { |required| required[:name] }
210+
result = { 'schema' => { 'type' => 'object', 'properties' => properties } }
211+
result['required'] = required_values unless required_values.empty?
212+
['application/x-www-form-urlencoded', result]
218213
end.to_h
219214
}
220215

spec/openapi_3/simple_mounted_api_spec.rb

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -135,44 +135,75 @@ def app
135135
},
136136
'/simple-options-test' => {
137137
'options' => {
138-
'responses' => { '200' => { 'content' => { 'application/json' => {} }, 'description' => 'option SimpleOptionsTest' } },
138+
'responses' => {
139+
'200' => { 'content' => { 'application/json' => {} },
140+
'description' => 'option SimpleOptionsTest' }
141+
},
139142
'tags' => ['simple-options-test'],
140143
'operationId' => 'optionsSimpleOptionsTest'
141144
}
142145
},
143146
'/simple_with_headers' => {
144147
'get' => {
145148
'description' => 'this gets something else',
149+
'operationId' => 'getSimpleWithHeaders',
146150
'parameters' => [
147-
{ 'in' => 'header', 'name' => 'XAuthToken', 'description' => 'A required header.', 'type' => 'string', 'required' => true },
148-
{ 'in' => 'header', 'name' => 'XOtherHeader', 'description' => 'An optional header.', 'type' => 'string', 'required' => false }
151+
{ 'description' => 'A required header.',
152+
'in' => 'header',
153+
'name' => 'XAuthToken',
154+
'required' => true,
155+
'schema' => { 'type' => 'string' } },
156+
{
157+
'description' => 'An optional header.',
158+
'in' => 'header',
159+
'name' => 'XOtherHeader',
160+
'required' => false,
161+
'schema' => { 'type' => 'string' }
162+
}
149163
],
150-
'tags' => ['simple_with_headers'],
151-
'operationId' => 'getSimpleWithHeaders',
152-
'responses' => {
153-
'200' => { 'content' => { 'application/json' => {} }, 'description' => 'this gets something else' },
154-
'403' => { 'content' => { 'application/json' => {} }, 'description' => 'invalid pony' },
155-
'405' => { 'content' => { 'application/json' => {} }, 'description' => 'no ponies left!' }
156-
}
157-
}
158-
},
159-
'/items' => {
160-
'post' => {
161-
'description' => 'this takes an array of parameters',
162-
'consumes' => ['application/json'],
163-
'parameters' => [{ 'in' => 'formData', 'name' => 'items[]', 'description' => 'array of items', 'required' => false, 'type' => 'array', 'items' => { 'type' => 'string' } }],
164-
'tags' => ['items'],
165-
'operationId' => 'postItems',
166-
'responses' => { '201' => { 'description' => 'this takes an array of parameters' } }
164+
'responses' => { '200' => { 'content' => { 'application/json' => {} },
165+
'description' => 'this gets something else' },
166+
'403' => { 'content' => { 'application/json' => {} },
167+
'description' => 'invalid pony' },
168+
'405' => { 'content' => { 'application/json' => {} },
169+
'description' => 'no ponies left!' } },
170+
'tags' => ['simple_with_headers']
167171
}
168172
},
169173
'/custom' => {
170174
'get' => {
171175
'description' => 'this uses a custom parameter',
172-
'parameters' => [{ 'in' => 'formData', 'name' => 'custom', 'description' => 'array of items', 'required' => false, 'type' => 'array', 'items' => { 'type' => 'CustomType' } }],
173-
'tags' => ['custom'],
174176
'operationId' => 'getCustom',
175-
'responses' => { '200' => { 'content' => { 'application/json' => {} }, 'description' => 'this uses a custom parameter' } }
177+
'responses' => { '200' => {
178+
'content' => { 'application/json' => {} },
179+
'description' => 'this uses a custom parameter'
180+
} },
181+
'tags' => ['custom']
182+
}
183+
},
184+
'/items' => {
185+
'post' => {
186+
'description' => 'this takes an array of parameters',
187+
'operationId' => 'postItems',
188+
'requestBody' => {
189+
'content' => {
190+
'application/x-www-form-urlencoded' => {
191+
'schema' => {
192+
'properties' => {
193+
'items[]' => {
194+
'description' => 'array of items',
195+
'items' => { 'type' => 'string' },
196+
'type' => 'array'
197+
}
198+
}, 'type' => 'object'
199+
}
200+
}
201+
}
202+
},
203+
'responses' => {
204+
'201' => { 'description' => 'this takes an array of parameters' }
205+
},
206+
'tags' => ['items']
176207
}
177208
}
178209
}
@@ -255,8 +286,8 @@ def app
255286
'get' => {
256287
'description' => 'this gets something else',
257288
'parameters' => [
258-
{ 'in' => 'header', 'name' => 'XAuthToken', 'description' => 'A required header.', 'type' => 'string', 'required' => true },
259-
{ 'in' => 'header', 'name' => 'XOtherHeader', 'description' => 'An optional header.', 'type' => 'string', 'required' => false }
289+
{ 'in' => 'header', 'name' => 'XAuthToken', 'description' => 'A required header.', 'schema' => { 'type' => 'string' }, 'required' => true },
290+
{ 'in' => 'header', 'name' => 'XOtherHeader', 'description' => 'An optional header.', 'schema' => { 'type' => 'string' }, 'required' => false }
260291
],
261292
'tags' => ['simple_with_headers'],
262293
'operationId' => 'getSimpleWithHeaders',
@@ -282,8 +313,20 @@ def app
282313
'/items' => {
283314
'post' => {
284315
'description' => 'this takes an array of parameters',
285-
'consumes' => ['application/json'],
286-
'parameters' => [{ 'in' => 'formData', 'name' => 'items[]', 'description' => 'array of items', 'required' => false, 'type' => 'array', 'items' => { 'type' => 'string' } }],
316+
'requestBody' => {
317+
'content' => { 'application/x-www-form-urlencoded' => {
318+
'schema' => {
319+
'properties' => {
320+
'items[]' => {
321+
'description' => 'array of items',
322+
'items' => { 'type' => 'string' },
323+
'type' => 'array'
324+
}
325+
},
326+
'type' => 'object'
327+
}
328+
} }
329+
},
287330
'tags' => ['items'],
288331
'operationId' => 'postItems',
289332
'responses' => { '201' => { 'description' => 'this takes an array of parameters' } }
@@ -300,14 +343,17 @@ def app
300343
end
301344

302345
specify do
346+
fail("TODO: Fix")
303347
expect(subject['paths']).to eq(
304348
'/custom' => {
305349
'get' => {
306350
'description' => 'this uses a custom parameter',
307-
'parameters' => [{ 'in' => 'formData', 'name' => 'custom', 'description' => 'array of items', 'required' => false, 'type' => 'array', 'items' => { 'type' => 'CustomType' } }],
308-
'tags' => ['custom'],
309351
'operationId' => 'getCustom',
310-
'responses' => { '200' => { 'content' => { 'application/json' => {} }, 'description' => 'this uses a custom parameter' } }
352+
'responses' => {
353+
'200' => { 'content' => { 'application/json' => {} },
354+
'description' => 'this uses a custom parameter' }
355+
},
356+
'tags' => ['custom']
311357
}
312358
}
313359
)

0 commit comments

Comments
 (0)