Skip to content

Commit 2c757fa

Browse files
arturoherreroTim Vandecasteele
authored andcommitted
Hide endpoints with the same namespace
1 parent 57eb499 commit 2c757fa

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

lib/grape-swagger.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ def self.setup(options)
6666
end
6767

6868
routes_array = routes.keys.map { |local_route|
69-
next if routes[local_route].any? { |route| route.route_hidden }
70-
{ :path => "#{parse_path(route.route_path.gsub('(.:format)', ''),route.route_version)}/#{local_route}#{@@hide_format ? '' : '.{format}'}" }
69+
next if routes[local_route].all? { |route| route.route_hidden }
70+
{ :path => "#{parse_path(route.route_path.gsub('(.:format)', ''),route.route_version)}/#{local_route}#{@@hide_format ? '' : '.{format}'}" }
7171
}.compact
7272

7373
{
@@ -88,7 +88,8 @@ def self.setup(options)
8888
header['Access-Control-Request-Method'] = '*'
8989
models = []
9090
routes = @@target_class::combined_routes[params[:name]]
91-
routes_array = routes.map do |route|
91+
routes_array = routes.map { |route|
92+
next if route.route_hidden
9293
notes = route.route_notes && @@markdown ? Kramdown::Document.new(strip_heredoc(route.route_notes)).to_html : route.route_notes
9394
http_codes = parse_http_codes route.route_http_codes
9495
models << route.route_entity if route.route_entity
@@ -106,7 +107,7 @@ def self.setup(options)
106107
:path => parse_path(route.route_path, api_version),
107108
:operations => [operations]
108109
}
109-
end
110+
}.compact
110111

111112
api_description = {
112113
apiVersion: api_version,

spec/hide_api_spec.rb

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class HideApi < Grape::API
2424

2525
def app; HideApi end
2626

27-
it "retrieves swagger-documentation that doesn't include hidden endpoint" do
27+
it "retrieves swagger-documentation that doesn't include hidden endpoints" do
2828
get '/swagger_doc.json'
2929
JSON.parse(last_response.body).should == {
3030
"apiVersion" => "0.1",
@@ -38,3 +38,67 @@ def app; HideApi end
3838
}
3939
end
4040
end
41+
42+
43+
describe "a hide mounted api with same namespace" do
44+
before :all do
45+
class HideNamespaceMountedApi < Grape::API
46+
desc 'Show this endpoint'
47+
get '/simple/show' do
48+
{ :foo => 'bar' }
49+
end
50+
51+
desc 'Hide this endpoint', {
52+
:hidden => true
53+
}
54+
get '/simple/hide' do
55+
{ :foo => 'bar' }
56+
end
57+
end
58+
59+
class HideNamespaceApi < Grape::API
60+
mount HideNamespaceMountedApi
61+
add_swagger_documentation
62+
end
63+
end
64+
65+
def app; HideNamespaceApi end
66+
67+
it "retrieves the documentation for mounted-api" do
68+
get '/swagger_doc.json'
69+
JSON.parse(last_response.body).should == {
70+
"apiVersion" => "0.1",
71+
"swaggerVersion" => "1.1",
72+
"basePath" => "http://example.org",
73+
"operations" => [],
74+
"apis" => [
75+
{ "path" => "/swagger_doc/simple.{format}" },
76+
{ "path" => "/swagger_doc/swagger_doc.{format}" }
77+
]
78+
}
79+
end
80+
81+
it "retrieves the documentation for mounted-api that doesn't include hidden paths" do
82+
get '/swagger_doc/simple.json'
83+
JSON.parse(last_response.body).should == {
84+
"apiVersion" => "0.1",
85+
"swaggerVersion" => "1.1",
86+
"basePath" => "http://example.org",
87+
"resourcePath" => "",
88+
"apis" => [
89+
{
90+
"path" => "/simple/show.{format}",
91+
"operations" => [
92+
{
93+
"notes" => nil,
94+
"summary" => "Show this endpoint",
95+
"nickname" => "GET-simple-show---format-",
96+
"httpMethod" => "GET",
97+
"parameters" => []
98+
}
99+
]
100+
}
101+
]
102+
}
103+
end
104+
end

0 commit comments

Comments
 (0)