grape-swagger 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rspec +2 -0
- data/.rvmrc +1 -1
- data/.travis.yml +2 -0
- data/CHANGELOG.markdown +9 -0
- data/Gemfile +2 -1
- data/Gemfile.lock +24 -10
- data/README.markdown +58 -6
- data/VERSION +1 -1
- data/grape-swagger.gemspec +14 -7
- data/lib/grape-swagger.rb +235 -93
- data/spec/api_models_spec.rb +132 -0
- data/spec/default_api_spec.rb +80 -10
- data/spec/form_params_spec.rb +83 -0
- data/spec/grape-swagger_helper_spec.rb +107 -0
- data/spec/grape-swagger_spec.rb +0 -2
- data/spec/hide_api_spec.rb +106 -0
- data/spec/non_default_api_spec.rb +318 -78
- data/spec/simple_mounted_api_spec.rb +139 -19
- data/spec/spec_helper.rb +2 -10
- metadata +26 -6
- data/spec/grape-swagger-helper_spec.rb +0 -88
@@ -1,28 +1,38 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "a simple mounted api" do
|
4
|
-
before
|
4
|
+
before :all do
|
5
|
+
class CustomType; end
|
6
|
+
|
5
7
|
class SimpleMountedApi < Grape::API
|
6
8
|
desc "Document root"
|
7
9
|
get do
|
8
10
|
end
|
9
11
|
|
10
|
-
desc '
|
12
|
+
desc 'This gets something.', {
|
11
13
|
:notes => '_test_'
|
12
14
|
}
|
13
15
|
|
14
16
|
get '/simple' do
|
15
|
-
{:
|
17
|
+
{ bla: 'something' }
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'This gets something for URL using - separator.', {
|
21
|
+
:notes => '_test_'
|
22
|
+
}
|
23
|
+
|
24
|
+
get '/simple-test' do
|
25
|
+
{ bla: 'something' }
|
16
26
|
end
|
17
27
|
|
18
28
|
desc 'this gets something else', {
|
19
29
|
:headers => {
|
20
|
-
"XAuthToken" => {description: "A required header.", required: true},
|
21
|
-
"XOtherHeader" => {description: "An optional header.", required: false}
|
30
|
+
"XAuthToken" => { description: "A required header.", required: true },
|
31
|
+
"XOtherHeader" => { description: "An optional header.", required: false }
|
22
32
|
},
|
23
33
|
:http_codes => {
|
24
|
-
|
25
|
-
|
34
|
+
403 => "invalid pony",
|
35
|
+
405 => "no ponies left!"
|
26
36
|
}
|
27
37
|
}
|
28
38
|
get '/simple_with_headers' do
|
@@ -31,12 +41,21 @@ describe "a simple mounted api" do
|
|
31
41
|
|
32
42
|
desc 'this takes an array of parameters', {
|
33
43
|
:params => {
|
34
|
-
"items[]" => { :
|
44
|
+
"items[]" => { description: "array of items" }
|
35
45
|
}
|
36
46
|
}
|
37
47
|
post '/items' do
|
38
48
|
{}
|
39
49
|
end
|
50
|
+
|
51
|
+
desc 'this uses a custom parameter', {
|
52
|
+
:params => {
|
53
|
+
"custom" => { type: CustomType, description: "array of items" }
|
54
|
+
}
|
55
|
+
}
|
56
|
+
get '/custom' do
|
57
|
+
{}
|
58
|
+
end
|
40
59
|
end
|
41
60
|
|
42
61
|
class SimpleApi < Grape::API
|
@@ -48,22 +67,123 @@ describe "a simple mounted api" do
|
|
48
67
|
def app; SimpleApi end
|
49
68
|
|
50
69
|
it "retrieves swagger-documentation on /swagger_doc" do
|
51
|
-
get '/swagger_doc'
|
52
|
-
last_response.body.should ==
|
70
|
+
get '/swagger_doc.json'
|
71
|
+
JSON.parse(last_response.body).should == {
|
72
|
+
"apiVersion" => "0.1",
|
73
|
+
"swaggerVersion" => "1.2",
|
74
|
+
"basePath" => "http://example.org",
|
75
|
+
"info" => {},
|
76
|
+
"produces" => ["application/xml", "application/json", "text/plain"],
|
77
|
+
"operations" => [],
|
78
|
+
"apis" => [
|
79
|
+
{ "path" => "/simple.{format}" },
|
80
|
+
{ "path" => "/simple-test.{format}" },
|
81
|
+
{ "path" => "/simple_with_headers.{format}" },
|
82
|
+
{ "path" => "/items.{format}" },
|
83
|
+
{ "path" => "/custom.{format}" },
|
84
|
+
{ "path" => "/swagger_doc.{format}" }
|
85
|
+
]
|
86
|
+
}
|
53
87
|
end
|
54
88
|
|
55
89
|
it "retrieves the documentation for mounted-api" do
|
56
|
-
get '/swagger_doc/simple'
|
57
|
-
last_response.body.should ==
|
90
|
+
get '/swagger_doc/simple.json'
|
91
|
+
JSON.parse(last_response.body).should == {
|
92
|
+
"apiVersion" => "0.1",
|
93
|
+
"swaggerVersion" => "1.2",
|
94
|
+
"basePath" => "http://example.org",
|
95
|
+
"resourcePath" => "",
|
96
|
+
"apis" => [{
|
97
|
+
"path" => "/simple.{format}",
|
98
|
+
"operations" => [{
|
99
|
+
"produces" => ["application/xml", "application/json", "text/plain"],
|
100
|
+
"notes" => "_test_",
|
101
|
+
"summary" => "This gets something.",
|
102
|
+
"nickname" => "GET-simple---format-",
|
103
|
+
"httpMethod" => "GET",
|
104
|
+
"parameters" => []
|
105
|
+
}]
|
106
|
+
}]
|
107
|
+
}
|
58
108
|
end
|
59
109
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
110
|
+
context "retrieves the documentation for mounted-api that" do
|
111
|
+
it "contains '-' in URL" do
|
112
|
+
get '/swagger_doc/simple-test.json'
|
113
|
+
JSON.parse(last_response.body).should == {
|
114
|
+
"apiVersion" => "0.1",
|
115
|
+
"swaggerVersion" => "1.2",
|
116
|
+
"basePath" => "http://example.org",
|
117
|
+
"resourcePath" => "",
|
118
|
+
"apis" => [{
|
119
|
+
"path" => "/simple-test.{format}",
|
120
|
+
"operations" => [{
|
121
|
+
"produces" => ["application/xml", "application/json", "text/plain"],
|
122
|
+
"notes" => "_test_",
|
123
|
+
"summary" => "This gets something for URL using - separator.",
|
124
|
+
"nickname" => "GET-simple-test---format-",
|
125
|
+
"httpMethod" => "GET",
|
126
|
+
"parameters" => []
|
127
|
+
}]
|
128
|
+
}]
|
129
|
+
}
|
130
|
+
end
|
131
|
+
|
132
|
+
it "includes headers" do
|
133
|
+
get '/swagger_doc/simple_with_headers.json'
|
134
|
+
JSON.parse(last_response.body)["apis"].should == [{
|
135
|
+
"path" => "/simple_with_headers.{format}",
|
136
|
+
"operations" => [{
|
137
|
+
"produces" => ["application/xml", "application/json", "text/plain"],
|
138
|
+
"notes" => nil,
|
139
|
+
"notes" => "",
|
140
|
+
"summary" => "this gets something else",
|
141
|
+
"nickname" => "GET-simple_with_headers---format-",
|
142
|
+
"httpMethod" => "GET",
|
143
|
+
"parameters" => [
|
144
|
+
{ "paramType" => "header", "name" => "XAuthToken", "description" => "A required header.", "type" => "String", "dataType" => "String", "required" => true },
|
145
|
+
{ "paramType" => "header", "name" => "XOtherHeader", "description" => "An optional header.", "type" => "String", "dataType" => "String", "required" => false }
|
146
|
+
],
|
147
|
+
"responseMessages" => [
|
148
|
+
{ "code" => 403, "message" => "invalid pony" },
|
149
|
+
{ "code" => 405, "message" => "no ponies left!" }
|
150
|
+
]
|
151
|
+
}]
|
152
|
+
}]
|
153
|
+
end
|
154
|
+
|
155
|
+
it "supports multiple parameters" do
|
156
|
+
get '/swagger_doc/items.json'
|
157
|
+
JSON.parse(last_response.body)["apis"].should == [{
|
158
|
+
"path" => "/items.{format}",
|
159
|
+
"operations" => [{
|
160
|
+
"produces" => ["application/xml", "application/json", "text/plain"],
|
161
|
+
"notes" => nil,
|
162
|
+
"notes" => "",
|
163
|
+
"summary" => "this takes an array of parameters",
|
164
|
+
"nickname" => "POST-items---format-",
|
165
|
+
"httpMethod" => "POST",
|
166
|
+
"parameters" => [ { "paramType" => "form", "name" => "items[]", "description" => "array of items", "type" => "String", "dataType" => "String", "required" => false } ]
|
167
|
+
}]
|
168
|
+
}]
|
169
|
+
end
|
170
|
+
|
171
|
+
it "supports custom types" do
|
172
|
+
get '/swagger_doc/custom.json'
|
173
|
+
JSON.parse(last_response.body)["apis"].should == [{
|
174
|
+
"path" => "/custom.{format}",
|
175
|
+
"operations" => [{
|
176
|
+
"produces" => ["application/xml", "application/json", "text/plain"],
|
177
|
+
"notes" => nil,
|
178
|
+
"notes" => "",
|
179
|
+
"summary" => "this uses a custom parameter",
|
180
|
+
"nickname" => "GET-custom---format-",
|
181
|
+
"httpMethod" => "GET",
|
182
|
+
"parameters" => [ { "paramType" => "query", "name" => "custom", "description" => "array of items", "type" => "CustomType", "dataType" => "CustomType", "required" => false } ]
|
183
|
+
}]
|
184
|
+
}]
|
185
|
+
end
|
64
186
|
|
65
|
-
it "retrieves the documentation for mounted-api that supports multiple parameters" do
|
66
|
-
get '/swagger_doc/items'
|
67
|
-
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/items.{format}\", :operations=>[{:notes=>nil, :summary=>\"this takes an array of parameters\", :nickname=>\"POST-items---format-\", :httpMethod=>\"POST\", :parameters=>[{:paramType=>\"form\", :name=>\"items[]\", :description=>\"array of items\", :dataType=>\"String\", :required=>false}]}]}]}"
|
68
187
|
end
|
188
|
+
|
69
189
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,29 +4,21 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'support'))
|
|
4
4
|
|
5
5
|
require 'grape'
|
6
6
|
require 'grape-swagger'
|
7
|
+
require 'grape-entity'
|
7
8
|
|
8
9
|
require 'rubygems'
|
9
10
|
require 'bundler'
|
10
11
|
|
11
12
|
require 'pry'
|
13
|
+
require 'json'
|
12
14
|
|
13
15
|
Bundler.setup :default, :test
|
14
16
|
|
15
|
-
|
16
17
|
require 'rack/test'
|
17
18
|
|
18
|
-
# Load support files
|
19
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
20
|
-
|
21
19
|
RSpec.configure do |config|
|
22
|
-
# Remove this line if you don't want RSpec's should and should_not
|
23
|
-
# methods or matchers
|
24
20
|
require 'rspec/expectations'
|
25
21
|
config.include RSpec::Matchers
|
26
|
-
|
27
|
-
# == Mock Framework
|
28
22
|
config.mock_with :rspec
|
29
|
-
|
30
23
|
config.include Rack::Test::Methods
|
31
|
-
#config.include Rack::Test::Methods::Patch
|
32
24
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-swagger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-02-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: grape
|
@@ -28,7 +28,23 @@ dependencies:
|
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 0.2.0
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
|
-
name:
|
31
|
+
name: grape-entity
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.3.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.3.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: github-markdown
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
33
49
|
none: false
|
34
50
|
requirements:
|
@@ -165,6 +181,7 @@ extra_rdoc_files:
|
|
165
181
|
- README.markdown
|
166
182
|
files:
|
167
183
|
- .document
|
184
|
+
- .rspec
|
168
185
|
- .rvmrc
|
169
186
|
- .travis.yml
|
170
187
|
- CHANGELOG.markdown
|
@@ -176,9 +193,12 @@ files:
|
|
176
193
|
- VERSION
|
177
194
|
- grape-swagger.gemspec
|
178
195
|
- lib/grape-swagger.rb
|
196
|
+
- spec/api_models_spec.rb
|
179
197
|
- spec/default_api_spec.rb
|
180
|
-
- spec/
|
198
|
+
- spec/form_params_spec.rb
|
199
|
+
- spec/grape-swagger_helper_spec.rb
|
181
200
|
- spec/grape-swagger_spec.rb
|
201
|
+
- spec/hide_api_spec.rb
|
182
202
|
- spec/non_default_api_spec.rb
|
183
203
|
- spec/simple_mounted_api_spec.rb
|
184
204
|
- spec/spec_helper.rb
|
@@ -197,7 +217,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
197
217
|
version: '0'
|
198
218
|
segments:
|
199
219
|
- 0
|
200
|
-
hash:
|
220
|
+
hash: 1382622318618846643
|
201
221
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
202
222
|
none: false
|
203
223
|
requirements:
|
@@ -206,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
206
226
|
version: '0'
|
207
227
|
requirements: []
|
208
228
|
rubyforge_project:
|
209
|
-
rubygems_version: 1.8.
|
229
|
+
rubygems_version: 1.8.23
|
210
230
|
signing_key:
|
211
231
|
specification_version: 3
|
212
232
|
summary: Add swagger compliant documentation to your grape API
|
@@ -1,88 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "helpers" do
|
4
|
-
|
5
|
-
before(:all) do
|
6
|
-
class HelperTestAPI < Grape::API
|
7
|
-
add_swagger_documentation
|
8
|
-
end
|
9
|
-
|
10
|
-
@api = Object.new
|
11
|
-
# after injecting grape-swagger into the Test API we get the helper methods
|
12
|
-
# back from the first endpoint's class (the API mounted by grape-swagger
|
13
|
-
# to serve the swagger_doc
|
14
|
-
@api.extend HelperTestAPI.endpoints.first.options[:app].helpers
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
context "parsing parameters" do
|
19
|
-
it "should parse params as query strings for a GET" do
|
20
|
-
params = {
|
21
|
-
name: {type: 'String', :desc => "A name", required: true },
|
22
|
-
level: 'max'
|
23
|
-
}
|
24
|
-
path = "/coolness"
|
25
|
-
method = "GET"
|
26
|
-
@api.parse_params(params, path, method).should ==
|
27
|
-
[
|
28
|
-
{paramType: "query", name: :name, description:"A name", dataType: "String", required: true},
|
29
|
-
{paramType: "query", name: :level, description:"", dataType: "String", required: false}
|
30
|
-
]
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should parse params as form for a POST" do
|
34
|
-
params = {
|
35
|
-
name: {type: 'String', :desc =>"A name", required: true },
|
36
|
-
level: 'max'
|
37
|
-
}
|
38
|
-
path = "/coolness"
|
39
|
-
method = "POST"
|
40
|
-
@api.parse_params(params, path, method).should ==
|
41
|
-
[
|
42
|
-
{paramType: "form", name: :name, description:"A name", dataType: "String", required: true},
|
43
|
-
{paramType: "form", name: :level, description:"", dataType: "String", required: false}
|
44
|
-
]
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
context "parsing the path" do
|
49
|
-
it "should parse the path" do
|
50
|
-
path = ":abc/def(.:format)"
|
51
|
-
@api.parse_path(path, nil).should == "{abc}/def.{format}"
|
52
|
-
end
|
53
|
-
|
54
|
-
it "should parse a path that has vars with underscores in the name" do
|
55
|
-
path = "abc/:def_g(.:format)"
|
56
|
-
@api.parse_path(path, nil).should == "abc/{def_g}.{format}"
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should parse a path that has vars with numbers in the name" do
|
61
|
-
path = "abc/:sha1(.:format)"
|
62
|
-
@api.parse_path(path, nil).should == "abc/{sha1}.{format}"
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should parse a path that has multiple variables" do
|
66
|
-
path1 = "abc/:def/:geh(.:format)"
|
67
|
-
path2 = "abc/:def:geh(.:format)"
|
68
|
-
@api.parse_path(path1, nil).should == "abc/{def}/{geh}.{format}"
|
69
|
-
@api.parse_path(path2, nil).should == "abc/{def}{geh}.{format}"
|
70
|
-
end
|
71
|
-
|
72
|
-
it "should parse the path with a specified version" do
|
73
|
-
path = ":abc/{version}/def(.:format)"
|
74
|
-
@api.parse_path(path, 'v1').should == "{abc}/v1/def.{format}"
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
context "parsing header parameters" do
|
79
|
-
it "should parse params for the header" do
|
80
|
-
params = {"XAuthToken" => { description: "A required header.", required: true}}
|
81
|
-
@api.parse_header_params(params).should ==
|
82
|
-
[
|
83
|
-
{paramType: "header", name: "XAuthToken", description:"A required header.", dataType: "String", required: true}
|
84
|
-
]
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|