Skip to content

Commit 6e97674

Browse files
committed
handle arrays of primitive data types in response
1 parent 4ee22a7 commit 6e97674

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

lib/grape-swagger/endpoint.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,15 @@ def build_response_for_type_parameter(memo, _route, value, _options)
323323
end
324324

325325
def build_response_schema(value)
326-
type, format = prepare_type_and_format(value)
326+
return { value[:as] => build_response_schema(value.except(:as)) } if value.include?(:as)
327327

328-
if value.include?(:as)
329-
return { value[:as] => { type: type, format: format }.compact }
330-
else
331-
return { type: type }
328+
if value[:type].is_a?(Array)
329+
items = build_response_schema({ **value, type: value[:type].first })
330+
return { type: 'array', items: items }
332331
end
332+
333+
type, format = prepare_type_and_format(value)
334+
{ type: type, format: format }.compact
333335
end
334336

335337
def prepare_type_and_format(value)

spec/swagger_v2/api_swagger_v2_response_with_models_and_primitive_types_spec.rb

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ class ResponseApiModelsAndPrimitiveTypes < Grape::API
1616
{ model: Entities::UseResponse, as: :user_response },
1717
{ type: 'String', as: :string_response },
1818
{ type: 'Float', as: :float_response },
19-
{ type: 'Hash', as: :hash_response }
19+
{ type: 'Hash', as: :hash_response },
20+
{ type: Array[Integer], as: :array_of_integer_response },
21+
{ type: Array[String], as: :array_of_string_response },
22+
{ type: Array[Float], as: :array_of_float_response },
23+
{ type: Array[Hash], as: :array_of_hash_response },
24+
{ type: Array[Array[Float]], as: :array_of_array_of_float_response }
2025
],
2126
failure: [
2227
{ code: 400, message: 'NotFound', model: '' },
@@ -56,7 +61,43 @@ def app
5661
'integer_response' => { 'type' => 'integer', 'format' => 'int32' },
5762
'string_response' => { 'type' => 'string' },
5863
'float_response' => { 'type' => 'number', 'format' => 'float' },
59-
'hash_response' => { 'type' => 'object' }
64+
'hash_response' => { 'type' => 'object' },
65+
'array_of_integer_response' => {
66+
'type' => 'array',
67+
'items' => {
68+
'type' => 'integer',
69+
'format' => 'int32'
70+
}
71+
},
72+
'array_of_string_response' => {
73+
'type' => 'array',
74+
'items' => {
75+
'type' => 'string'
76+
}
77+
},
78+
'array_of_float_response' => {
79+
'type' => 'array',
80+
'items' => {
81+
'type' => 'number',
82+
'format' => 'float'
83+
}
84+
},
85+
'array_of_hash_response' => {
86+
'type' => 'array',
87+
'items' => {
88+
'type' => 'object'
89+
}
90+
},
91+
'array_of_array_of_float_response' => {
92+
'type' => 'array',
93+
'items' => {
94+
'type' => 'array',
95+
'items' => {
96+
'type' => 'number',
97+
'format' => 'float'
98+
}
99+
}
100+
}
60101
}
61102
}
62103
},

0 commit comments

Comments
 (0)