Skip to content

Commit b33ff7c

Browse files
thogg4LeFnord
authored andcommitted
use route_settings for hidden and operations extensions (#596)
* use route_settings for hidden and operations extensions * remove puts statements * revert grape version * add some details to the readme * add route to demonstrate how to hide endpoint with route settings * correct hide with route setting in readme * add changelog entry * remove curly braces for rubocop
1 parent 3493e3c commit b33ff7c

File tree

7 files changed

+40
-4
lines changed

7 files changed

+40
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

55
* Your contribution here.
6+
* [#596](https://github.com/ruby-grape/grape-swagger/pull/596): Use route_settings for hidden and operations extensions - [@thogg4](https://github.com/thogg4).
67
* [#607](https://github.com/ruby-grape/grape-swagger/pull/607): Allow body parameter name to be specified - [@tjwp](https://github.com/tjwp).
78

89
#### Fixes

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,13 @@ Or by adding ```hidden: true``` on the verb method of the endpoint, such as `get
458458
get '/kittens', hidden: true do
459459
```
460460

461+
Or by using a route setting:
462+
463+
```ruby
464+
route_setting :swagger, { hidden: true }
465+
gem '/kittens' do
466+
```
467+
461468
Endpoints can be conditionally hidden by providing a callable object such as a lambda which evaluates to the desired
462469
state:
463470

@@ -933,6 +940,20 @@ this would generate:
933940
}
934941
```
935942

943+
- `operation` extension, by setting via route settings::
944+
```ruby
945+
route_setting :x_operation, { some: 'stuff' }
946+
```
947+
this would generate:
948+
```json
949+
"/path":{
950+
"get":{
951+
"…":"",
952+
"x-some":"stuff"
953+
}
954+
}
955+
```
956+
936957
- `path` extension, by setting via route settings:
937958
```ruby
938959
route_setting :x_path, { some: 'stuff' }

lib/grape-swagger/doc_methods/extensions.rb

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def add(path, definitions, route)
1111
add_extension_to(path[method], extension(description)) if description && extended?(description, :x)
1212

1313
settings = route.settings
14+
add_extensions_to_operation(settings, path, route) if settings && extended?(settings, :x_operation)
1415
add_extensions_to_path(settings, path) if settings && extended?(settings, :x_path)
1516
add_extensions_to_definition(settings, path, definitions) if settings && extended?(settings, :x_def)
1617
end
@@ -19,6 +20,10 @@ def add_extensions_to_info(settings, info)
1920
add_extension_to(info, extension(settings)) if extended?(settings, :x)
2021
end
2122

23+
def add_extensions_to_operation(settings, path, route)
24+
add_extension_to(path[route.request_method.downcase.to_sym], extension(settings, :x_operation))
25+
end
26+
2227
def add_extensions_to_path(settings, path)
2328
add_extension_to(path, extension(settings, :x_path))
2429
end

lib/grape-swagger/endpoint.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ def model_name(name)
338338
end
339339

340340
def hidden?(route, options)
341-
route_hidden = route.options[:hidden]
341+
route_hidden = route.settings.try(:[], :swagger).try(:[], :hidden)
342+
route_hidden = route.options[:hidden] if route.options.key?(:hidden)
342343
return route_hidden unless route_hidden.is_a?(Proc)
343344
options[:token_owner] ? route_hidden.call(send(options[:token_owner].to_sym)) : route_hidden.call
344345
end

spec/swagger_v2/api_swagger_v2_extensions_spec.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ class ExtensionsApi < Grape::API
1919
{ 'declared_params' => declared(params) }
2020
end
2121

22+
route_setting :x_operation, some: 'stuff'
23+
2224
desc 'This returns something with extension on verb level',
2325
params: Entities::UseResponse.documentation,
24-
failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }],
25-
x: { some: 'stuff' }
26+
failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }]
2627
params do
2728
requires :id, type: Integer
2829
end

spec/swagger_v2/guarded_endpoint_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def sample_auth(*scopes)
5656
class GuardedMountedApi < Grape::API
5757
resource_owner_valid = proc { |token_owner = nil| token_owner.nil? }
5858

59-
desc 'Show endpoint if authenticated', hidden: resource_owner_valid
59+
desc 'Show endpoint if authenticated'
60+
route_setting :swagger, hidden: resource_owner_valid
6061
get '/auth' do
6162
{ foo: 'bar' }
6263
end

spec/swagger_v2/hide_api_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ class HideMountedApi < Grape::API
1515
{ foo: 'bar' }
1616
end
1717

18+
desc 'Hide this endpoint using route setting'
19+
route_setting :swagger, hidden: true
20+
get '/hide_as_well' do
21+
{ foo: 'bar' }
22+
end
23+
1824
desc 'Lazily show endpoint', hidden: -> { false }
1925
get '/lazy' do
2026
{ foo: 'bar' }

0 commit comments

Comments
 (0)