diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cc8cd3d..f680c149 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 4ab0992d..44a6c091 100644 --- a/README.md +++ b/README.md @@ -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', diff --git a/lib/grape-swagger/doc_methods/extensions.rb b/lib/grape-swagger/doc_methods/extensions.rb index 1762922f..ad1ac195 100644 --- a/lib/grape-swagger/doc_methods/extensions.rb +++ b/lib/grape-swagger/doc_methods/extensions.rb @@ -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 diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index 2dc21e0f..a5ce7cad 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -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 diff --git a/spec/swagger_v2/default_api_spec.rb b/spec/swagger_v2/default_api_spec.rb index 2e61bf0e..30213f6b 100644 --- a/spec/swagger_v2/default_api_spec.rb +++ b/spec/swagger_v2/default_api_spec.rb @@ -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 @@ -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