Skip to content

Commit

Permalink
Allow specifying custom tags at the route level
Browse files Browse the repository at this point in the history
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']
```
  • Loading branch information
jordanfbrown committed Oct 20, 2016
1 parent 7061fd1 commit 9340714
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

* [#520](https://github.com/ruby-grape/grape-swagger/pull/520): Response model can have required attributes - [@WojciechKo](https://github.com/WojciechKo).
* [#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).
* Your contribution here.
* [#523](https://github.com/ruby-grape/grape-swagger/pull/523): Allow specifying custom tags at the route level. - [@jordanfbrown](https://github.com/jordanfbrown).

#### Fixes

Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def method_object(route, options, path)
method[:parameters] = params_object(route)
method[:security] = security_object(route)
method[:responses] = response_object(route, options[:markdown])
method[:tags] = tag_object(route)
method[:tags] = route.options.fetch(:tags, tag_object(route))
method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path)
method.delete_if { |_, value| value.blank? }

Expand Down
27 changes: 24 additions & 3 deletions spec/swagger_v2/endpoint_versioned_path_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
require 'spec_helper'

describe 'Grape::Endpoint#path_and_definitions' do
let(:api) do
item = Class.new(Grape::API) do
let(:item) do
Class.new(Grape::API) do
version 'v1', using: :path

resource :item do
get '/'
end
end
end

let(:api) do
item_api = item

Class.new(Grape::API) do
mount item
mount item_api
add_swagger_documentation add_version: true
end
end
Expand All @@ -28,4 +32,21 @@
it 'tags the endpoint with the resource name' do
expect(subject.first['/v1/item'][:get][:tags]).to eq ['item']
end

context 'when custom tags are specified' do
let(:item) do
Class.new(Grape::API) do
version 'v1', using: :path

resource :item do
desc 'Item description', tags: ['special-item']
get '/'
end
end
end

it 'tags the endpoint with the custom tags' do
expect(subject.first['/v1/item'][:get][:tags]).to eq ['special-item']
end
end
end

0 comments on commit 9340714

Please sign in to comment.