lib/grape-swagger/endpoint.rb in grape-swagger-1.5.0 vs lib/grape-swagger/endpoint.rb in grape-swagger-1.6.0

- old
+ new

@@ -3,11 +3,11 @@ require 'active_support' require 'active_support/core_ext/string/inflections' require 'grape-swagger/endpoint/params_parser' module Grape - class Endpoint + class Endpoint # rubocop:disable Metrics/ClassLength def content_types_for(target_class) content_types = (target_class.content_types || {}).values if content_types.empty? formats = [target_class.format, target_class.default_format].compact.uniq @@ -25,11 +25,12 @@ # required keys for SwaggerObject def swagger_object(target_class, request, options) object = { info: info_object(options[:info].merge(version: options[:doc_version])), swagger: '2.0', - produces: content_types_for(target_class), + produces: options[:produces] || content_types_for(target_class), + consumes: options[:consumes], authorizations: options[:authorizations], securityDefinitions: options[:security_definitions], security: options[:security], host: GrapeSwagger::DocMethods::OptionalObject.build(:host, options, request), basePath: GrapeSwagger::DocMethods::OptionalObject.build(:base_path, options, request), @@ -115,11 +116,11 @@ def method_object(route, options, path) method = {} method[:summary] = summary_object(route) method[:description] = description_object(route) method[:produces] = produces_object(route, options[:produces] || options[:format]) - method[:consumes] = consumes_object(route, options[:format]) + method[:consumes] = consumes_object(route, options[:consumes] || options[:format]) method[:parameters] = params_object(route, options, path) method[:security] = security_object(route) method[:responses] = response_object(route, options) method[:tags] = route.options.fetch(:tags, tag_object(route, path)) method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path) @@ -238,11 +239,12 @@ def http_codes_from_route(route) if route.http_codes.is_a?(Array) && route.http_codes.any? { |code| success_code?(code) } route.http_codes.clone else - success_codes_from_route(route) + (route.http_codes || route.options[:failure] || []) + success_codes_from_route(route) + default_code_from_route(route) + + (route.http_codes || route.options[:failure] || []) end end def success_codes_from_route(route) if @entity.is_a?(Array) @@ -265,10 +267,25 @@ ).presence end private + def default_code_from_route(route) + entity = route.options[:default_response] + return [] if entity.nil? + + default_code = { code: 'default', message: 'Default Response' } + if entity.is_a?(Hash) + default_code[:message] = entity[:message] || default_code[:message] + default_code[:model] = entity[:model] if entity[:model].present? + else + default_code[:model] = entity + end + + [default_code] + end + def build_memo_schema(memo, route, value, response_model, options) if memo[value[:code]][:schema] && value[:as] memo[value[:code]][:schema][:properties].merge!(build_reference(route, value, response_model, options)) if value[:required] @@ -292,10 +309,10 @@ reference = if value.key?(:as) { value[:as] => build_reference_hash(response_model) } else build_reference_hash(response_model) end - return reference unless value[:code] < 300 + return reference unless value[:code] == 'default' || value[:code] < 300 if value.key?(:as) && value.key?(:is_array) reference[value[:as]] = build_reference_array(reference[value[:as]]) elsif route.options[:is_array] reference = build_reference_array(reference)