Skip to content

Commit 3b8cb13

Browse files
mattyrpeter scholz
authored and
peter scholz
committed
allow overriding tag definitions (#589)
* allow overriding tag definitions * add changelog, address rubocop issues * add second tag to customized tag spec to make spec clearer * bump travis
1 parent 86dba46 commit 3b8cb13

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

CHANGELOG.md

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

55
* [#583](https://github.com/ruby-grape/grape-swagger/pull/583): Issue #582: document file response - [@LeFnord](https://github.com/LeFnord).
66
* [#588](https://github.com/ruby-grape/grape-swagger/pull/588): Allow extension keys in Info object - [@mattyr](https://github.com/mattyr).
7+
* [#589](https://github.com/ruby-grape/grape-swagger/pull/589): Allow overriding tag definitions in Info object - [@mattyr](https://github.com/mattyr).
78

89
* Your contribution here.
910

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ end
202202
* [token_owner](#token_owner)
203203
* [security_definitions](#security_definitions)
204204
* [models](#models)
205+
* [tags](#tags)
205206
* [hide_documentation_path](#hide_documentation_path)
206207
* [info](#info)
207208

@@ -329,6 +330,18 @@ add_swagger_documentation \
329330
]
330331
```
331332
333+
<a name="tags" />
334+
### tags:
335+
A list of tags to document. By default tags are automatically generated
336+
for endpoints based on route names.
337+
338+
```ruby
339+
add_swagger_documentation \
340+
tags: [
341+
{ name: 'widgets', description: 'A description of widgets' }
342+
]
343+
```
344+
332345
<a name="hide_documentation_path" />
333346
#### hide_documentation_path: (default: `true`)
334347
```ruby

lib/grape-swagger/doc_methods.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def setup(options)
5252
)
5353

5454
paths, definitions = endpoint.path_and_definition_objects(combi_routes, options)
55-
tags = GrapeSwagger::DocMethods::TagNameDescription.build(paths)
55+
tags = tags_from(paths, options)
56+
5657
output[:tags] = tags unless tags.empty? || paths.blank?
5758
output[:paths] = paths unless paths.blank?
5859
output[:definitions] = definitions unless definitions.blank?
@@ -109,5 +110,17 @@ def class_variables_from(options)
109110
@@class_name = options[:class_name] || options[:mount_path].delete('/')
110111
@@hide_documentation_path = options[:hide_documentation_path]
111112
end
113+
114+
def tags_from(paths, options)
115+
tags = GrapeSwagger::DocMethods::TagNameDescription.build(paths)
116+
117+
if options[:tags]
118+
names = options[:tags].map { |t| t[:name] }
119+
tags.reject! { |t| names.include?(t[:name]) }
120+
tags += options[:tags]
121+
end
122+
123+
tags
124+
end
112125
end
113126
end

spec/swagger_v2/default_api_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,37 @@ def app
139139
expect(subject['x-logo']).to eql('http://logo.com/img.png')
140140
end
141141
end
142+
143+
context 'with tags' do
144+
def app
145+
Class.new(Grape::API) do
146+
format :json
147+
desc 'This gets something.'
148+
get '/something' do
149+
{ bla: 'something' }
150+
end
151+
get '/somethingelse' do
152+
{ bla: 'somethingelse' }
153+
end
154+
155+
add_swagger_documentation tags: [
156+
{ name: 'something', description: 'customized description' }
157+
]
158+
end
159+
end
160+
161+
subject do
162+
get '/swagger_doc'
163+
JSON.parse(last_response.body)
164+
end
165+
166+
it 'documents the customized tag' do
167+
expect(subject['tags']).to eql(
168+
[
169+
{ 'name' => 'somethingelse', 'description' => 'Operations about somethingelses' },
170+
{ 'name' => 'something', 'description' => 'customized description' }
171+
]
172+
)
173+
end
174+
end
142175
end

0 commit comments

Comments
 (0)