lib/grape-swagger/endpoint.rb in grape-swagger-1.3.0 vs lib/grape-swagger/endpoint.rb in grape-swagger-1.3.1
- old
+ new
@@ -199,12 +199,11 @@
end
def response_object(route, options)
codes(route).each_with_object({}) do |value, memo|
value[:message] ||= ''
- memo[value[:code]] = { description: value[:message] }
-
+ memo[value[:code]] = { description: value[:message] ||= '' } unless memo[value[:code]].present?
memo[value[:code]][:headers] = value[:headers] if value[:headers]
next build_file_response(memo[value[:code]]) if file_response?(value[:model])
if memo.key?(200) && route.request_method == 'DELETE' && value[:model].nil?
@@ -219,11 +218,11 @@
response_model = value[:model] ? expose_params_from_model(value[:model]) : @item
next unless @definitions[response_model]
next if response_model.start_with?('Swagger_doc')
@definitions[response_model][:description] ||= "#{response_model} model"
- memo[value[:code]][:schema] = build_reference(route, value, response_model, options)
+ build_memo_schema(memo, route, value, response_model, options)
memo[value[:code]][:examples] = value[:examples] if value[:examples]
end
end
def codes(route)
@@ -266,19 +265,49 @@
).presence
end
private
+ 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))
+ elsif value[:as]
+ memo[value[:code]][:schema] = {
+ type: :object,
+ properties: build_reference(route, value, response_model, options)
+ }
+ else
+ memo[value[:code]][:schema] = build_reference(route, value, response_model, options)
+ end
+ end
+
def build_reference(route, value, response_model, settings)
# TODO: proof that the definition exist, if model isn't specified
- reference = { '$ref' => "#/definitions/#{response_model}" }
+ 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
- reference = { type: 'array', items: reference } if route.options[:is_array]
+ 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)
+ end
+
build_root(route, reference, response_model, settings)
end
+ def build_reference_hash(response_model)
+ { '$ref' => "#/definitions/#{response_model}" }
+ end
+
+ def build_reference_array(reference)
+ { type: 'array', items: reference }
+ end
+
def build_root(route, reference, response_model, settings)
default_root = response_model.underscore
default_root = default_root.pluralize if route.options[:is_array]
case route.settings.dig(:swagger, :root)
when true
@@ -380,9 +409,11 @@
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)
default_code[:examples] = entity[:examples] if entity[:examples]
default_code[:headers] = entity[:headers] if entity[:headers]
+ default_code[:as] = entity[:as] if entity[:as]
+ default_code[:is_array] = entity[:is_array] if entity[:is_array]
else
default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym]
default_code[:model] = entity if entity
default_code[:message] = route.description || default_code[:message].sub('{item}', @item)
end