Skip to content

Commit

Permalink
Handle Hash type
Browse files Browse the repository at this point in the history
  • Loading branch information
gregg-platogo committed Nov 22, 2024
1 parent e7a8ace commit 1cce607
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 13 additions & 4 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def response_object(route, options)
next build_file_response(memo[value[:code]]) if file_response?(value[:model])

next build_delete_response(memo, value) if delete_response?(memo, route, value)
next build_primitive_response(memo, route, value, options) if value[:type]
next build_response_for_type_parameter(memo, route, value, options) if value[:type]

# Explicitly request no model with { model: '' }
next if value[:model] == ''
Expand Down Expand Up @@ -310,9 +310,8 @@ def build_memo_schema(memo, route, value, response_model, options)
end
end

def build_primitive_response(memo, _route, value, _options)
data_type = GrapeSwagger::DocMethods::DataType.call(value[:type])
type, format = GrapeSwagger::DocMethods::DataType.mapping(data_type)
def build_response_for_type_parameter(memo, _route, value, _options)
type, format = prepare_type_and_format(value)

if memo[value[:code]].include?(:schema) && value.include?(:as)
memo[value[:code]][:schema][:properties].merge!(value[:as] => { type: type, format: format }.compact)
Expand All @@ -324,6 +323,16 @@ def build_primitive_response(memo, _route, value, _options)
end
end

def prepare_type_and_format(value)
data_type = GrapeSwagger::DocMethods::DataType.call(value[:type])

if GrapeSwagger::DocMethods::DataType.primitive?(data_type)
GrapeSwagger::DocMethods::DataType.mapping(data_type)
else
data_type
end
end

def build_reference(route, value, response_model, settings)
# TODO: proof that the definition exist, if model isn't specified
reference = if value.key?(:as)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class ResponseApiModelsAndPrimitiveTypes < Grape::API
{ type: 'Integer', as: :integer_response },
{ model: Entities::UseResponse, as: :user_response },
{ type: 'String', as: :string_response },
{ type: 'Float', as: :float_response }
{ type: 'Float', as: :float_response },
{ type: 'Hash', as: :hash_response }
],
failure: [
{ code: 400, message: 'NotFound', model: '' },
Expand Down Expand Up @@ -54,7 +55,8 @@ def app
'user_response' => { '$ref' => '#/definitions/UseResponse' },
'integer_response' => { 'type' => 'integer', 'format' => 'int32' },
'string_response' => { 'type' => 'string' },
'float_response' => { 'type' => 'number', 'format' => 'float' }
'float_response' => { 'type' => 'number', 'format' => 'float' },
'hash_response' => { 'type' => 'object' }
}
}
},
Expand Down

0 comments on commit 1cce607

Please sign in to comment.