Skip to content

Commit 4df92af

Browse files
idyllTim Vandecasteele
authored andcommitted
added basic support for specifying parameters that need to be passed in the header
1 parent f08e6b9 commit 4df92af

File tree

4 files changed

+87
-15
lines changed

4 files changed

+87
-15
lines changed

README.markdown

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,28 @@ You can pass a hash with some configuration possibilities to ```add_swagger_docu
4242
* ```:base_path``` Basepath of the API that's being exposed
4343
* ```:markdown``` Allow markdown in `notes`, default `false`
4444

45+
## Swagger Header Parameters
46+
47+
Swagger also supports the documentation of parameters passed in the header. Since grape's ```params[]``` doesn't return header parameters we can
48+
to specify header parameters seperately in a block after the description.
49+
50+
``` ruby
51+
desc "Return super-secret information", {
52+
headers: [
53+
{
54+
name: "XAuthToken",
55+
description: "Valdates your identity",
56+
required: true
57+
},
58+
{
59+
name: "XOptionalHeader",
60+
description: "Not reallly needed",
61+
required: false
62+
}
63+
]
64+
}
65+
```
66+
4567
## Swagger additions
4668
grape-swagger allows you to add an explanation in markdown in the notes field. Which would result in proper formatted markdown in Swagger UI. The default Swagger UI doesn't allow HTML in the notes field, so you need to use an adapted version of Swagger UI (you can find one at https://github.com/tim-vandecasteele/swagger-ui/tree/vasco).
4769

lib/grape-swagger.rb

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def self.setup(options)
8787
:summary => route.route_description || '',
8888
:nickname => route.route_method + route.route_path.gsub(/[\/:\(\)\.]/,'-'),
8989
:httpMethod => route.route_method,
90-
:parameters => parse_params(route.route_params, route.route_path, route.route_method)
90+
:parameters => parse_header_params(route.route_headers) +
91+
parse_params(route.route_params, route.route_path, route.route_method)
9192
}]
9293
}
9394
end
@@ -105,19 +106,46 @@ def self.setup(options)
105106

106107
helpers do
107108
def parse_params(params, path, method)
108-
params.map do |param, value|
109-
value[:type] = 'file' if value.is_a?(Hash) && value[:type] == 'Rack::Multipart::UploadedFile'
110-
dataType = value.is_a?(Hash) ? value[:type]||'String' : 'String'
111-
description = value.is_a?(Hash) ? value[:desc] : ''
112-
required = value.is_a?(Hash) ? !!value[:required] : false
113-
paramType = path.match(":#{param}") ? 'path' : (method == 'POST') ? 'body' : 'query'
114-
{
115-
paramType: paramType,
116-
name: value[:full_name] || param,
117-
description: description,
118-
dataType: dataType,
119-
required: required
120-
}
109+
if params
110+
params.map do |param, value|
111+
value[:type] = 'file' if value.is_a?(Hash) && value[:type] == 'Rack::Multipart::UploadedFile'
112+
113+
dataType = value.is_a?(Hash) ? value[:type]||'String' : 'String'
114+
description = value.is_a?(Hash) ? value[:desc] : ''
115+
required = value.is_a?(Hash) ? !!value[:required] : false
116+
paramType = path.match(":#{param}") ? 'path' : (method == 'POST') ? 'body' : 'query'
117+
{
118+
paramType: paramType,
119+
name: value[:full_name] || param,
120+
description: description,
121+
dataType: dataType,
122+
required: required
123+
}
124+
end
125+
else
126+
[]
127+
end
128+
end
129+
130+
131+
def parse_header_params(params)
132+
if params
133+
params.map do |details|
134+
name = details[:name]
135+
dataType = 'String'
136+
description = details[:description]
137+
required = details[:required]
138+
paramType = "header"
139+
{
140+
paramType: paramType,
141+
name: name,
142+
description: description,
143+
dataType: dataType,
144+
required: required
145+
}
146+
end
147+
else
148+
[]
121149
end
122150
end
123151

spec/grape-swagger-helper_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,14 @@ class HelperTestAPI < Grape::API
5252
end
5353
end
5454

55+
context "parsing header parameters" do
56+
it "should parse params for the header" do
57+
params = [{name: "XAuthToken", description: "A required header.", required: true}]
58+
@api.parse_header_params(params).should ==
59+
[
60+
{paramType: "header", name: "XAuthToken", description:"A required header.", dataType: "String", required: true}
61+
]
62+
end
63+
end
64+
5565
end

spec/simple_mounted_api_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ class SimpleMountedApi < Grape::API
99
get '/simple' do
1010
{:bla => 'something'}
1111
end
12+
13+
desc 'this gets something else', {
14+
:headers => [{name: "XAuthToken", description: "A required header.", required: true}]
15+
}
16+
get '/simple_with_headers' do
17+
{:bla => 'something_else'}
18+
end
1219
end
1320

1421
class SimpleApi < Grape::API
@@ -21,11 +28,16 @@ def app; SimpleApi end
2128

2229
it "retrieves swagger-documentation on /swagger_doc" do
2330
get '/swagger_doc'
24-
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}\"}]}"
31+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/simple.{format}\"}, {:path=>\"/swagger_doc/simple_with_headers.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
2532
end
2633

2734
it "retrieves the documentation for mounted-api" do
2835
get '/swagger_doc/simple'
2936
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/simple.{format}\", :operations=>[{:notes=>\"_test_\", :summary=>\"this gets something\", :nickname=>\"GET-simple---format-\", :httpMethod=>\"GET\", :parameters=>[]}]}]}"
3037
end
38+
39+
it "retrieves the documentation for mounted-api that includes headers" do
40+
get '/swagger_doc/simple_with_headers'
41+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/simple_with_headers.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something else\", :nickname=>\"GET-simple_with_headers---format-\", :httpMethod=>\"GET\", :parameters=>[{:paramType=>\"header\", :name=>\"XAuthToken\", :description=>\"A required header.\", :dataType=>\"String\", :required=>true}]}]}]}"
42+
end
3143
end

0 commit comments

Comments
 (0)