Skip to content

Commit 7408607

Browse files
thogg4LeFnord
authored andcommitted
support extensions on the root object (ruby-grape#608)
* support extensions on the root object * remove puts statement * add spec for documentation * tweaks for rubocop * update changelog
1 parent 60afebd commit 7408607

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
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
* Your contribution here.
6+
* [#608](https://github.com/ruby-grape/grape-swagger/pull/608): Support extensions on the root object - [@thogg4](https://github.com/thogg4).
67
* [#596](https://github.com/ruby-grape/grape-swagger/pull/596): Use route_settings for hidden and operations extensions - [@thogg4](https://github.com/thogg4).
78
* [#607](https://github.com/ruby-grape/grape-swagger/pull/607): Allow body parameter name to be specified - [@tjwp](https://github.com/tjwp).
89

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,11 +906,29 @@ end
906906
#### Extensions <a name="extensions" />
907907

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

914+
- root object extension, add a `x` key to the root hash when calling ```add_swagger_documentation```:
915+
```ruby
916+
add_swagger_documentation \
917+
x: {
918+
some: 'stuff'
919+
},
920+
info: {
921+
}
922+
```
923+
this would generate:
924+
```json
925+
{
926+
"x-some": "stuff",
927+
"info":{
928+
}
929+
}
930+
```
931+
914932
- `info` extension, add a `x` key to the `info` hash when calling ```add_swagger_documentation```:
915933
```ruby
916934
add_swagger_documentation \

lib/grape-swagger/doc_methods/extensions.rb

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

19+
def add_extensions_to_root(settings, object)
20+
add_extension_to(object, extension(settings)) if extended?(settings, :x)
21+
end
22+
1923
def add_extensions_to_info(settings, info)
2024
add_extension_to(info, extension(settings)) if extended?(settings, :x)
2125
end

lib/grape-swagger/endpoint.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ def content_types_for(target_class)
2323
#
2424
# required keys for SwaggerObject
2525
def swagger_object(target_class, request, options)
26-
{
27-
info: info_object(options[:info].merge(version: options[:doc_version])),
28-
swagger: '2.0',
29-
produces: content_types_for(target_class),
30-
authorizations: options[:authorizations],
26+
object = {
27+
info: info_object(options[:info].merge(version: options[:doc_version])),
28+
swagger: '2.0',
29+
produces: content_types_for(target_class),
30+
authorizations: options[:authorizations],
3131
securityDefinitions: options[:security_definitions],
32-
security: options[:security],
33-
host: GrapeSwagger::DocMethods::OptionalObject.build(:host, options, request),
34-
basePath: GrapeSwagger::DocMethods::OptionalObject.build(:base_path, options, request),
35-
schemes: options[:schemes].is_a?(String) ? [options[:schemes]] : options[:schemes]
36-
}.delete_if { |_, value| value.blank? }
32+
security: options[:security],
33+
host: GrapeSwagger::DocMethods::OptionalObject.build(:host, options, request),
34+
basePath: GrapeSwagger::DocMethods::OptionalObject.build(:base_path, options, request),
35+
schemes: options[:schemes].is_a?(String) ? [options[:schemes]] : options[:schemes]
36+
}
37+
38+
GrapeSwagger::DocMethods::Extensions.add_extensions_to_root(options, object)
39+
object.delete_if { |_, value| value.blank? }
3740
end
3841

3942
# building info object

spec/swagger_v2/api_swagger_v2_extensions_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ExtensionsApi < Grape::API
5858
{ 'declared_params' => declared(params) }
5959
end
6060

61-
add_swagger_documentation
61+
add_swagger_documentation(x: { some: 'stuff' })
6262
end
6363
end
6464
end
@@ -67,6 +67,18 @@ def app
6767
TheApi::ExtensionsApi
6868
end
6969

70+
describe 'extension on root level' do
71+
subject do
72+
get '/swagger_doc/path_extension'
73+
JSON.parse(last_response.body)
74+
end
75+
76+
specify do
77+
expect(subject).to include 'x-some'
78+
expect(subject['x-some']).to eql 'stuff'
79+
end
80+
end
81+
7082
describe 'extension on path level' do
7183
subject do
7284
get '/swagger_doc/path_extension'

0 commit comments

Comments
 (0)