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
2 changes: 1 addition & 1 deletion grape-swagger.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.16.2'
s.add_runtime_dependency 'grape', '>= 0.19.1'
Copy link
Member

Choose a reason for hiding this comment

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

please revert it back


s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec}/*`.split("\n")
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 @@ -337,7 +337,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