Skip to content

Commit f2717ff

Browse files
author
Tim Vandecasteele
committed
Merge branch 'pr/46'
2 parents e1201dd + 87aaa45 commit f2717ff

File tree

4 files changed

+93
-56
lines changed

4 files changed

+93
-56
lines changed

CHANGELOG.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
### Next Release
22

3+
* Fix: translate parameter `type` to String, enables using Mongoid fields as parameter definitions - [@dblock](https://github.com/dblock).
4+
* Adding support for generating swagger responseClass and models from Grape Entities - [@calebwoods](https://github.com/calebwoods).
5+
* Your Contribution Here
6+
37
### 0.6.0 (June 19, 2013)
48

59
* Added Rails 4 support - [@jrhe](https://github.com/jrhe).

lib/grape-swagger.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def parse_params(params, path, method)
126126
params.map do |param, value|
127127
value[:type] = 'file' if value.is_a?(Hash) && value[:type] == 'Rack::Multipart::UploadedFile'
128128

129-
dataType = value.is_a?(Hash) ? value[:type]||'String' : 'String'
129+
dataType = value.is_a?(Hash) ? (value[:type] || 'String').to_s : 'String'
130130
description = value.is_a?(Hash) ? value[:desc] || value[:description] : ''
131131
required = value.is_a?(Hash) ? !!value[:required] : false
132132
paramType = path.include?(":#{param}") ? 'path' : (method == 'POST') ? 'form' : 'query'

spec/grape-swagger_helper_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ class HelperTestAPI < Grape::API
4444
{ paramType: "form", name: :level, description: "", dataType: "String", required: false }
4545
]
4646
end
47+
48+
context "custom type" do
49+
before :all do
50+
class CustomType
51+
end
52+
end
53+
it "parses a custom parameters" do
54+
params = {
55+
option: { type: CustomType, desc: "Custom option" }
56+
}
57+
path = "/coolness"
58+
method = "GET"
59+
@api.parse_params(params, path, method).should == [
60+
{ paramType: "query", name: :option, description: "Custom option", dataType: "CustomType", required: false }
61+
]
62+
end
63+
end
64+
4765
end
4866

4967
context "parsing the path" do

spec/simple_mounted_api_spec.rb

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
describe "a simple mounted api" do
44
before :all do
5+
class CustomType; end
6+
57
class SimpleMountedApi < Grape::API
68
desc "Document root"
79
get do
@@ -17,8 +19,8 @@ class SimpleMountedApi < Grape::API
1719

1820
desc 'this gets something else', {
1921
:headers => {
20-
"XAuthToken" => {description: "A required header.", required: true},
21-
"XOtherHeader" => {description: "An optional header.", required: false}
22+
"XAuthToken" => { description: "A required header.", required: true },
23+
"XOtherHeader" => { description: "An optional header.", required: false }
2224
},
2325
:http_codes => {
2426
403 => "invalid pony",
@@ -31,12 +33,21 @@ class SimpleMountedApi < Grape::API
3133

3234
desc 'this takes an array of parameters', {
3335
:params => {
34-
"items[]" => { :description => "array of items" }
36+
"items[]" => { description: "array of items" }
3537
}
3638
}
3739
post '/items' do
3840
{}
3941
end
42+
43+
desc 'this uses a custom parameter', {
44+
:params => {
45+
"custom" => { type: CustomType, description: "array of items" }
46+
}
47+
}
48+
get '/custom' do
49+
{}
50+
end
4051
end
4152

4253
class SimpleApi < Grape::API
@@ -58,6 +69,7 @@ def app; SimpleApi end
5869
{ "path" => "/swagger_doc/simple.{format}" },
5970
{ "path" => "/swagger_doc/simple_with_headers.{format}" },
6071
{ "path" => "/swagger_doc/items.{format}" },
72+
{ "path" => "/swagger_doc/custom.{format}" },
6173
{ "path" => "/swagger_doc/swagger_doc.{format}" }
6274
]
6375
}
@@ -81,59 +93,62 @@ def app; SimpleApi end
8193
}
8294
end
8395

84-
it "retrieves the documentation for mounted-api that includes headers" do
85-
get '/swagger_doc/simple_with_headers.json'
86-
JSON.parse(last_response.body).should == {
87-
"apiVersion" => "0.1",
88-
"swaggerVersion" => "1.1",
89-
"basePath" => "http://example.org",
90-
"resourcePath" => "",
91-
"apis" => [
92-
{
93-
"path" => "/simple_with_headers.{format}",
94-
"operations" => [
95-
{
96-
"notes" => nil,
97-
"summary" => "this gets something else",
98-
"nickname" => "GET-simple_with_headers---format-",
99-
"httpMethod" => "GET",
100-
"parameters" => [
101-
{ "paramType" => "header", "name" => "XAuthToken", "description" => "A required header.", "dataType" => "String", "required" => true },
102-
{ "paramType" => "header", "name" => "XOtherHeader", "description" => "An optional header.", "dataType" => "String", "required" => false }
103-
],
104-
"errorResponses" => [
105-
{ "code" => 403, "reason" => "invalid pony" },
106-
{ "code" => 405, "reason" => "no ponies left!" }
107-
]
108-
}
109-
]
110-
}
111-
]
112-
}
113-
end
96+
context "retrieves the documentation for mounted-api that" do
97+
it "includes headers" do
98+
get '/swagger_doc/simple_with_headers.json'
99+
JSON.parse(last_response.body)["apis"].should == [{
100+
"path" => "/simple_with_headers.{format}",
101+
"operations" => [
102+
{
103+
"notes" => nil,
104+
"summary" => "this gets something else",
105+
"nickname" => "GET-simple_with_headers---format-",
106+
"httpMethod" => "GET",
107+
"parameters" => [
108+
{ "paramType" => "header", "name" => "XAuthToken", "description" => "A required header.", "dataType" => "String", "required" => true },
109+
{ "paramType" => "header", "name" => "XOtherHeader", "description" => "An optional header.", "dataType" => "String", "required" => false }
110+
],
111+
"errorResponses" => [
112+
{ "code" => 403, "reason" => "invalid pony" },
113+
{ "code" => 405, "reason" => "no ponies left!" }
114+
]
115+
}
116+
]
117+
}]
118+
end
114119

115-
it "retrieves the documentation for mounted-api that supports multiple parameters" do
116-
get '/swagger_doc/items.json'
120+
it "retrieves the documentation for mounted-api that supports multiple parameters" do
121+
get '/swagger_doc/items.json'
122+
JSON.parse(last_response.body)["apis"].should == [{
123+
"path" => "/items.{format}",
124+
"operations" => [
125+
{
126+
"notes" => nil,
127+
"summary" => "this takes an array of parameters",
128+
"nickname" => "POST-items---format-",
129+
"httpMethod" => "POST",
130+
"parameters" => [ { "paramType" => "form", "name" => "items[]", "description" => "array of items", "dataType" => "String", "required" => false } ]
131+
}
132+
]
133+
}]
134+
end
135+
136+
it "supports custom types" do
137+
get '/swagger_doc/custom.json'
138+
JSON.parse(last_response.body)["apis"].should == [{
139+
"path" => "/custom.{format}",
140+
"operations" => [
141+
{
142+
"notes" => nil,
143+
"summary" => "this uses a custom parameter",
144+
"nickname" => "GET-custom---format-",
145+
"httpMethod" => "GET",
146+
"parameters" => [ { "paramType" => "query", "name" => "custom", "description" => "array of items", "dataType" => "CustomType", "required" => false } ]
147+
}
148+
]
149+
}]
150+
end
117151

118-
JSON.parse(last_response.body).should == {
119-
"apiVersion" => "0.1",
120-
"swaggerVersion" => "1.1",
121-
"basePath" => "http://example.org",
122-
"resourcePath" => "",
123-
"apis" => [
124-
{
125-
"path" => "/items.{format}",
126-
"operations" => [
127-
{
128-
"notes" => nil,
129-
"summary" => "this takes an array of parameters",
130-
"nickname" => "POST-items---format-",
131-
"httpMethod" => "POST",
132-
"parameters" => [ { "paramType" => "form", "name" => "items[]", "description" => "array of items", "dataType" => "String", "required" => false } ]
133-
}
134-
]
135-
}
136-
]
137-
}
138152
end
153+
139154
end

0 commit comments

Comments
 (0)