Skip to content
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

Allow extension keys in Info Object #588

Merged
merged 4 commits into from
Mar 1, 2017
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -3,6 +3,7 @@
#### Features

* [#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).

* Your contribution here.

Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -858,11 +858,25 @@ end
#### Extensions

Swagger spec2.0 supports extensions on different levels, for the moment,
the documentation on `verb`, `path` and `definition` level would be supported.
the documentation on `info`, `verb`, `path` and `definition` level would be supported.
The documented key would be generated from the `x` + `-` + key of the submitted hash,
for possibilities refer to the [extensions spec](spec/lib/extensions_spec.rb).
To get an overview *how* the extensions would be defined on grape level, see the following examples:

- `info` extension, add a `x` key to the `info` hash when calling ```add_swagger_documentation```:
```ruby
add_swagger_documentation \
info: {
x: { some: 'stuff' }
}
```
this would generate:
```json
"info":{
"x-some":"stuff"
}
```

- `verb` extension, add a `x` key to the `desc` hash:
```ruby
desc 'This returns something with extension on verb level',
Expand Down
4 changes: 4 additions & 0 deletions lib/grape-swagger/doc_methods/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def add(path, definitions, route)
add_extensions_to_definition(settings, path, definitions) if settings && extended?(settings, :x_def)
end

def add_extensions_to_info(settings, info)
add_extension_to(info, extension(settings)) if extended?(settings, :x)
end

def add_extensions_to_path(settings, path)
add_extension_to(path, extension(settings, :x_path))
end
Expand Down
8 changes: 6 additions & 2 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ def swagger_object(target_class, request, options)

# building info object
def info_object(infos)
{
result = {
title: infos[:title] || 'API title',
description: infos[:description],
termsOfServiceUrl: infos[:terms_of_service_url],
contact: contact_object(infos),
license: license_object(infos),
version: infos[:version]
}.delete_if { |_, value| value.blank? }
}

GrapeSwagger::DocMethods::Extensions.add_extensions_to_info(infos, result)

result.delete_if { |_, value| value.blank? }
end

# sub-objects of info object
Expand Down
9 changes: 8 additions & 1 deletion spec/swagger_v2/default_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ def app
license: 'Apache 2',
license_url: 'http://test.com',
terms_of_service_url: 'http://terms.com',
contact_email: 'support@test.com'
contact_email: 'support@test.com',
x: {
logo: 'http://logo.com/img.png'
}
}
end
end
Expand Down Expand Up @@ -131,5 +134,9 @@ def app
it 'documents the contact email' do
expect(subject['contact']['email']).to eql('support@test.com')
end

it 'documents the extension field' do
expect(subject['x-logo']).to eql('http://logo.com/img.png')
end
end
end