Skip to content

Commit 66d975b

Browse files
author
Tim Vandecasteele
committed
Merge branch 'pr/63'
2 parents f2717ff + fe7ccd5 commit 66d975b

File tree

4 files changed

+125
-8
lines changed

4 files changed

+125
-8
lines changed

CHANGELOG.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Fix: translate parameter `type` to String, enables using Mongoid fields as parameter definitions - [@dblock](https://github.com/dblock).
44
* Adding support for generating swagger responseClass and models from Grape Entities - [@calebwoods](https://github.com/calebwoods).
5+
* Adding hidden endpoints - [@arturoherrero](https://github.com/arturoherrero).
56
* Your Contribution Here
67

78
### 0.6.0 (June 19, 2013)

README.markdown

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ You can pass a hash with some configuration possibilities to ```add_swagger_docu
5353

5454
## Swagger Header Parameters
5555

56-
Swagger also supports the documentation of parameters passed in the header. Since grape's ```params[]``` doesn't return header parameters we can
57-
to specify header parameters seperately in a block after the description.
56+
Swagger also supports the documentation of parameters passed in the header. Since grape's ```params[]``` doesn't return header parameters we can specify header parameters seperately in a block after the description.
5857

5958
``` ruby
6059
desc "Return super-secret information", {
@@ -70,7 +69,18 @@ desc "Return super-secret information", {
7069
}
7170
}
7271
```
73-
### Grape Entities
72+
73+
## Hiding an endpoint
74+
75+
You can hide an endpoint by adding ```:hidden => true``` in the description of the endpoint:
76+
77+
``` ruby
78+
desc 'Hide this endpoint', {
79+
:hidden => true
80+
}
81+
```
82+
83+
## Grape Entities
7484

7585
Add the [grape-entity](https://github.com/agileanimal/grape-entity) gem to our Gemfile.
7686
Please refer to the [grape-entity documentation](https://github.com/gileanimal/grape-entity/blob/master/README.markdown)

lib/grape-swagger.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ def self.setup(options)
6565
routes.reject!{ |route, value| "/#{route}/".index(parse_path(@@mount_path, nil) << '/') == 0 }
6666
end
6767

68-
routes_array = routes.keys.map do |local_route|
69-
{ :path => "#{parse_path(route.route_path.gsub('(.:format)', ''),route.route_version)}/#{local_route}#{@@hide_format ? '' : '.{format}'}" }
70-
end
68+
routes_array = routes.keys.map { |local_route|
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}'}" }
71+
}.compact
7172

7273
{
7374
apiVersion: api_version,
@@ -87,7 +88,8 @@ def self.setup(options)
8788
header['Access-Control-Request-Method'] = '*'
8889
models = []
8990
routes = @@target_class::combined_routes[params[:name]]
90-
routes_array = routes.map do |route|
91+
routes_array = routes.map { |route|
92+
next if route.route_hidden
9193
notes = route.route_notes && @@markdown ? Kramdown::Document.new(strip_heredoc(route.route_notes)).to_html : route.route_notes
9294
http_codes = parse_http_codes route.route_http_codes
9395
models << route.route_entity if route.route_entity
@@ -105,7 +107,7 @@ def self.setup(options)
105107
:path => parse_path(route.route_path, api_version),
106108
:operations => [operations]
107109
}
108-
end
110+
}.compact
109111

110112
api_description = {
111113
apiVersion: api_version,

spec/hide_api_spec.rb

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
require 'spec_helper'
2+
3+
describe "a hide mounted api" do
4+
before :all do
5+
class HideMountedApi < Grape::API
6+
desc 'Show this endpoint'
7+
get '/simple' do
8+
{ :foo => 'bar' }
9+
end
10+
11+
desc 'Hide this endpoint', {
12+
:hidden => true
13+
}
14+
get '/hide' do
15+
{ :foo => 'bar' }
16+
end
17+
end
18+
19+
class HideApi < Grape::API
20+
mount HideMountedApi
21+
add_swagger_documentation
22+
end
23+
end
24+
25+
def app; HideApi end
26+
27+
it "retrieves swagger-documentation that doesn't include hidden endpoints" do
28+
get '/swagger_doc.json'
29+
JSON.parse(last_response.body).should == {
30+
"apiVersion" => "0.1",
31+
"swaggerVersion" => "1.1",
32+
"basePath" => "http://example.org",
33+
"operations" => [],
34+
"apis" => [
35+
{ "path" => "/swagger_doc/simple.{format}" },
36+
{ "path" => "/swagger_doc/swagger_doc.{format}" }
37+
]
38+
}
39+
end
40+
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 swagger-documentation on /swagger_doc" 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 endpoints" 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)