Skip to content

Commit 86dba46

Browse files
mattyrpeter scholz
authored and
peter scholz
committed
Allow extension keys in Info Object (#588)
* Allow extension keys in Info Object, per http://swagger.io/specification/#infoObject * address comments - make rubocop happy - add changelog entry - add entry to extension section in the README * fix changelog entry * another changelog fix
1 parent 79052e3 commit 86dba46

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

CHANGELOG.md

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

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

78
* Your contribution here.
89

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,11 +858,25 @@ end
858858
#### Extensions
859859

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

866+
- `info` extension, add a `x` key to the `info` hash when calling ```add_swagger_documentation```:
867+
```ruby
868+
add_swagger_documentation \
869+
info: {
870+
x: { some: 'stuff' }
871+
}
872+
```
873+
this would generate:
874+
```json
875+
"info":{
876+
"x-some":"stuff"
877+
}
878+
```
879+
866880
- `verb` extension, add a `x` key to the `desc` hash:
867881
```ruby
868882
desc 'This returns something with extension on verb level',

lib/grape-swagger/doc_methods/extensions.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ def add(path, definitions, route)
1313
add_extensions_to_definition(settings, path, definitions) if settings && extended?(settings, :x_def)
1414
end
1515

16+
def add_extensions_to_info(settings, info)
17+
add_extension_to(info, extension(settings)) if extended?(settings, :x)
18+
end
19+
1620
def add_extensions_to_path(settings, path)
1721
add_extension_to(path, extension(settings, :x_path))
1822
end

lib/grape-swagger/endpoint.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ def swagger_object(target_class, request, options)
3535

3636
# building info object
3737
def info_object(infos)
38-
{
38+
result = {
3939
title: infos[:title] || 'API title',
4040
description: infos[:description],
4141
termsOfServiceUrl: infos[:terms_of_service_url],
4242
contact: contact_object(infos),
4343
license: license_object(infos),
4444
version: infos[:version]
45-
}.delete_if { |_, value| value.blank? }
45+
}
46+
47+
GrapeSwagger::DocMethods::Extensions.add_extensions_to_info(infos, result)
48+
49+
result.delete_if { |_, value| value.blank? }
4650
end
4751

4852
# sub-objects of info object

spec/swagger_v2/default_api_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ def app
9898
license: 'Apache 2',
9999
license_url: 'http://test.com',
100100
terms_of_service_url: 'http://terms.com',
101-
contact_email: '[email protected]'
101+
contact_email: '[email protected]',
102+
x: {
103+
logo: 'http://logo.com/img.png'
104+
}
102105
}
103106
end
104107
end
@@ -131,5 +134,9 @@ def app
131134
it 'documents the contact email' do
132135
expect(subject['contact']['email']).to eql('[email protected]')
133136
end
137+
138+
it 'documents the extension field' do
139+
expect(subject['x-logo']).to eql('http://logo.com/img.png')
140+
end
134141
end
135142
end

0 commit comments

Comments
 (0)