From ea53d8c772ef367b8b4f7342172398c6279d0164 Mon Sep 17 00:00:00 2001 From: Tim Hogg Date: Wed, 8 Mar 2017 15:59:35 -0700 Subject: [PATCH 1/8] use route_settings for hidden and operations extensions --- grape-swagger.gemspec | 2 +- lib/grape-swagger/doc_methods/extensions.rb | 10 ++++++++++ lib/grape-swagger/endpoint.rb | 3 ++- spec/swagger_v2/api_swagger_v2_extensions_spec.rb | 5 +++-- spec/swagger_v2/guarded_endpoint_spec.rb | 3 ++- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/grape-swagger.gemspec b/grape-swagger.gemspec index 4f5f02ef..9f501299 100644 --- a/grape-swagger.gemspec +++ b/grape-swagger.gemspec @@ -12,7 +12,7 @@ Gem::Specification.new do |s| s.license = 'MIT' s.required_ruby_version = '>= 2.2.6' - s.add_runtime_dependency 'grape', '>= 0.16.2' + s.add_runtime_dependency 'grape', '>= 0.19.1' s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec}/*`.split("\n") diff --git a/lib/grape-swagger/doc_methods/extensions.rb b/lib/grape-swagger/doc_methods/extensions.rb index ad1ac195..3bc3e345 100644 --- a/lib/grape-swagger/doc_methods/extensions.rb +++ b/lib/grape-swagger/doc_methods/extensions.rb @@ -9,6 +9,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 @@ -17,6 +18,15 @@ 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) + p settings + p path + p route.request_method + p path[route.request_method.downcase.to_sym] + p extension(settings, :x_operation) + p 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 diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index a5ce7cad..96131a7e 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -335,7 +335,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) 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 diff --git a/spec/swagger_v2/api_swagger_v2_extensions_spec.rb b/spec/swagger_v2/api_swagger_v2_extensions_spec.rb index e483ba5d..b5535af2 100644 --- a/spec/swagger_v2/api_swagger_v2_extensions_spec.rb +++ b/spec/swagger_v2/api_swagger_v2_extensions_spec.rb @@ -17,10 +17,11 @@ class ExtensionsApi < Grape::API { 'declared_params' => declared(params) } end + route_setting :x_operation, some: 'stuff' + 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 diff --git a/spec/swagger_v2/guarded_endpoint_spec.rb b/spec/swagger_v2/guarded_endpoint_spec.rb index c18f6b8a..22ef9874 100644 --- a/spec/swagger_v2/guarded_endpoint_spec.rb +++ b/spec/swagger_v2/guarded_endpoint_spec.rb @@ -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 From 4ae1ece1474bae60acffdbd2ad7eec1f59a5466c Mon Sep 17 00:00:00 2001 From: Tim Hogg Date: Wed, 8 Mar 2017 16:15:02 -0700 Subject: [PATCH 2/8] remove puts statements --- lib/grape-swagger/doc_methods/extensions.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/grape-swagger/doc_methods/extensions.rb b/lib/grape-swagger/doc_methods/extensions.rb index 3bc3e345..63463cfe 100644 --- a/lib/grape-swagger/doc_methods/extensions.rb +++ b/lib/grape-swagger/doc_methods/extensions.rb @@ -19,12 +19,7 @@ def add_extensions_to_info(settings, info) end def add_extensions_to_operation(settings, path, route) - p settings - p path - p route.request_method - p path[route.request_method.downcase.to_sym] - p extension(settings, :x_operation) - p add_extension_to(path[route.request_method.downcase.to_sym], extension(settings, :x_operation)) + add_extension_to(path[route.request_method.downcase.to_sym], extension(settings, :x_operation)) end def add_extensions_to_path(settings, path) From 2a49465736c704f97ef4f586e7b2496d3d59494c Mon Sep 17 00:00:00 2001 From: Tim Hogg Date: Wed, 10 May 2017 10:11:00 -0600 Subject: [PATCH 3/8] revert grape version --- grape-swagger.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grape-swagger.gemspec b/grape-swagger.gemspec index 9cafacdf..3d1736c1 100644 --- a/grape-swagger.gemspec +++ b/grape-swagger.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.license = 'MIT' s.required_ruby_version = '>= 2.2.6' - s.add_runtime_dependency 'grape', '>= 0.19.1' + s.add_runtime_dependency 'grape', '>= 0.16.2' s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec}/*`.split("\n") From 6e96fd91485a6cc30882fbed6d1bdbcef5a54138 Mon Sep 17 00:00:00 2001 From: Tim Hogg Date: Wed, 10 May 2017 12:20:12 -0600 Subject: [PATCH 4/8] add some details to the readme --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index ab3ce9bc..c4b5ae92 100644 --- a/README.md +++ b/README.md @@ -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 :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: @@ -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' } From 2ea0dd0736d924b36357bdea55c796f1e5a0840d Mon Sep 17 00:00:00 2001 From: Tim Hogg Date: Wed, 10 May 2017 12:27:27 -0600 Subject: [PATCH 5/8] add route to demonstrate how to hide endpoint with route settings --- spec/swagger_v2/hide_api_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/swagger_v2/hide_api_spec.rb b/spec/swagger_v2/hide_api_spec.rb index 47e7681a..700d88a0 100644 --- a/spec/swagger_v2/hide_api_spec.rb +++ b/spec/swagger_v2/hide_api_spec.rb @@ -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' } From 2f533d46d076967718c4f74865128022769d3210 Mon Sep 17 00:00:00 2001 From: Tim Hogg Date: Wed, 10 May 2017 12:28:46 -0600 Subject: [PATCH 6/8] correct hide with route setting in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c4b5ae92..40aea719 100644 --- a/README.md +++ b/README.md @@ -461,7 +461,7 @@ get '/kittens', hidden: true do Or by using a route setting: ```ruby -route_setting :hidden, true +route_setting :swagger, { hidden: true } gem '/kittens' do ``` From 34f57cfc580be0a3105c89b600411332750f0634 Mon Sep 17 00:00:00 2001 From: Tim Hogg Date: Wed, 10 May 2017 12:30:07 -0600 Subject: [PATCH 7/8] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 029dc34c..d785b9e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 4c39b148190d5917d51167258af7b3bd28c42092 Mon Sep 17 00:00:00 2001 From: Tim Hogg Date: Wed, 10 May 2017 12:48:08 -0600 Subject: [PATCH 8/8] remove curly braces for rubocop --- spec/swagger_v2/hide_api_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/swagger_v2/hide_api_spec.rb b/spec/swagger_v2/hide_api_spec.rb index 700d88a0..c6104fb2 100644 --- a/spec/swagger_v2/hide_api_spec.rb +++ b/spec/swagger_v2/hide_api_spec.rb @@ -16,7 +16,7 @@ class HideMountedApi < Grape::API end desc 'Hide this endpoint using route setting' - route_setting :swagger, { hidden: true } + route_setting :swagger, hidden: true get '/hide_as_well' do { foo: 'bar' } end