Skip to content

Commit 2d0de79

Browse files
committed
add support for oneOf primitive types inside response arrays
1 parent 2bd9f8e commit 2d0de79

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/grape-swagger/endpoint.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,12 @@ def build_response_schema(value)
328328
end
329329

330330
if value[:type].is_a?(Array)
331-
items = build_response_schema({ **value, type: value[:type].first })
331+
if value[:type].size == 1
332+
items = build_response_schema({ **value, type: value[:type].first })
333+
else
334+
items = { oneOf: value[:type].map { |type| build_response_schema({ **value, type: type }) } }
335+
end
336+
332337
return { type: 'array', items: items }
333338
end
334339

spec/swagger_v2/api_swagger_v2_response_with_models_and_primitive_types_spec.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class ResponseApiModelsAndPrimitiveTypes < Grape::API
2121
{ type: Array[String], as: :array_of_string_response },
2222
{ type: Array[Float], as: :array_of_float_response },
2323
{ type: Array[Hash], as: :array_of_hash_response },
24-
{ type: Array[Array[Float]], as: :array_of_array_of_float_response }
24+
{ type: Array[Array[Float]], as: :array_of_array_of_float_response },
25+
{ type: Array[Integer, String], as: :array_of_integer_or_string_response }
2526
],
2627
failure: [
2728
{ code: 400, message: 'NotFound', model: '' },
@@ -97,7 +98,16 @@ def app
9798
'format' => 'float'
9899
}
99100
}
100-
}
101+
},
102+
'array_of_integer_or_string_response' => {
103+
'type' => 'array',
104+
'items' => {
105+
'oneOf' => [
106+
{ 'type' => 'integer', 'format' => 'int32' },
107+
{ 'type' => 'string' }
108+
]
109+
}
110+
},
101111
}
102112
}
103113
},

0 commit comments

Comments
 (0)