Skip to content

Commit 45c0956

Browse files
pezholiopeter scholz
authored and
peter scholz
committed
Add ignore_defaults option (#491)
* Add `ignore_defaults` option * Update Changelog * Make Rubocop happy * Add seperate spec for `ignore_defaults` * Make tests more specific * Revert Rubocop-pleasing changes * Increase max line length
1 parent 64908af commit 45c0956

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Metrics/AbcSize:
2828
# Offense count: 3
2929
# Configuration parameters: CountComments.
3030
Metrics/ClassLength:
31-
Max: 222
31+
Max: 226
3232

3333
# Offense count: 10
3434
Metrics/CyclomaticComplexity:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

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

78
* Your contribution here.
89

lib/grape-swagger/endpoint.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,9 @@ def params_object(route)
168168
end
169169

170170
def response_object(route, markdown)
171-
default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym]
172-
default_code[:model] = @entity if @entity
173-
default_code[:message] = route.description || default_code[:message].sub('{item}', @item)
171+
codes = (route.http_codes || route.options[:failure] || [])
172+
codes = apply_defaults(route, codes) if route.options[:ignore_defaults].nil?
174173

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

178176
codes.each_with_object({}) do |value, memo|
@@ -200,6 +198,13 @@ def response_object(route, markdown)
200198
end
201199
end
202200

201+
def apply_defaults(route, codes)
202+
default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym]
203+
default_code[:model] = @entity if @entity
204+
default_code[:message] = route.description || default_code[:message].sub('{item}', @item)
205+
[default_code] + codes
206+
end
207+
203208
def tag_object(route)
204209
Array(route.path.split('{')[0].split('/').reject(&:empty?).delete_if { |i| ((i == route.prefix.to_s) || (i == route.version)) }.first)
205210
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'spec_helper'
2+
3+
describe 'swagger spec v2.0' do
4+
include_context "#{MODEL_PARSER} swagger example"
5+
6+
def app
7+
Class.new(Grape::API) do
8+
format :json
9+
10+
desc 'This creates Thing after a delay',
11+
success: Entities::Something
12+
params do
13+
requires :text, type: String, documentation: { type: 'string', desc: 'Content of something.' }
14+
requires :links, type: Array, documentation: { type: 'link', is_array: true }
15+
end
16+
post '/delay_thing', http_codes: [{ code: 202, message: 'OK' }], ignore_defaults: true do
17+
status 202
18+
end
19+
20+
version 'v3', using: :path
21+
add_swagger_documentation api_version: 'v1',
22+
base_path: '/api',
23+
info: {
24+
title: 'The API title to be displayed on the API homepage.',
25+
description: 'A description of the API.',
26+
contact_name: 'Contact name',
27+
contact_email: '[email protected]',
28+
contact_url: 'Contact URL',
29+
license: 'The name of the license.',
30+
license_url: 'www.The-URL-of-the-license.org',
31+
terms_of_service_url: 'www.The-URL-of-the-terms-and-service.com'
32+
}
33+
end
34+
end
35+
36+
before do
37+
get '/v3/swagger_doc'
38+
end
39+
40+
let(:json) { JSON.parse(last_response.body) }
41+
42+
it 'only returns one response if ignore_defaults is specified' do
43+
expect(json['paths']['/delay_thing']['post']['responses']).to eq('202' => { 'description' => 'OK' })
44+
expect(json['paths']['/delay_thing']['post']['responses'].keys).not_to include '201'
45+
end
46+
end

0 commit comments

Comments
 (0)