From 1cce60702d9ca553e6b682663f147b081b0fcf16 Mon Sep 17 00:00:00 2001 From: Grzegorz Jakubiak Date: Fri, 22 Nov 2024 15:55:17 +0100 Subject: [PATCH] Handle `Hash` type --- lib/grape-swagger/endpoint.rb | 17 +++++++++++++---- ...onse_with_models_and_primitive_types_spec.rb | 6 ++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index 58b54f58..15509be5 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -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] == '' @@ -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) @@ -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) diff --git a/spec/swagger_v2/api_swagger_v2_response_with_models_and_primitive_types_spec.rb b/spec/swagger_v2/api_swagger_v2_response_with_models_and_primitive_types_spec.rb index 7f0d9f9a..8050f1b6 100644 --- a/spec/swagger_v2/api_swagger_v2_response_with_models_and_primitive_types_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_response_with_models_and_primitive_types_spec.rb @@ -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: '' }, @@ -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' } } } },