Skip to content

Commit 14364e3

Browse files
Jordan Brownpeter scholz
Jordan Brown
authored and
peter scholz
committed
Allow specifying custom tags at the route level (#523)
The current logic to determine a route's set of tags is based solely on the route, which works in most cases, but isn't flexible when you want to override the tags. The situation that led me here was the following setup: ``` prefix 'locations/:id' resource :employees do desc 'Retrieve employees for a given location' get '/' do ... end end ``` Which grouped the `/locations/:id/employees` under the `locations` namespace. This was undesirable, because almost all of the endpoints in our API are prefixed with `/locations/:id`, which meant that they were all grouped under the `locations` namespace. Allowing an additional `tags` attribute on the `desc` method solved the issue for me. ``` desc 'Retrieve employees for a given location', tags: ['employees'] ```
1 parent 7061fd1 commit 14364e3

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [#520](https://github.com/ruby-grape/grape-swagger/pull/520): Response model can have required attributes - [@WojciechKo](https://github.com/WojciechKo).
66
* [#510](https://github.com/ruby-grape/grape-swagger/pull/510): Use 'token_owner' instead of 'oauth_token' on Swagger UI endpoint authorization. - [@texpert](https://github.com/texpert).
7+
* [#523](https://github.com/ruby-grape/grape-swagger/pull/523): Allow specifying custom tags at the route level. - [@jordanfbrown](https://github.com/jordanfbrown).
78
* Your contribution here.
89

910
#### Fixes

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ add_swagger_documentation \
391391
* [Overriding Auto-Generated Nicknames](#overriding-auto-generated-nicknames)
392392
* [Specify endpoint details](#details)
393393
* [Overriding the route summary](#summary)
394+
* [Overriding the tags](#tags)
394395
* [Defining an endpoint as an array](#array)
395396
* [Using an options hash](#options)
396397
* [Overriding param type](#overriding-param-type)
@@ -481,6 +482,22 @@ end
481482
```
482483

483484

485+
<a name="tags" />
486+
#### Overriding the tags
487+
488+
Tags are used for logical grouping of operations by resources or any other qualifier. To override the
489+
tags array, add `tags: ['tag1', 'tag2']` after the description.
490+
491+
```ruby
492+
namespace 'order' do
493+
desc 'This will be your summary', tags: ['orders']
494+
get :order_id do
495+
...
496+
end
497+
end
498+
```
499+
500+
484501
<a name="array" />
485502
#### Defining an endpoint as an array
486503

lib/grape-swagger/endpoint.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def method_object(route, options, path)
112112
method[:parameters] = params_object(route)
113113
method[:security] = security_object(route)
114114
method[:responses] = response_object(route, options[:markdown])
115-
method[:tags] = tag_object(route)
115+
method[:tags] = route.options.fetch(:tags, tag_object(route))
116116
method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path)
117117
method.delete_if { |_, value| value.blank? }
118118

spec/swagger_v2/endpoint_versioned_path_spec.rb

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
require 'spec_helper'
22

33
describe 'Grape::Endpoint#path_and_definitions' do
4-
let(:api) do
5-
item = Class.new(Grape::API) do
4+
let(:item) do
5+
Class.new(Grape::API) do
66
version 'v1', using: :path
77

88
resource :item do
99
get '/'
1010
end
1111
end
12+
end
13+
14+
let(:api) do
15+
item_api = item
1216

1317
Class.new(Grape::API) do
14-
mount item
18+
mount item_api
1519
add_swagger_documentation add_version: true
1620
end
1721
end
@@ -28,4 +32,21 @@
2832
it 'tags the endpoint with the resource name' do
2933
expect(subject.first['/v1/item'][:get][:tags]).to eq ['item']
3034
end
35+
36+
context 'when custom tags are specified' do
37+
let(:item) do
38+
Class.new(Grape::API) do
39+
version 'v1', using: :path
40+
41+
resource :item do
42+
desc 'Item description', tags: ['special-item']
43+
get '/'
44+
end
45+
end
46+
end
47+
48+
it 'tags the endpoint with the custom tags' do
49+
expect(subject.first['/v1/item'][:get][:tags]).to eq ['special-item']
50+
end
51+
end
3152
end

0 commit comments

Comments
 (0)