Skip to content

Add ignore_defaults option #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Features

* [#486](https://github.com/ruby-grape/grape/pull/486): Use an automated PR linter, [danger.systems](http://danger.systems) - [@dblock](https://github.com/dblock).
* [#491](https://github.com/ruby-grape/grape/pull/491): Add `ignore_defaults` option - [@pezholio](https://github.com/pezholio).

* Your contribution here.

Expand Down
19 changes: 10 additions & 9 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,9 @@ def params_object(route)
end

def response_object(route, markdown)
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)
codes = (route.http_codes || route.options[:failure] || [])
codes = apply_defaults(route, codes) if route.options[:ignore_defaults].nil?

codes = [default_code] + (route.http_codes || route.options[:failure] || [])
codes.map! { |x| x.is_a?(Array) ? { code: x[0], message: x[1], model: x[2] } : x }

codes.each_with_object({}) do |value, memo|
Expand All @@ -192,14 +190,17 @@ def response_object(route, markdown)

@definitions[response_model][:description] = description_object(route, markdown)
# TODO: proof that the definition exist, if model isn't specified
memo[value[:code]][:schema] = if route.options[:is_array]
{ 'type' => 'array', 'items' => { '$ref' => "#/definitions/#{response_model}" } }
else
{ '$ref' => "#/definitions/#{response_model}" }
end
memo[value[:code]][:schema] = route.options[:is_array] ? { 'type' => 'array', 'items' => { '$ref' => "#/definitions/#{response_model}" } } : { '$ref' => "#/definitions/#{response_model}" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a rubocop recommendation? … it is really a very long line … can you change it back?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't see any other way to cut down the number of lines. A bit of a cheat I know. Happy to change it back, but Rubocop will complain, and I'm not sure there's any way to add the functionality without increasing the number of lines :/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeap, it is known, change it back and increase the number, please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool - done! 😄

end
end

def apply_defaults(route, codes)
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)
[default_code] + codes
end

def tag_object(route)
Array(route.path.split('{')[0].split('/').reject(&:empty?).delete_if { |i| ((i == route.prefix.to_s) || (i == route.version)) }.first)
end
Expand Down
45 changes: 45 additions & 0 deletions spec/swagger_v2/api_swagger_v2_ignore_defaults_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'spec_helper'

describe 'swagger spec v2.0' do
include_context "#{MODEL_PARSER} swagger example"

def app
Class.new(Grape::API) do
format :json

desc 'This creates Thing after a delay',
success: Entities::Something
params do
requires :text, type: String, documentation: { type: 'string', desc: 'Content of something.' }
requires :links, type: Array, documentation: { type: 'link', is_array: true }
end
post '/delay_thing', http_codes: [{ code: 202, message: 'OK' }], ignore_defaults: true do
status 202
end

version 'v3', using: :path
add_swagger_documentation api_version: 'v1',
base_path: '/api',
info: {
title: 'The API title to be displayed on the API homepage.',
description: 'A description of the API.',
contact_name: 'Contact name',
contact_email: '[email protected]',
contact_url: 'Contact URL',
license: 'The name of the license.',
license_url: 'www.The-URL-of-the-license.org',
terms_of_service_url: 'www.The-URL-of-the-terms-and-service.com'
}
end
end

before do
get '/v3/swagger_doc'
end

let(:json) { JSON.parse(last_response.body) }

it 'only returns one response if ignore_defaults is specified' do
expect(json['paths']['/delay_thing']['post']['responses']).to eq('202' => { 'description' => 'OK' })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add also an expectation, which proofs that the default wasn't created:

expect(json['paths']['/delay_thing']['post']['responses'].keys).not_to include '201'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No probs 👍

end
end