Skip to content

Commit 2161e51

Browse files
committed
add support for oneOf primitive types inside response arrays
1 parent 6e97674 commit 2161e51

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/grape-swagger/endpoint.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,12 @@ def build_response_schema(value)
326326
return { value[:as] => build_response_schema(value.except(:as)) } if value.include?(:as)
327327

328328
if value[:type].is_a?(Array)
329-
items = build_response_schema({ **value, type: value[:type].first })
329+
items = if value[:type].size == 1
330+
build_response_schema({ **value, type: value[:type].first })
331+
else
332+
{ oneOf: value[:type].map { |type| build_response_schema({ **value, type: type }) } }
333+
end
334+
330335
return { type: 'array', items: items }
331336
end
332337

spec/swagger_v2/api_swagger_v2_response_with_models_and_primitive_types_spec.rb

Lines changed: 11 additions & 1 deletion
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,6 +98,15 @@ def app
9798
'format' => 'float'
9899
}
99100
}
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+
}
100110
}
101111
}
102112
}

0 commit comments

Comments
 (0)