Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#953](https://github.com/ruby-grape/grape-swagger/pull/953): Added `super_diff` - [@numbata](https://github.com/numbata).
* [#951](https://github.com/ruby-grape/grape-swagger/pull/951): Use `x-example` for non-body parameters - [@olivier-thatch](https://github.com/olivier-thatch).
* [#963](https://github.com/ruby-grape/grape-swagger/pull/963): Allow empty model definitions for swagger 2.0 - [@numbata](https://github.com/numbata).
* [#964](https://github.com/ruby-grape/grape-swagger/pull/964): Add support for array of primitive data types in responses - [@khiav223577](https://github.com/khiav223577).
* Your contribution here.

#### Fixes
Expand Down
26 changes: 21 additions & 5 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,32 @@ def build_memo_schema(memo, route, value, response_model, options)
end

def build_response_for_type_parameter(memo, _route, value, _options)
type, format = prepare_type_and_format(value)
schema = build_response_schema(value)

if memo[value[:code]].include?(:schema) && value.include?(:as)
memo[value[:code]][:schema][:properties].merge!(value[:as] => { type: type, format: format }.compact)
memo[value[:code]][:schema][:properties].merge!(schema)
elsif value.include?(:as)
memo[value[:code]][:schema] =
{ type: :object, properties: { value[:as] => { type: type, format: format }.compact } }
memo[value[:code]][:schema] = { type: :object, properties: schema }
else
memo[value[:code]][:schema] = { type: type }
memo[value[:code]][:schema] = schema
end
end

def build_response_schema(value)
return { value[:as] => build_response_schema(value.except(:as)) } if value.include?(:as)

if value[:type].is_a?(Array)
items = if value[:type].size == 1
build_response_schema({ **value, type: value[:type].first })
else
{ oneOf: value[:type].map { |type| build_response_schema({ **value, type: type }) } }
end

return { type: 'array', items: items }
end

type, format = prepare_type_and_format(value)
{ type: type, format: format }.compact
end

def prepare_type_and_format(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ class ResponseApiModelsAndPrimitiveTypes < Grape::API
{ model: Entities::UseResponse, as: :user_response },
{ type: 'String', as: :string_response },
{ type: 'Float', as: :float_response },
{ type: 'Hash', as: :hash_response }
{ type: 'Hash', as: :hash_response },
{ type: Array[Integer], as: :array_of_integer_response },
{ type: Array[String], as: :array_of_string_response },
{ type: Array[Float], as: :array_of_float_response },
{ type: Array[Hash], as: :array_of_hash_response },
{ type: Array[Array[Float]], as: :array_of_array_of_float_response },
{ type: Array[Integer, String], as: :array_of_integer_or_string_response }
],
failure: [
{ code: 400, message: 'NotFound', model: '' },
Expand Down Expand Up @@ -56,7 +62,52 @@ def app
'integer_response' => { 'type' => 'integer', 'format' => 'int32' },
'string_response' => { 'type' => 'string' },
'float_response' => { 'type' => 'number', 'format' => 'float' },
'hash_response' => { 'type' => 'object' }
'hash_response' => { 'type' => 'object' },
'array_of_integer_response' => {
'type' => 'array',
'items' => {
'type' => 'integer',
'format' => 'int32'
}
},
'array_of_string_response' => {
'type' => 'array',
'items' => {
'type' => 'string'
}
},
'array_of_float_response' => {
'type' => 'array',
'items' => {
'type' => 'number',
'format' => 'float'
}
},
'array_of_hash_response' => {
'type' => 'array',
'items' => {
'type' => 'object'
}
},
'array_of_array_of_float_response' => {
'type' => 'array',
'items' => {
'type' => 'array',
'items' => {
'type' => 'number',
'format' => 'float'
}
}
},
'array_of_integer_or_string_response' => {
'type' => 'array',
'items' => {
'oneOf' => [
{ 'type' => 'integer', 'format' => 'int32' },
{ 'type' => 'string' }
]
}
}
}
}
},
Expand Down