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

use route_settings for hidden and operations extensions #596

Merged
merged 10 commits into from
May 10, 2017
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Features

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

#### Fixes
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ Or by adding ```hidden: true``` on the verb method of the endpoint, such as `get
get '/kittens', hidden: true do
```

Or by using a route setting:

```ruby
route_setting :swagger, { hidden: true }
gem '/kittens' do
```

Endpoints can be conditionally hidden by providing a callable object such as a lambda which evaluates to the desired
state:

Expand Down Expand Up @@ -933,6 +940,20 @@ this would generate:
}
```

- `operation` extension, by setting via route settings::
```ruby
route_setting :x_operation, { some: 'stuff' }
```
this would generate:
```json
"/path":{
"get":{
"…":"…",
"x-some":"stuff"
}
}
```

- `path` extension, by setting via route settings:
```ruby
route_setting :x_path, { some: 'stuff' }
Expand Down
5 changes: 5 additions & 0 deletions lib/grape-swagger/doc_methods/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def add(path, definitions, route)
add_extension_to(path[method], extension(description)) if description && extended?(description, :x)

settings = route.settings
add_extensions_to_operation(settings, path, route) if settings && extended?(settings, :x_operation)
add_extensions_to_path(settings, path) if settings && extended?(settings, :x_path)
add_extensions_to_definition(settings, path, definitions) if settings && extended?(settings, :x_def)
end
Expand All @@ -19,6 +20,10 @@ def add_extensions_to_info(settings, info)
add_extension_to(info, extension(settings)) if extended?(settings, :x)
end

def add_extensions_to_operation(settings, path, route)
add_extension_to(path[route.request_method.downcase.to_sym], extension(settings, :x_operation))
end

def add_extensions_to_path(settings, path)
add_extension_to(path, extension(settings, :x_path))
end
Expand Down
3 changes: 2 additions & 1 deletion lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ def model_name(name)
end

def hidden?(route, options)
route_hidden = route.options[:hidden]
route_hidden = route.settings.try(:[], :swagger).try(:[], :hidden)
route_hidden = route.options[:hidden] if route.options.key?(:hidden)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am I right, the original behavior still available?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the original behavior is default.

return route_hidden unless route_hidden.is_a?(Proc)
options[:token_owner] ? route_hidden.call(send(options[:token_owner].to_sym)) : route_hidden.call
end
Expand Down
5 changes: 3 additions & 2 deletions spec/swagger_v2/api_swagger_v2_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ class ExtensionsApi < Grape::API
{ 'declared_params' => declared(params) }
end

route_setting :x_operation, some: 'stuff'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, makes the extensions more consistent, thanks


desc 'This returns something with extension on verb level',
params: Entities::UseResponse.documentation,
failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }],
x: { some: 'stuff' }
failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }]
params do
requires :id, type: Integer
end
Expand Down
3 changes: 2 additions & 1 deletion spec/swagger_v2/guarded_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def sample_auth(*scopes)
class GuardedMountedApi < Grape::API
resource_owner_valid = proc { |token_owner = nil| token_owner.nil? }

desc 'Show endpoint if authenticated', hidden: resource_owner_valid
desc 'Show endpoint if authenticated'
route_setting :swagger, hidden: resource_owner_valid
get '/auth' do
{ foo: 'bar' }
end
Expand Down
6 changes: 6 additions & 0 deletions spec/swagger_v2/hide_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class HideMountedApi < Grape::API
{ foo: 'bar' }
end

desc 'Hide this endpoint using route setting'
route_setting :swagger, hidden: true
get '/hide_as_well' do
{ foo: 'bar' }
end

desc 'Lazily show endpoint', hidden: -> { false }
get '/lazy' do
{ foo: 'bar' }
Expand Down