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

support extensions on the root object #608

Merged
merged 6 commits into from
May 10, 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
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -899,11 +899,29 @@ end
#### Extensions <a name="extensions" />

Swagger spec2.0 supports extensions on different levels, for the moment,
the documentation on `info`, `verb`, `path` and `definition` level would be supported.
the documentation on the root level object and the `info`, `verb`, `path` and `definition` levels are 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:

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

- `info` extension, add a `x` key to the `info` hash when calling ```add_swagger_documentation```:
```ruby
add_swagger_documentation \
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 @@ -15,6 +15,10 @@ def add(path, definitions, route)
add_extensions_to_definition(settings, path, definitions) if settings && extended?(settings, :x_def)
end

def add_extensions_to_root(settings, object)
add_extension_to(object, extension(settings)) if extended?(settings, :x)
end

def add_extensions_to_info(settings, info)
add_extension_to(info, extension(settings)) if extended?(settings, :x)
end
Expand Down
24 changes: 14 additions & 10 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@ def content_types_for(target_class)
#
# required keys for SwaggerObject
def swagger_object(target_class, request, options)
{
info: info_object(options[:info].merge(version: options[:doc_version])),
swagger: '2.0',
produces: content_types_for(target_class),
authorizations: options[:authorizations],
object = {
info: info_object(options[:info].merge(version: options[:doc_version])),
swagger: '2.0',
produces: content_types_for(target_class),
authorizations: options[:authorizations],
securityDefinitions: options[:security_definitions],
security: options[:security],
host: GrapeSwagger::DocMethods::OptionalObject.build(:host, options, request),
basePath: GrapeSwagger::DocMethods::OptionalObject.build(:base_path, options, request),
schemes: options[:schemes].is_a?(String) ? [options[:schemes]] : options[:schemes]
}.delete_if { |_, value| value.blank? }
security: options[:security],
host: GrapeSwagger::DocMethods::OptionalObject.build(:host, options, request),
basePath: GrapeSwagger::DocMethods::OptionalObject.build(:base_path, options, request),
schemes: options[:schemes].is_a?(String) ? [options[:schemes]] : options[:schemes]
}

GrapeSwagger::DocMethods::Extensions.add_extensions_to_root(options, object)

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

# building info object
Expand Down