Skip to content

allow overriding tag definitions #589

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 4 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -4,6 +4,7 @@

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

* Your contribution here.

Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ end
* [token_owner](#token_owner)
* [security_definitions](#security_definitions)
* [models](#models)
* [tags](#tags)
* [hide_documentation_path](#hide_documentation_path)
* [info](#info)

Expand Down Expand Up @@ -329,6 +330,18 @@ add_swagger_documentation \
]
```

<a name="tags" />
### tags:
A list of tags to document. By default tags are automatically generated
for endpoints based on route names.

```ruby
add_swagger_documentation \
tags: [
{ name: 'widgets', description: 'A description of widgets' }
]
```

<a name="hide_documentation_path" />
#### hide_documentation_path: (default: `true`)
```ruby
Expand Down
15 changes: 14 additions & 1 deletion lib/grape-swagger/doc_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def setup(options)
)

paths, definitions = endpoint.path_and_definition_objects(combi_routes, options)
tags = GrapeSwagger::DocMethods::TagNameDescription.build(paths)
tags = tags_from(paths, options)

output[:tags] = tags unless tags.empty? || paths.blank?
output[:paths] = paths unless paths.blank?
output[:definitions] = definitions unless definitions.blank?
Expand Down Expand Up @@ -109,5 +110,17 @@ def class_variables_from(options)
@@class_name = options[:class_name] || options[:mount_path].delete('/')
@@hide_documentation_path = options[:hide_documentation_path]
end

def tags_from(paths, options)
tags = GrapeSwagger::DocMethods::TagNameDescription.build(paths)

if options[:tags]
names = options[:tags].map { |t| t[:name] }
tags.reject! { |t| names.include?(t[:name]) }
tags += options[:tags]
end

tags
end
end
end
24 changes: 24 additions & 0 deletions spec/swagger_v2/default_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,28 @@ def app
expect(subject['x-logo']).to eql('http://logo.com/img.png')
end
end

context 'with tags' do
def app
Class.new(Grape::API) do
format :json
desc 'This gets something.'
get '/something' do
{ bla: 'something' }
end
add_swagger_documentation tags: [
{ name: 'something', description: 'customized description' }
]
Copy link
Member

@LeFnord LeFnord Mar 1, 2017

Choose a reason for hiding this comment

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

please can you add a second tag, so that it will be clear, that an existing tag can be overwritten and non-existing will be added, but only if I understand the tag description of the swagger-object right, please correct me, if I'm wrong, many thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you're right, pushing a revised spec

end
end

subject do
get '/swagger_doc'
JSON.parse(last_response.body)
end

it 'documents the customized tag' do
expect(subject['tags']).to eql([{ 'name' => 'something', 'description' => 'customized description' }])
end
end
end