grape-swagger 0.11.0 → 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -1
- data/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +14 -22
- data/.travis.yml +7 -4
- data/CHANGELOG.md +53 -26
- data/Gemfile +1 -1
- data/README.md +414 -327
- data/RELEASING.md +3 -4
- data/example/api/endpoints.rb +132 -0
- data/example/api/entities.rb +18 -0
- data/example/config.ru +36 -2
- data/example/example_requests.postman_collection +146 -0
- data/example/swagger-example.png +0 -0
- data/grape-swagger.gemspec +9 -6
- data/lib/grape-swagger.rb +69 -99
- data/lib/grape-swagger/doc_methods.rb +69 -544
- data/lib/grape-swagger/doc_methods/data_type.rb +77 -0
- data/lib/grape-swagger/doc_methods/extensions.rb +75 -0
- data/lib/grape-swagger/doc_methods/move_params.rb +153 -0
- data/lib/grape-swagger/doc_methods/operation_id.rb +27 -0
- data/lib/grape-swagger/doc_methods/optional_object.rb +15 -0
- data/lib/grape-swagger/doc_methods/parse_params.rb +113 -0
- data/lib/grape-swagger/doc_methods/path_string.rb +29 -0
- data/lib/grape-swagger/doc_methods/produces_consumes.rb +12 -0
- data/lib/grape-swagger/doc_methods/status_codes.rb +17 -0
- data/lib/grape-swagger/doc_methods/tag_name_description.rb +26 -0
- data/lib/grape-swagger/endpoint.rb +317 -0
- data/lib/grape-swagger/version.rb +1 -1
- data/spec/lib/data_type_spec.rb +57 -0
- data/spec/lib/endpoint_spec.rb +6 -0
- data/spec/lib/extensions_spec.rb +127 -0
- data/spec/lib/move_params_spec.rb +298 -0
- data/spec/lib/operation_id_spec.rb +24 -0
- data/spec/lib/optional_object_spec.rb +40 -0
- data/spec/lib/path_string_spec.rb +38 -0
- data/spec/lib/produces_consumes_spec.rb +98 -0
- data/spec/markdown/kramdown_adapter_spec.rb +2 -9
- data/spec/markdown/redcarpet_adapter_spec.rb +2 -16
- data/spec/spec_helper.rb +7 -13
- data/spec/support/api_swagger_v2_result.rb +204 -0
- data/spec/support/namespace_tags.rb +73 -0
- data/spec/support/the_api_entities.rb +52 -0
- data/spec/support/the_paths_definitions.rb +94 -0
- data/spec/swagger_v2/api_swagger_v2_definitions-models_spec.rb +32 -0
- data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +151 -0
- data/spec/swagger_v2/api_swagger_v2_extensions_spec.rb +109 -0
- data/spec/swagger_v2/api_swagger_v2_format-content_type_spec.rb +124 -0
- data/spec/swagger_v2/api_swagger_v2_global_configuration_spec.rb +51 -0
- data/spec/swagger_v2/api_swagger_v2_headers_spec.rb +44 -0
- data/spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb +56 -0
- data/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +146 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_body_nested_spec.rb +197 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +151 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_spec.rb +217 -0
- data/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb +64 -0
- data/spec/swagger_v2/api_swagger_v2_response_spec.rb +184 -0
- data/spec/swagger_v2/api_swagger_v2_spec.rb +207 -0
- data/spec/swagger_v2/api_swagger_v2_type-format_spec.rb +121 -0
- data/spec/{boolean_params_spec.rb → swagger_v2/boolean_params_spec.rb} +2 -2
- data/spec/{default_api_spec.rb → swagger_v2/default_api_spec.rb} +40 -36
- data/spec/swagger_v2/description_not_initialized.rb +39 -0
- data/spec/{float_api_spec.rb → swagger_v2/float_api_spec.rb} +2 -2
- data/spec/{form_params_spec.rb → swagger_v2/form_params_spec.rb} +9 -9
- data/spec/{grape-swagger_spec.rb → swagger_v2/grape-swagger_spec.rb} +0 -0
- data/spec/swagger_v2/hide_api_spec.rb +131 -0
- data/spec/swagger_v2/mounted_target_class_spec.rb +76 -0
- data/spec/swagger_v2/namespace_tags_prefix_spec.rb +84 -0
- data/spec/swagger_v2/namespace_tags_spec.rb +76 -0
- data/spec/{namespaced_api_spec.rb → swagger_v2/namespaced_api_spec.rb} +6 -26
- data/spec/{param_type_spec.rb → swagger_v2/param_type_spec.rb} +10 -8
- data/spec/{param_values_spec.rb → swagger_v2/param_values_spec.rb} +52 -22
- data/spec/swagger_v2/params_array_spec.rb +63 -0
- data/spec/swagger_v2/params_hash_spec.rb +65 -0
- data/spec/swagger_v2/params_nested_spec.rb +63 -0
- data/spec/{reference_entity.rb → swagger_v2/reference_entity.rb} +18 -23
- data/spec/swagger_v2/response_model_spec.rb +212 -0
- data/spec/swagger_v2/simple_mounted_api_spec.rb +264 -0
- metadata +175 -90
- data/example/api.rb +0 -66
- data/lib/grape-swagger/markdown.rb +0 -23
- data/spec/api_description_spec.rb +0 -43
- data/spec/api_global_models_spec.rb +0 -77
- data/spec/api_models_spec.rb +0 -364
- data/spec/api_paths_spec.rb +0 -128
- data/spec/api_root_spec.rb +0 -30
- data/spec/api_with_nil_types.rb +0 -50
- data/spec/api_with_path_versioning_spec.rb +0 -33
- data/spec/api_with_prefix_and_namespace_spec.rb +0 -32
- data/spec/api_with_standalone_namespace_spec.rb +0 -215
- data/spec/array_entity_spec.rb +0 -34
- data/spec/array_params_spec.rb +0 -85
- data/spec/grape-swagger_helper_spec.rb +0 -152
- data/spec/group_params_spec.rb +0 -31
- data/spec/hash_params_spec.rb +0 -30
- data/spec/hide_api_spec.rb +0 -124
- data/spec/i18n_spec.rb +0 -364
- data/spec/markdown/markdown_spec.rb +0 -27
- data/spec/mounted_target_class_spec.rb +0 -63
- data/spec/mutually_exclusive_spec.rb +0 -36
- data/spec/non_default_api_spec.rb +0 -733
- data/spec/response_model_spec.rb +0 -121
- data/spec/simple_mounted_api_spec.rb +0 -213
- data/spec/support/i18n_helper.rb +0 -8
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'Mutually exclusive group params' do
|
4
|
-
def app
|
5
|
-
Class.new(Grape::API) do
|
6
|
-
format :json
|
7
|
-
|
8
|
-
params do
|
9
|
-
requires :required_group, type: Hash do
|
10
|
-
group :param_group_1, type: Hash do
|
11
|
-
requires :param_1, type: String
|
12
|
-
end
|
13
|
-
optional :param_group_2, type: Array do
|
14
|
-
requires :param_2, type: String
|
15
|
-
end
|
16
|
-
mutually_exclusive :param_group_1, :param_group_2
|
17
|
-
end
|
18
|
-
end
|
19
|
-
post '/groups' do
|
20
|
-
{}
|
21
|
-
end
|
22
|
-
|
23
|
-
add_swagger_documentation
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'marks the nested required params in a group as optional if the group is in a mutually_exclusive set' do
|
28
|
-
get '/swagger_doc/groups'
|
29
|
-
|
30
|
-
body = JSON.parse last_response.body
|
31
|
-
parameters = body['apis'].first['operations'].first['parameters']
|
32
|
-
expect(parameters).to eq [
|
33
|
-
{ 'paramType' => 'form', 'name' => 'required_group[param_group_1][param_1]', 'description' => '', 'type' => 'string', 'required' => false, 'allowMultiple' => false },
|
34
|
-
{ 'paramType' => 'form', 'name' => 'required_group[param_group_2][][param_2]', 'description' => '', 'type' => 'string', 'required' => true, 'allowMultiple' => false }]
|
35
|
-
end
|
36
|
-
end
|
@@ -1,733 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'options: ' do
|
4
|
-
context 'overriding the basepath' do
|
5
|
-
before :all do
|
6
|
-
class BasePathMountedApi < Grape::API
|
7
|
-
desc 'This gets something.'
|
8
|
-
get '/something' do
|
9
|
-
{ bla: 'something' }
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class SimpleApiWithBasePath < Grape::API
|
14
|
-
NON_DEFAULT_BASE_PATH = 'http://www.breakcoregivesmewood.com'.freeze
|
15
|
-
|
16
|
-
mount BasePathMountedApi
|
17
|
-
add_swagger_documentation base_path: NON_DEFAULT_BASE_PATH
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def app
|
22
|
-
SimpleApiWithBasePath
|
23
|
-
end
|
24
|
-
|
25
|
-
subject do
|
26
|
-
get '/swagger_doc/something.json'
|
27
|
-
JSON.parse(last_response.body)
|
28
|
-
end
|
29
|
-
|
30
|
-
# it "retrieves the given base-path on /swagger_doc" do
|
31
|
-
# get '/swagger_doc.json'
|
32
|
-
# JSON.parse(last_response.body)["basePath"].should == SimpleApiWithBasePath::NON_DEFAULT_BASE_PATH
|
33
|
-
# end
|
34
|
-
|
35
|
-
it 'retrieves the same given base-path for mounted-api' do
|
36
|
-
expect(subject['basePath']).to eq SimpleApiWithBasePath::NON_DEFAULT_BASE_PATH
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
context 'overriding the basepath with a proc' do
|
41
|
-
before :all do
|
42
|
-
class ProcBasePathMountedApi < Grape::API
|
43
|
-
desc 'This gets something.'
|
44
|
-
get '/something' do
|
45
|
-
{ bla: 'something' }
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
class SimpleApiWithProcBasePath < Grape::API
|
50
|
-
mount ProcBasePathMountedApi
|
51
|
-
add_swagger_documentation base_path: proc { |request| [request.base_url, request.params[:base_path], 'some_value'].compact.join('/') }
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def app
|
56
|
-
SimpleApiWithProcBasePath
|
57
|
-
end
|
58
|
-
|
59
|
-
context 'default' do
|
60
|
-
subject do
|
61
|
-
get '/swagger_doc/something.json'
|
62
|
-
JSON.parse(last_response.body)
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'retrieves the same given base-path for mounted-api' do
|
66
|
-
expect(subject['basePath']).to eq 'http://example.org/some_value'
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
context 'param' do
|
71
|
-
subject do
|
72
|
-
get '/swagger_doc/something.json?base_path=foobar'
|
73
|
-
JSON.parse(last_response.body)
|
74
|
-
end
|
75
|
-
|
76
|
-
it 're-evaluates base-path' do
|
77
|
-
expect(subject['basePath']).to eq 'http://example.org/foobar/some_value'
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
context 'relative base_path' do
|
83
|
-
before :all do
|
84
|
-
class RelativeBasePathMountedApi < Grape::API
|
85
|
-
desc 'This gets something.'
|
86
|
-
get '/something' do
|
87
|
-
{ bla: 'something' }
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
class SimpleApiWithRelativeBasePath < Grape::API
|
92
|
-
mount RelativeBasePathMountedApi
|
93
|
-
add_swagger_documentation base_path: '/some_value'
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
def app
|
98
|
-
SimpleApiWithRelativeBasePath
|
99
|
-
end
|
100
|
-
|
101
|
-
subject do
|
102
|
-
get '/swagger_doc/something.json'
|
103
|
-
JSON.parse(last_response.body)
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'retrieves the same given base-path for mounted-api' do
|
107
|
-
get '/swagger_doc/something.json'
|
108
|
-
expect(subject['basePath']).to eq 'http://example.org/some_value'
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
context 'overriding the version' do
|
113
|
-
before :all do
|
114
|
-
class ApiVersionMountedApi < Grape::API
|
115
|
-
desc 'This gets something.'
|
116
|
-
get '/something' do
|
117
|
-
{ bla: 'something' }
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
class SimpleApiWithApiVersion < Grape::API
|
122
|
-
API_VERSION = '101'.freeze
|
123
|
-
|
124
|
-
mount ApiVersionMountedApi
|
125
|
-
add_swagger_documentation api_version: API_VERSION
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
def app
|
130
|
-
SimpleApiWithApiVersion
|
131
|
-
end
|
132
|
-
|
133
|
-
it 'retrieves the api version on /swagger_doc' do
|
134
|
-
get '/swagger_doc.json'
|
135
|
-
expect(JSON.parse(last_response.body)['apiVersion']).to eq SimpleApiWithApiVersion::API_VERSION
|
136
|
-
end
|
137
|
-
|
138
|
-
it 'retrieves the same api version for mounted-api' do
|
139
|
-
get '/swagger_doc/something.json'
|
140
|
-
expect(JSON.parse(last_response.body)['apiVersion']).to eq SimpleApiWithApiVersion::API_VERSION
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
context 'mounting in a versioned api' do
|
145
|
-
before :all do
|
146
|
-
class SimpleApiToMountInVersionedApi < Grape::API
|
147
|
-
desc 'This gets something.'
|
148
|
-
get '/something' do
|
149
|
-
{ bla: 'something' }
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
class SimpleApiWithVersionInPath < Grape::API
|
154
|
-
version 'v1', using: :path
|
155
|
-
|
156
|
-
mount SimpleApiToMountInVersionedApi
|
157
|
-
add_swagger_documentation
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
def app
|
162
|
-
SimpleApiWithVersionInPath
|
163
|
-
end
|
164
|
-
|
165
|
-
it 'gets the documentation on a versioned path /v1/swagger_doc' do
|
166
|
-
get '/v1/swagger_doc.json'
|
167
|
-
expect(last_response.status).to eq 200
|
168
|
-
expect(JSON.parse(last_response.body)).to eq(
|
169
|
-
'apiVersion' => '0.1',
|
170
|
-
'swaggerVersion' => '1.2',
|
171
|
-
'info' => {},
|
172
|
-
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
|
173
|
-
'apis' => [
|
174
|
-
{ 'path' => '/something.{format}', 'description' => 'Operations about somethings' },
|
175
|
-
{ 'path' => '/swagger_doc.{format}', 'description' => 'Operations about swagger_docs' }
|
176
|
-
]
|
177
|
-
)
|
178
|
-
end
|
179
|
-
|
180
|
-
it 'gets the resource specific documentation on a versioned path /v1/swagger_doc/something' do
|
181
|
-
get '/v1/swagger_doc/something.json'
|
182
|
-
expect(last_response.status).to eq 200
|
183
|
-
expect(JSON.parse(last_response.body)).to eq(
|
184
|
-
'apiVersion' => '0.1',
|
185
|
-
'swaggerVersion' => '1.2',
|
186
|
-
'basePath' => 'http://example.org',
|
187
|
-
'resourcePath' => '/something',
|
188
|
-
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
|
189
|
-
'apis' => [{
|
190
|
-
'path' => '/0.1/something.{format}',
|
191
|
-
'operations' => [{
|
192
|
-
'notes' => '',
|
193
|
-
'summary' => 'This gets something.',
|
194
|
-
'nickname' => 'GET--version-something---format-',
|
195
|
-
'method' => 'GET',
|
196
|
-
'parameters' => [],
|
197
|
-
'type' => 'void'
|
198
|
-
}]
|
199
|
-
}]
|
200
|
-
)
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
context 'overriding hiding the documentation paths' do
|
205
|
-
before :all do
|
206
|
-
class HideDocumentationPathMountedApi < Grape::API
|
207
|
-
desc 'This gets something.'
|
208
|
-
get '/something' do
|
209
|
-
{ bla: 'something' }
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
class SimpleApiWithHiddenDocumentation < Grape::API
|
214
|
-
mount HideDocumentationPathMountedApi
|
215
|
-
add_swagger_documentation hide_documentation_path: true
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|
219
|
-
def app
|
220
|
-
SimpleApiWithHiddenDocumentation
|
221
|
-
end
|
222
|
-
|
223
|
-
subject do
|
224
|
-
get '/swagger_doc.json'
|
225
|
-
JSON.parse(last_response.body)
|
226
|
-
end
|
227
|
-
|
228
|
-
it "it doesn't show the documentation path on /swagger_doc" do
|
229
|
-
expect(subject).to eq(
|
230
|
-
'apiVersion' => '0.1',
|
231
|
-
'swaggerVersion' => '1.2',
|
232
|
-
'info' => {},
|
233
|
-
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
|
234
|
-
'apis' => [
|
235
|
-
{ 'path' => '/something.{format}', 'description' => 'Operations about somethings' }
|
236
|
-
]
|
237
|
-
)
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
context 'overriding hiding the documentation paths in prefixed API' do
|
242
|
-
before :all do
|
243
|
-
class HideDocumentationPathPrefixedMountedApi < Grape::API
|
244
|
-
desc 'This gets something.'
|
245
|
-
get '/something' do
|
246
|
-
{ bla: 'something' }
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
class PrefixedApiWithHiddenDocumentation < Grape::API
|
251
|
-
prefix 'abc'
|
252
|
-
mount HideDocumentationPathPrefixedMountedApi
|
253
|
-
add_swagger_documentation hide_documentation_path: true
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
def app
|
258
|
-
PrefixedApiWithHiddenDocumentation
|
259
|
-
end
|
260
|
-
|
261
|
-
subject do
|
262
|
-
get '/abc/swagger_doc/something.json'
|
263
|
-
JSON.parse(last_response.body)
|
264
|
-
end
|
265
|
-
|
266
|
-
it "it doesn't show the documentation path on /abc/swagger_doc/something.json" do
|
267
|
-
expect(subject).to eq(
|
268
|
-
'apiVersion' => '0.1',
|
269
|
-
'swaggerVersion' => '1.2',
|
270
|
-
'basePath' => 'http://example.org',
|
271
|
-
'resourcePath' => '/something',
|
272
|
-
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
|
273
|
-
'apis' => [{
|
274
|
-
'path' => '/abc/something.{format}',
|
275
|
-
'operations' => [{
|
276
|
-
'notes' => '',
|
277
|
-
'summary' => 'This gets something.',
|
278
|
-
'nickname' => 'GET-abc-something---format-',
|
279
|
-
'method' => 'GET',
|
280
|
-
'parameters' => [],
|
281
|
-
'type' => 'void'
|
282
|
-
}]
|
283
|
-
}]
|
284
|
-
)
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
context 'overriding hiding the documentation paths in prefixed and versioned API' do
|
289
|
-
before :all do
|
290
|
-
class HideDocumentationPathMountedApi2 < Grape::API
|
291
|
-
desc 'This gets something.'
|
292
|
-
get '/something' do
|
293
|
-
{ bla: 'something' }
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
class PrefixedAndVersionedApiWithHiddenDocumentation < Grape::API
|
298
|
-
prefix 'abc'
|
299
|
-
version 'v20', using: :path
|
300
|
-
|
301
|
-
mount HideDocumentationPathMountedApi2
|
302
|
-
|
303
|
-
add_swagger_documentation hide_documentation_path: true, api_version: version
|
304
|
-
end
|
305
|
-
end
|
306
|
-
|
307
|
-
def app
|
308
|
-
PrefixedAndVersionedApiWithHiddenDocumentation
|
309
|
-
end
|
310
|
-
|
311
|
-
subject do
|
312
|
-
get '/abc/v20/swagger_doc/something.json'
|
313
|
-
JSON.parse(last_response.body)
|
314
|
-
end
|
315
|
-
|
316
|
-
it "it doesn't show the documentation path on /abc/v1/swagger_doc/something.json" do
|
317
|
-
expect(subject).to eq(
|
318
|
-
'apiVersion' => 'v20',
|
319
|
-
'swaggerVersion' => '1.2',
|
320
|
-
'basePath' => 'http://example.org',
|
321
|
-
'resourcePath' => '/something',
|
322
|
-
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
|
323
|
-
'apis' => [{
|
324
|
-
'path' => '/abc/v20/something.{format}',
|
325
|
-
'operations' => [{
|
326
|
-
'notes' => '',
|
327
|
-
'summary' => 'This gets something.',
|
328
|
-
'nickname' => 'GET-abc--version-something---format-',
|
329
|
-
'method' => 'GET',
|
330
|
-
'parameters' => [],
|
331
|
-
'type' => 'void'
|
332
|
-
}]
|
333
|
-
}]
|
334
|
-
)
|
335
|
-
end
|
336
|
-
end
|
337
|
-
|
338
|
-
context 'overriding the mount-path' do
|
339
|
-
before :all do
|
340
|
-
class DifferentMountMountedApi < Grape::API
|
341
|
-
desc 'This gets something.'
|
342
|
-
get '/something' do
|
343
|
-
{ bla: 'something' }
|
344
|
-
end
|
345
|
-
end
|
346
|
-
|
347
|
-
class SimpleApiWithDifferentMount < Grape::API
|
348
|
-
MOUNT_PATH = '/api_doc'.freeze
|
349
|
-
|
350
|
-
mount DifferentMountMountedApi
|
351
|
-
add_swagger_documentation mount_path: MOUNT_PATH
|
352
|
-
end
|
353
|
-
end
|
354
|
-
|
355
|
-
def app
|
356
|
-
SimpleApiWithDifferentMount
|
357
|
-
end
|
358
|
-
|
359
|
-
# it "retrieves the given base-path on /api_doc" do
|
360
|
-
# get '/api_doc.json'
|
361
|
-
# JSON.parse(last_response.body)["apis"].each do |api|
|
362
|
-
# api["path"].should start_with SimpleApiWithDifferentMount::MOUNT_PATH
|
363
|
-
# end
|
364
|
-
# end
|
365
|
-
|
366
|
-
it 'retrieves the same given base-path for mounted-api' do
|
367
|
-
get '/api_doc/something.json'
|
368
|
-
JSON.parse(last_response.body)['apis'].each do |api|
|
369
|
-
expect(api['path']).to_not start_with SimpleApiWithDifferentMount::MOUNT_PATH
|
370
|
-
end
|
371
|
-
end
|
372
|
-
|
373
|
-
it 'does not respond to swagger_doc' do
|
374
|
-
get '/swagger_doc.json'
|
375
|
-
expect(last_response.status).to eq 404
|
376
|
-
end
|
377
|
-
end
|
378
|
-
|
379
|
-
context 'overriding the markdown' do
|
380
|
-
before :all do
|
381
|
-
class MarkDownMountedApi < Grape::API
|
382
|
-
desc 'This gets something.', notes: '_test_'
|
383
|
-
get '/something' do
|
384
|
-
{ bla: 'something' }
|
385
|
-
end
|
386
|
-
end
|
387
|
-
|
388
|
-
class SimpleApiWithMarkdown < Grape::API
|
389
|
-
mount MarkDownMountedApi
|
390
|
-
add_swagger_documentation markdown: GrapeSwagger::Markdown::KramdownAdapter.new, info: { description: '_test_' }
|
391
|
-
end
|
392
|
-
end
|
393
|
-
|
394
|
-
subject do
|
395
|
-
Class.new(Grape::API) do
|
396
|
-
mount MarkDownMountedApi
|
397
|
-
end
|
398
|
-
end
|
399
|
-
|
400
|
-
def app
|
401
|
-
SimpleApiWithMarkdown
|
402
|
-
end
|
403
|
-
|
404
|
-
context 'with instance' do
|
405
|
-
before do
|
406
|
-
subject.add_swagger_documentation markdown: GrapeSwagger::Markdown::KramdownAdapter.new, info: { description: '_test_' }
|
407
|
-
end
|
408
|
-
|
409
|
-
it 'parses markdown for a mounted-api' do
|
410
|
-
get '/swagger_doc/something.json'
|
411
|
-
expect(JSON.parse(last_response.body)['apis'][0]['operations'][0]['notes']).to eq("<p><em>test</em></p>\n")
|
412
|
-
end
|
413
|
-
|
414
|
-
it 'parses markdown for swagger info' do
|
415
|
-
get '/swagger_doc.json'
|
416
|
-
expect(JSON.parse(last_response.body)['info']).to eq('description' => "<p><em>test</em></p>\n")
|
417
|
-
end
|
418
|
-
end
|
419
|
-
|
420
|
-
context 'with class' do
|
421
|
-
before do
|
422
|
-
subject.add_swagger_documentation markdown: GrapeSwagger::Markdown::KramdownAdapter, info: { description: '_test_' }
|
423
|
-
end
|
424
|
-
|
425
|
-
it 'parses markdown for a mounted-api' do
|
426
|
-
get '/swagger_doc/something.json'
|
427
|
-
expect(JSON.parse(last_response.body)['apis'][0]['operations'][0]['notes']).to eq("<p><em>test</em></p>\n")
|
428
|
-
end
|
429
|
-
|
430
|
-
it 'parses markdown for swagger info' do
|
431
|
-
get '/swagger_doc.json'
|
432
|
-
expect(JSON.parse(last_response.body)['info']).to eq('description' => "<p><em>test</em></p>\n")
|
433
|
-
end
|
434
|
-
end
|
435
|
-
end
|
436
|
-
|
437
|
-
context 'prefixed and versioned API' do
|
438
|
-
before :all do
|
439
|
-
class VersionedMountedApi < Grape::API
|
440
|
-
prefix 'api'
|
441
|
-
version 'v1'
|
442
|
-
|
443
|
-
desc 'This gets something.'
|
444
|
-
get '/something' do
|
445
|
-
{ bla: 'something' }
|
446
|
-
end
|
447
|
-
end
|
448
|
-
|
449
|
-
class SimpleApiWithVersion < Grape::API
|
450
|
-
mount VersionedMountedApi
|
451
|
-
add_swagger_documentation api_version: 'v1'
|
452
|
-
end
|
453
|
-
end
|
454
|
-
|
455
|
-
def app
|
456
|
-
SimpleApiWithVersion
|
457
|
-
end
|
458
|
-
|
459
|
-
it 'parses version and places it in the path' do
|
460
|
-
get '/swagger_doc/something.json'
|
461
|
-
|
462
|
-
JSON.parse(last_response.body)['apis'].each do |api|
|
463
|
-
expect(api['path']).to start_with '/api/v1/'
|
464
|
-
end
|
465
|
-
end
|
466
|
-
end
|
467
|
-
|
468
|
-
context 'protected API' do
|
469
|
-
before :all do
|
470
|
-
class ProtectedApi < Grape::API
|
471
|
-
desc 'This gets something.'
|
472
|
-
get '/something' do
|
473
|
-
{ bla: 'something' }
|
474
|
-
end
|
475
|
-
end
|
476
|
-
|
477
|
-
class SimpleApiWithProtection < Grape::API
|
478
|
-
mount ::ProtectedApi
|
479
|
-
add_swagger_documentation
|
480
|
-
end
|
481
|
-
end
|
482
|
-
|
483
|
-
def app
|
484
|
-
SimpleApiWithProtection
|
485
|
-
end
|
486
|
-
|
487
|
-
# it "uses https schema in mount point" do
|
488
|
-
# get '/swagger_doc.json', {}, 'rack.url_scheme' => 'https'
|
489
|
-
# JSON.parse(last_response.body)["basePath"].should == "https://example.org:80"
|
490
|
-
# end
|
491
|
-
|
492
|
-
it 'uses https schema in endpoint doc' do
|
493
|
-
get '/swagger_doc/something.json', {}, 'rack.url_scheme' => 'https'
|
494
|
-
expect(JSON.parse(last_response.body)['basePath']).to eq 'https://example.org:80'
|
495
|
-
end
|
496
|
-
end
|
497
|
-
|
498
|
-
context ':hide_format' do
|
499
|
-
context 'with no explicit api format specified' do
|
500
|
-
before :all do
|
501
|
-
class HidePathsApi < Grape::API
|
502
|
-
desc 'This gets something.'
|
503
|
-
get '/something' do
|
504
|
-
{ bla: 'something' }
|
505
|
-
end
|
506
|
-
end
|
507
|
-
|
508
|
-
class SimpleApiWithHiddenPaths < Grape::API
|
509
|
-
mount HidePathsApi
|
510
|
-
add_swagger_documentation hide_format: true
|
511
|
-
end
|
512
|
-
end
|
513
|
-
|
514
|
-
def app
|
515
|
-
SimpleApiWithHiddenPaths
|
516
|
-
end
|
517
|
-
|
518
|
-
it 'does not end with format' do
|
519
|
-
get '/swagger_doc/something.json'
|
520
|
-
JSON.parse(last_response.body)['apis'].each do |api|
|
521
|
-
expect(api['path']).to_not end_with '.{format}'
|
522
|
-
end
|
523
|
-
end
|
524
|
-
end
|
525
|
-
|
526
|
-
context 'with single api format specified' do
|
527
|
-
before :all do
|
528
|
-
class SingleFormatApi < Grape::API
|
529
|
-
format :json
|
530
|
-
desc 'This gets something.'
|
531
|
-
get '/something' do
|
532
|
-
{ bla: 'something' }
|
533
|
-
end
|
534
|
-
end
|
535
|
-
|
536
|
-
class SimpleApiWithSingleFormat < Grape::API
|
537
|
-
mount SingleFormatApi
|
538
|
-
add_swagger_documentation hide_format: true
|
539
|
-
end
|
540
|
-
end
|
541
|
-
|
542
|
-
def app
|
543
|
-
SimpleApiWithSingleFormat
|
544
|
-
end
|
545
|
-
|
546
|
-
it 'does not end with format' do
|
547
|
-
get '/swagger_doc/something.json'
|
548
|
-
JSON.parse(last_response.body)['apis'].each do |api|
|
549
|
-
expect(api['path']).to_not end_with '.{format}'
|
550
|
-
expect(api['path']).to_not end_with '(.json)'
|
551
|
-
end
|
552
|
-
end
|
553
|
-
end
|
554
|
-
end
|
555
|
-
|
556
|
-
context 'multiple documentations' do
|
557
|
-
before :all do
|
558
|
-
class FirstApi < Grape::API
|
559
|
-
desc 'This is the first API'
|
560
|
-
get '/first' do
|
561
|
-
{ first: 'hip' }
|
562
|
-
end
|
563
|
-
|
564
|
-
add_swagger_documentation mount_path: '/first/swagger_doc'
|
565
|
-
end
|
566
|
-
|
567
|
-
class SecondApi < Grape::API
|
568
|
-
desc 'This is the second API'
|
569
|
-
get '/second' do
|
570
|
-
{ second: 'hop' }
|
571
|
-
end
|
572
|
-
|
573
|
-
add_swagger_documentation mount_path: '/second/swagger_doc'
|
574
|
-
end
|
575
|
-
|
576
|
-
class SimpleApiWithMultipleMountedDocumentations < Grape::API
|
577
|
-
mount FirstApi
|
578
|
-
mount SecondApi
|
579
|
-
end
|
580
|
-
end
|
581
|
-
|
582
|
-
def app
|
583
|
-
SimpleApiWithMultipleMountedDocumentations
|
584
|
-
end
|
585
|
-
|
586
|
-
it 'retrieves the first swagger-documentation on /first/swagger_doc' do
|
587
|
-
get '/first/swagger_doc.json'
|
588
|
-
expect(JSON.parse(last_response.body)).to eq(
|
589
|
-
'apiVersion' => '0.1',
|
590
|
-
'swaggerVersion' => '1.2',
|
591
|
-
'info' => {},
|
592
|
-
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
|
593
|
-
'apis' => [
|
594
|
-
{ 'path' => '/first.{format}', 'description' => 'Operations about firsts' }
|
595
|
-
]
|
596
|
-
)
|
597
|
-
end
|
598
|
-
|
599
|
-
it 'retrieves the second swagger-documentation on /second/swagger_doc' do
|
600
|
-
get '/second/swagger_doc.json'
|
601
|
-
expect(JSON.parse(last_response.body)).to eq(
|
602
|
-
'apiVersion' => '0.1',
|
603
|
-
'swaggerVersion' => '1.2',
|
604
|
-
'info' => {},
|
605
|
-
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
|
606
|
-
'apis' => [
|
607
|
-
{ 'path' => '/second.{format}', 'description' => 'Operations about seconds' }
|
608
|
-
]
|
609
|
-
)
|
610
|
-
end
|
611
|
-
end
|
612
|
-
|
613
|
-
context ':formatting' do
|
614
|
-
before :all do
|
615
|
-
class JSONDefaultFormatAPI < Grape::API
|
616
|
-
desc 'This gets something.'
|
617
|
-
get '/something' do
|
618
|
-
{ bla: 'something' }
|
619
|
-
end
|
620
|
-
end
|
621
|
-
|
622
|
-
class SimpleJSONFormattedAPI < Grape::API
|
623
|
-
mount JSONDefaultFormatAPI
|
624
|
-
add_swagger_documentation format: :json
|
625
|
-
end
|
626
|
-
end
|
627
|
-
|
628
|
-
def app
|
629
|
-
SimpleJSONFormattedAPI
|
630
|
-
end
|
631
|
-
|
632
|
-
subject do
|
633
|
-
get '/swagger_doc/something'
|
634
|
-
last_response
|
635
|
-
end
|
636
|
-
|
637
|
-
it 'defaults to JSON format when none is specified' do
|
638
|
-
get '/swagger_doc/something'
|
639
|
-
expect(subject.headers['Content-Type']).to eq 'application/json'
|
640
|
-
expect(-> { JSON.parse(subject.body) }).to_not raise_error
|
641
|
-
end
|
642
|
-
end
|
643
|
-
|
644
|
-
context 'documented namespace description' do
|
645
|
-
before :all do
|
646
|
-
class NestedNamespaceWithDescAPI < Grape::API
|
647
|
-
namespace :nestedspace, desc: 'Description for nested space' do
|
648
|
-
desc 'Nested get'
|
649
|
-
get '/somethingelse' do
|
650
|
-
{ foo: 'bar' }
|
651
|
-
end
|
652
|
-
end
|
653
|
-
end
|
654
|
-
|
655
|
-
class NamespaceWithDescAPI < Grape::API
|
656
|
-
namespace :aspace, desc: 'Description for aspace' do
|
657
|
-
desc 'This gets something.'
|
658
|
-
get '/something' do
|
659
|
-
{ bla: 'something' }
|
660
|
-
end
|
661
|
-
end
|
662
|
-
|
663
|
-
mount NestedNamespaceWithDescAPI
|
664
|
-
|
665
|
-
add_swagger_documentation format: :json
|
666
|
-
end
|
667
|
-
get '/swagger_doc'
|
668
|
-
end
|
669
|
-
|
670
|
-
def app
|
671
|
-
NamespaceWithDescAPI
|
672
|
-
end
|
673
|
-
|
674
|
-
subject do
|
675
|
-
JSON.parse(last_response.body)['apis']
|
676
|
-
end
|
677
|
-
|
678
|
-
it 'shows the namespace description in the json spec' do
|
679
|
-
expect(subject[0]['description']).to eql('Description for aspace')
|
680
|
-
end
|
681
|
-
|
682
|
-
it 'shows the nested namespace description in the json spec' do
|
683
|
-
expect(subject[1]['description']).to eql('Description for nested space')
|
684
|
-
end
|
685
|
-
end
|
686
|
-
|
687
|
-
context 'override nickname' do
|
688
|
-
before :all do
|
689
|
-
class NicknameAPI < Grape::API
|
690
|
-
desc 'This gets something.', nickname: 'getSomething'
|
691
|
-
get '/something' do
|
692
|
-
{ bla: 'something' }
|
693
|
-
end
|
694
|
-
add_swagger_documentation
|
695
|
-
end
|
696
|
-
end
|
697
|
-
|
698
|
-
def app
|
699
|
-
NicknameAPI
|
700
|
-
end
|
701
|
-
|
702
|
-
it 'documents the user-specified nickname' do
|
703
|
-
get '/swagger_doc/something.json'
|
704
|
-
ret = JSON.parse(last_response.body)
|
705
|
-
expect(ret['apis'][0]['operations'][0]['nickname']).to eq 'getSomething'
|
706
|
-
end
|
707
|
-
end
|
708
|
-
|
709
|
-
context 'invalid name' do
|
710
|
-
subject do
|
711
|
-
Class.new(Grape::API) do
|
712
|
-
get 'hidden', hidden: true
|
713
|
-
add_swagger_documentation
|
714
|
-
end
|
715
|
-
end
|
716
|
-
|
717
|
-
def app
|
718
|
-
subject
|
719
|
-
end
|
720
|
-
|
721
|
-
it 'returns a 404 for an non-existent route' do
|
722
|
-
get '/swagger_doc/invalid.json'
|
723
|
-
expect(last_response.status).to eq 404
|
724
|
-
expect(JSON.parse(last_response.body)).to eq('error' => 'Not Found')
|
725
|
-
end
|
726
|
-
|
727
|
-
it 'returns a 404 for a hidden route' do
|
728
|
-
get '/swagger_doc/hidden.json'
|
729
|
-
expect(last_response.status).to eq 404
|
730
|
-
expect(JSON.parse(last_response.body)).to eq('error' => 'Not Found')
|
731
|
-
end
|
732
|
-
end
|
733
|
-
end
|