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

Hide endpoint from the swagger documentation. Resolves #24 #63

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ def self.setup(options)
routes.reject!{ |route, value| "/#{route}/".index(parse_path(@@mount_path, nil) << '/') == 0 }
end

routes_array = routes.keys.map do |local_route|
{ :path => "#{parse_path(route.route_path.gsub('(.:format)', ''),route.route_version)}/#{local_route}#{@@hide_format ? '' : '.{format}'}" }
end
routes_array = routes.keys.map { |local_route|
next if routes[local_route].all? { |route| route.route_hidden }
{ :path => "#{parse_path(route.route_path.gsub('(.:format)', ''),route.route_version)}/#{local_route}#{@@hide_format ? '' : '.{format}'}" }
}.compact

{
apiVersion: api_version,
Expand All @@ -85,7 +86,8 @@ def self.setup(options)
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
routes = @@target_class::combined_routes[params[:name]]
routes_array = routes.map do |route|
routes_array = routes.map { |route|
next if route.route_hidden
notes = route.route_notes && @@markdown ? Kramdown::Document.new(strip_heredoc(route.route_notes)).to_html : route.route_notes
http_codes = parse_http_codes route.route_http_codes
operations = {
Expand All @@ -101,7 +103,7 @@ def self.setup(options)
:path => parse_path(route.route_path, api_version),
:operations => [operations]
}
end
}.compact

{
apiVersion: api_version,
Expand Down
104 changes: 104 additions & 0 deletions spec/hide_api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
require 'spec_helper'

describe "a hide mounted api" do
before :all do
class HideMountedApi < Grape::API
desc 'Show this endpoint'
get '/simple' do
{ :foo => 'bar' }
end

desc 'Hide this endpoint', {
:hidden => true
}
get '/hide' do
{ :foo => 'bar' }
end
end

class HideApi < Grape::API
mount HideMountedApi
add_swagger_documentation
end
end

def app; HideApi end

it "retrieves swagger-documentation that doesn't include hidden endpoints" do
get '/swagger_doc.json'
JSON.parse(last_response.body).should == {
"apiVersion" => "0.1",
"swaggerVersion" => "1.1",
"basePath" => "http://example.org",
"operations" => [],
"apis" => [
{ "path" => "/swagger_doc/simple.{format}" },
{ "path" => "/swagger_doc/swagger_doc.{format}" }
]
}
end
end


describe "a hide mounted api with same namespace" do
before :all do
class HideNamespaceMountedApi < Grape::API
desc 'Show this endpoint'
get '/simple/show' do
{ :foo => 'bar' }
end

desc 'Hide this endpoint', {
:hidden => true
}
get '/simple/hide' do
{ :foo => 'bar' }
end
end

class HideNamespaceApi < Grape::API
mount HideNamespaceMountedApi
add_swagger_documentation
end
end

def app; HideNamespaceApi end

it "retrieves swagger-documentation on /swagger_doc" do
get '/swagger_doc.json'
JSON.parse(last_response.body).should == {
"apiVersion" => "0.1",
"swaggerVersion" => "1.1",
"basePath" => "http://example.org",
"operations" => [],
"apis" => [
{ "path" => "/swagger_doc/simple.{format}" },
{ "path" => "/swagger_doc/swagger_doc.{format}" }
]
}
end

it "retrieves the documentation for mounted-api that doesn't include hidden endpoints" do
get '/swagger_doc/simple.json'
JSON.parse(last_response.body).should == {
"apiVersion" => "0.1",
"swaggerVersion" => "1.1",
"basePath" => "http://example.org",
"resourcePath" => "",
"apis" => [
{
"path" => "/simple/show.{format}",
"operations" => [
{
"notes" => nil,
"summary" => "Show this endpoint",
"nickname" => "GET-simple-show---format-",
"httpMethod" => "GET",
"parameters" => []
}
]
}
]
}
end
end