lib/grape-swagger/endpoint.rb in grape-swagger-0.27.3 vs lib/grape-swagger/endpoint.rb in grape-swagger-0.28.0
- old
+ new
@@ -75,11 +75,11 @@
# building path and definitions objects
def path_and_definition_objects(namespace_routes, options)
@paths = {}
@definitions = {}
- namespace_routes.keys.each do |key|
+ namespace_routes.each_key do |key|
routes = namespace_routes[key]
path_item(routes, options)
end
add_definitions_from options[:models]
@@ -141,10 +141,11 @@
end
def description_object(route)
description = route.description if route.description.present?
description = route.options[:detail] if route.options.key?(:detail)
+ description ||= ''
description
end
def produces_object(route, format)
@@ -159,18 +160,15 @@
end.flatten.compact.uniq
route_mime_types.present? ? route_mime_types : mime_types
end
- def consumes_object(route, format)
- method = route.request_method.downcase.to_sym
- if route.settings[:description] && route.settings[:description][:consumes]
- format = route.settings[:description][:consumes]
- end
- mime_types = GrapeSwagger::DocMethods::ProducesConsumes.call(format) if %i[post put].include?(method)
+ SUPPORTS_CONSUMES = %i[post put patch].freeze
- mime_types
+ def consumes_object(route, format)
+ return unless SUPPORTS_CONSUMES.include?(route.request_method.downcase.to_sym)
+ GrapeSwagger::DocMethods::ProducesConsumes.call(route.settings.dig(:description, :consumes) || format)
end
def params_object(route, path)
parameters = partition_params(route).map do |param, value|
value = { required: false }.merge(value) if value.is_a?(Hash)
@@ -189,16 +187,15 @@
parameters
end
def response_object(route)
- codes = (route.http_codes || route.options[:failure] || [])
-
- codes = apply_success_codes(route) + codes
+ codes = http_codes_from_route(route)
codes.map! { |x| x.is_a?(Array) ? { code: x[0], message: x[1], model: x[2] } : x }
codes.each_with_object({}) do |value, memo|
+ value[:message] ||= ''
memo[value[:code]] = { description: value[:message] }
next build_file_response(memo[value[:code]]) if file_response?(value[:model])
response_model = @item
response_model = expose_params_from_model(value[:model]) if value[:model]
@@ -220,11 +217,19 @@
reference
end
end
end
- def apply_success_codes(route)
+ def http_codes_from_route(route)
+ if route.http_codes.is_a?(Array) && route.http_codes.any? { |code| code[:code].between?(200, 299) }
+ route.http_codes.clone
+ else
+ success_codes_from_route(route) + (route.http_codes || route.options[:failure] || [])
+ end
+ end
+
+ def success_codes_from_route(route)
default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym]
if @entity.is_a?(Hash)
default_code[:code] = @entity[:code] if @entity[:code].present?
default_code[:model] = @entity[:model] if @entity[:model].present?
default_code[:message] = @entity[:message] || route.description || default_code[:message].sub('{item}', @item)
@@ -255,10 +260,11 @@
def build_file_response(memo)
memo['schema'] = { type: 'file' }
end
+ # rubocop:disable Style/IfUnlessModifier
def partition_params(route)
declared_params = route.settings[:declared_params] if route.settings[:declared_params].present?
required = merge_params(route)
required = GrapeSwagger::DocMethods::Headers.parse(route) + required unless route.headers.nil?
@@ -268,10 +274,11 @@
parse_request_params(required)
end || {}
request_params.empty? ? required : request_params
end
+ # rubocop:enable Style/IfUnlessModifier
def merge_params(route)
param_keys = route.params.keys
route.params.delete_if { |key| key.is_a?(String) && param_keys.include?(key.to_sym) }.to_a
end
@@ -324,10 +331,10 @@
parser = GrapeSwagger.model_parsers.find(model)
raise GrapeSwagger::Errors::UnregisteredParser, "No parser registered for #{model_name}." unless parser
properties = parser.new(model, self).call
- unless properties && properties.any?
+ unless properties&.any?
raise GrapeSwagger::Errors::SwaggerSpec,
"Empty model #{model_name}, swagger 2.0 doesn't support empty definitions."
end
@definitions[model_name] = GrapeSwagger::DocMethods::BuildModelDefinition.build(model, properties)