swagger-blocks 2.0.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +5 -5
  2. data/.github/ISSUE_TEMPLATE.md +4 -0
  3. data/lib/swagger/blocks.rb +14 -0
  4. data/lib/swagger/blocks/class_methods.rb +16 -2
  5. data/lib/swagger/blocks/internal_helpers.rb +25 -0
  6. data/lib/swagger/blocks/node.rb +48 -10
  7. data/lib/swagger/blocks/nodes/all_of_node.rb +6 -5
  8. data/lib/swagger/blocks/nodes/callback_destination_node.rb +11 -0
  9. data/lib/swagger/blocks/nodes/callback_method_node.rb +16 -0
  10. data/lib/swagger/blocks/nodes/callback_node.rb +11 -0
  11. data/lib/swagger/blocks/nodes/component_node.rb +50 -0
  12. data/lib/swagger/blocks/nodes/content_node.rb +20 -0
  13. data/lib/swagger/blocks/nodes/example_node.rb +3 -1
  14. data/lib/swagger/blocks/nodes/flow_node.rb +11 -0
  15. data/lib/swagger/blocks/nodes/header_node.rb +4 -0
  16. data/lib/swagger/blocks/nodes/items_node.rb +1 -1
  17. data/lib/swagger/blocks/nodes/link_node.rb +11 -0
  18. data/lib/swagger/blocks/nodes/link_parameter_node.rb +8 -0
  19. data/lib/swagger/blocks/nodes/one_of_node.rb +11 -0
  20. data/lib/swagger/blocks/nodes/operation_node.rb +15 -0
  21. data/lib/swagger/blocks/nodes/parameter_node.rb +5 -0
  22. data/lib/swagger/blocks/nodes/path_node.rb +6 -0
  23. data/lib/swagger/blocks/nodes/property_node.rb +5 -0
  24. data/lib/swagger/blocks/nodes/request_body_node.rb +12 -0
  25. data/lib/swagger/blocks/nodes/response_node.rb +17 -4
  26. data/lib/swagger/blocks/nodes/root_node.rb +16 -2
  27. data/lib/swagger/blocks/nodes/schema_node.rb +9 -0
  28. data/lib/swagger/blocks/nodes/scopes_node.rb +0 -1
  29. data/lib/swagger/blocks/nodes/security_scheme_node.rb +5 -1
  30. data/lib/swagger/blocks/nodes/server_node.rb +12 -0
  31. data/lib/swagger/blocks/nodes/value_node.rb +8 -0
  32. data/lib/swagger/blocks/nodes/variable_node.rb +8 -0
  33. data/lib/swagger/blocks/nodes/vendor_extension_node.rb +9 -0
  34. data/lib/swagger/blocks/root.rb +8 -1
  35. data/lib/swagger/blocks/version.rb +1 -1
  36. data/spec/lib/swagger_v2_blocks_spec.rb +1 -1
  37. data/spec/lib/swagger_v3_api_declaration.json +592 -0
  38. data/spec/lib/swagger_v3_blocks_spec.rb +555 -0
  39. metadata +22 -4
@@ -0,0 +1,555 @@
1
+ require 'json'
2
+ require 'swagger/blocks'
3
+
4
+ # TODO: Test data originally based on the Swagger UI example data
5
+
6
+ RESOURCE_LISTING_JSON_V3 = open(File.expand_path('../swagger_v3_api_declaration.json', __FILE__)).read
7
+
8
+ class PetControllerV3
9
+ include Swagger::Blocks
10
+
11
+ swagger_root do
12
+ key :openapi, '3.0.0'
13
+ info version: '1.0.1' do
14
+ key :title, 'Swagger Petstore'
15
+ key :description, 'A sample API that uses a petstore as an example to ' \
16
+ 'demonstrate features in the swagger-2.0 specification'
17
+ key :termsOfService, 'http://helloreverb.com/terms/'
18
+ contact do
19
+ key :name, 'Wordnik API Team'
20
+ end
21
+ license do
22
+ key :name, 'MIT'
23
+ end
24
+ end
25
+
26
+ server do
27
+ key :url, 'http://petstore.swagger.io/v1'
28
+ key :description, 'Petstore API'
29
+ end
30
+
31
+ server do
32
+ key :url, 'https://{subdomain}.site.com/{version}'
33
+ key :description, 'The main prod server'
34
+
35
+ variable :subdomain do
36
+ key :default, :production
37
+ end
38
+ variable :version do
39
+ key :enum, ['v1', 'v2']
40
+ key :default, :v2
41
+ end
42
+ end
43
+
44
+ security do
45
+ key :ApiKeyAuth, []
46
+ end
47
+ security do
48
+ key :OAuth2, ['read', 'write']
49
+ end
50
+
51
+ extension :'x-tagGroups' do
52
+ key :name, 'Pets'
53
+ key :tags, ['dogs', 'cats']
54
+ end
55
+
56
+ tag do
57
+ key :name, 'dogs'
58
+ key :description, 'Dogs'
59
+ end
60
+
61
+ tag do
62
+ key :name, 'cats'
63
+ key :description, 'Cats'
64
+ end
65
+ end
66
+
67
+ swagger_path '/pets' do
68
+ key :description, 'Perform actions on pet resources'
69
+ server do
70
+ key :url, 'http://petstore.swagger.io/'
71
+ key :description, 'Petstore API (without version prefix)'
72
+ end
73
+ operation :get do
74
+ key :summary, 'List all pets'
75
+ key :operationId, 'listPets'
76
+ key :tags, [
77
+ 'pets'
78
+ ]
79
+ parameter do
80
+ key :name, :limit
81
+ key :in, :query
82
+ key :description, 'How many items to return at one time (max 100)'
83
+ key :required, false
84
+ schema do
85
+ key :type, :integer
86
+ key :format, :int32
87
+ end
88
+ example :large do
89
+ key :value, 100
90
+ key :summary, 'Return a maximum of 100 results'
91
+ end
92
+ example :small do
93
+ key :value, 5
94
+ key :summary, 'Return a maximum of 5 results'
95
+ end
96
+ end
97
+ server do
98
+ key :url, 'http://petstore.swagger.io/2.1/'
99
+ key :description, 'Petstore API (with version 2.1 prefix)'
100
+ end
101
+ response 200 do
102
+ key :description, 'A paged array of pets'
103
+ header :'x-next' do
104
+ key :description, 'A link to the next page of responses'
105
+ schema do
106
+ key :type, :string
107
+ end
108
+ end
109
+ content :'application/json' do
110
+ schema do
111
+ key :'$ref', :Pets
112
+ end
113
+ example :Rabbit do
114
+ value do
115
+ key :id, 10
116
+ key :name, 'Rabbit'
117
+ end
118
+ end
119
+ example :Cat do
120
+ key :'$ref', :Cat
121
+ end
122
+ end
123
+ link :getPetById do
124
+ key :'$ref', '#/components/links/GetPetById'
125
+ end
126
+ end
127
+ response :default do
128
+ key :description, 'unexpected error'
129
+ content :'application/json' do
130
+ schema do
131
+ key :'$ref', :Error
132
+ end
133
+ end
134
+ end
135
+ end
136
+ operation :post do
137
+ key :summary, 'Create a pet'
138
+ key :operationId, 'createPets'
139
+ key :tags, [
140
+ 'pets'
141
+ ]
142
+ request_body do
143
+ key :description, 'Pet to add to the store'
144
+ key :required, true
145
+ content 'application/json' do
146
+ schema do
147
+ property :name do
148
+ key :type, :string
149
+ end
150
+ example do
151
+ key :name, 'Fluffy'
152
+ end
153
+ end
154
+ end
155
+ end
156
+ response 201 do
157
+ key :description, 'New Pet'
158
+ content :'application/json' do
159
+ schema do
160
+ key :'$ref', :Pet
161
+ end
162
+ end
163
+ link :getPetById do
164
+ key :operationId, 'showPetById'
165
+ parameters do
166
+ key :id, '$response.body#/id'
167
+ end
168
+ key :description, 'The `id` value returned in the response can be used as the `petId` parameter in `GET /pets/{petId}`.'
169
+ end
170
+ end
171
+ response :default, description: 'unexpected error' do
172
+ content :'application/json' do
173
+ schema do
174
+ key :'$ref', :Error
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
180
+
181
+ swagger_path '/pets/{petId}' do
182
+ parameter do
183
+ key :name, :petId
184
+ key :in, :path
185
+ key :required, true
186
+ key :description, 'The id of the pet to retrieve'
187
+ schema do
188
+ key :type, 'string'
189
+ end
190
+ end
191
+ operation :get do
192
+ key :summary, 'Info for a specific pet'
193
+ key :operationId, 'showPetById'
194
+ key :tags, [
195
+ 'pets'
196
+ ]
197
+ response 200 do
198
+ key :description, 'Expected response to a valid request'
199
+ content :'application/json' do
200
+ schema do
201
+ key :'$ref', :Pet
202
+ end
203
+ end
204
+ end
205
+ response :default do
206
+ key :description, 'unexpected error'
207
+ content :'application/json' do
208
+ schema do
209
+ key :'$ref', :Error
210
+ end
211
+ end
212
+ end
213
+ end
214
+ operation :post do
215
+ key :summary, 'Update info for a specific pet'
216
+ key :operationId, 'updatePetById'
217
+ key :tags, [
218
+ 'pets'
219
+ ]
220
+ request_body do
221
+ key :"$ref", :PetBody
222
+ end
223
+ response 200 do
224
+ key :'$ref', :UpdatePetBodyResponse
225
+ end
226
+ response :default do
227
+ key :description, 'unexpected error'
228
+ content :'application/json' do
229
+ schema do
230
+ key :'$ref', :Error
231
+ end
232
+ end
233
+ end
234
+ end
235
+ operation :put do
236
+ key :summary, 'Replace info for a specific pet'
237
+ key :operationId, 'replacePetById'
238
+ key :tags, [
239
+ 'pets'
240
+ ]
241
+ request_body do
242
+ key :"$ref", :PetBody
243
+ end
244
+ response 200 do
245
+ key :'$ref', :ReplacePetBodyResponse
246
+ end
247
+ response :default do
248
+ key :description, 'unexpected error'
249
+ content :'application/json' do
250
+ schema do
251
+ key :'$ref', :Error
252
+ end
253
+ end
254
+ end
255
+ end
256
+ end
257
+ end
258
+
259
+ class PetV3
260
+ include Swagger::Blocks
261
+
262
+ swagger_path '/pets/{petId}/purchase' do
263
+ operation :post do
264
+ key :summary, 'Purchase a specific pet'
265
+ key :operationId, 'purchasePetById'
266
+ key :deprecated, true
267
+ key :tags, [
268
+ 'pets'
269
+ ]
270
+ parameter do
271
+ key :$ref, :petId
272
+ end
273
+ request_body do
274
+ key :description, 'Pet order object'
275
+ key :required, true
276
+ content 'application/json' do
277
+ schema do
278
+ one_of do
279
+ key :'$ref', 'PetOrderRequest'
280
+ end
281
+ one_of do
282
+ key :'$ref', 'ComplexPetOrderRequest'
283
+ end
284
+ end
285
+ example do
286
+ key :id, 10
287
+ key :name, 'Fluffy'
288
+ end
289
+ end
290
+ end
291
+ response 201 do
292
+ key :description, 'Expected response to a valid request'
293
+ content :'application/json' do
294
+ schema do
295
+ key :'$ref', :PetOrder
296
+ end
297
+ end
298
+ end
299
+ response :default do
300
+ key :description, 'unexpected error'
301
+ content :'application/json' do
302
+ schema do
303
+ key :'$ref', :Error
304
+ end
305
+ end
306
+ end
307
+
308
+ callback :orderUpdated do
309
+ destination '{$request.body#/webhook_url}' do
310
+ method :post do
311
+ request_body do
312
+ key :required, true
313
+ content 'application/json' do
314
+ schema do
315
+ key :'$ref', :OrderUpdated
316
+ end
317
+ end
318
+ end
319
+ response 200 do
320
+ key :description, 'The server must return an HTTP 200, otherwise delivery will be reattempted.'
321
+ end
322
+ end
323
+ end
324
+ end
325
+ end
326
+ end
327
+ swagger_component do
328
+ schema :Pet, required: [:id, :name] do
329
+ property :id do
330
+ key :type, :integer
331
+ key :format, :int64
332
+ end
333
+ property :name do
334
+ key :type, :string
335
+ end
336
+ property :tag_ids do
337
+ key :type, :array
338
+ items do
339
+ key :type, :integer
340
+ key :format, :int64
341
+ key :example, 1
342
+ end
343
+ key :example, [1, 2, 3]
344
+ end
345
+ end
346
+
347
+ schema :Pets do
348
+ key :type, :array
349
+ items do
350
+ key :'$ref', :Pet
351
+ end
352
+ key :example, [{ id: 10, name: 'Rover' }, { id: 20, name: 'Felicity' }]
353
+ end
354
+
355
+ schema :PetOrderRequest, required: [:phone_number] do
356
+ property :phone_number do
357
+ key :type, :string
358
+ end
359
+ property :webhook_url do
360
+ key :type, :string
361
+ end
362
+ end
363
+
364
+ schema :PetOrder, required: [:phone_number, :id, :status] do
365
+ property :id do
366
+ key :type, :integer
367
+ key :format, :int64
368
+ end
369
+ property :phone_number do
370
+ key :type, :string
371
+ end
372
+ property :webhook_url do
373
+ key :type, :string
374
+ end
375
+ property :status do
376
+ key :type, :string
377
+ end
378
+ end
379
+
380
+ schema :OrderUpdated, required: [:order_id, :status, :phone_number] do
381
+ property :order_id do
382
+ key :type, :integer
383
+ key :format, :int64
384
+ end
385
+ property :phone_number do
386
+ key :type, :string
387
+ end
388
+ property :status do
389
+ key :type, :string
390
+ end
391
+ example do
392
+ key :order_id, 123
393
+ key :phone_number, '3125556666'
394
+ key :status, 'complete'
395
+ end
396
+ end
397
+
398
+ link :GetPetById do
399
+ key :operationId, :showPetById
400
+ parameters do
401
+ key :petId, '$response.body#/id'
402
+ end
403
+ end
404
+
405
+ example :PetExample do
406
+ value do
407
+ key :id, 1
408
+ key :name, 'Rover'
409
+ end
410
+ key :summary, 'An example pet response'
411
+ end
412
+ security_scheme :BasicAuth do
413
+ key :type, :http
414
+ key :scheme, :basic
415
+ end
416
+ security_scheme :BearerAuth do
417
+ key :type, :http
418
+ key :scheme, :bearer
419
+ end
420
+ security_scheme :ApiKeyAuth do
421
+ key :type, :apiKey
422
+ key :in, :header
423
+ key :name, :"X-API-Key"
424
+ end
425
+ security_scheme :OpenID do
426
+ key :type, :openIdConnect
427
+ key :openIdConnectUrl, 'https://example.com/.well-known/openid-configuration'
428
+ end
429
+ parameter :petId do
430
+ key :name, :petId
431
+ key :in, :path
432
+ key :required, true
433
+ key :description, 'The id of the pet to retrieve'
434
+ schema do
435
+ key :type, 'string'
436
+ end
437
+ end
438
+ request_body :PetBody do
439
+ key :description, 'A JSON object containing pet information'
440
+ key :required, true
441
+ content :'application/json' do
442
+ schema do
443
+ key :'$ref', :Pet
444
+ end
445
+ end
446
+ end
447
+ response :ReplacePetBodyResponse do
448
+ key :description, 'Expected response to a valid request'
449
+ content :'application/json' do
450
+ schema do
451
+ key :'$ref', :Pet
452
+ end
453
+ end
454
+ end
455
+ end
456
+ end
457
+
458
+ class ErrorModelV3
459
+ include Swagger::Blocks
460
+
461
+ swagger_component do
462
+ schema :Error do
463
+ key :required, [:code, :message]
464
+ property :code do
465
+ key :type, :integer
466
+ key :format, :int32
467
+ end
468
+ property :message do
469
+ key :type, :string
470
+ end
471
+ end
472
+ end
473
+ end
474
+
475
+ class AuxiliaryModelV3
476
+ include Swagger::Blocks
477
+
478
+ swagger_component do
479
+ response :UpdatePetBodyResponse do
480
+ key :description, 'Expected response to a valid request'
481
+ content :'application/json' do
482
+ schema do
483
+ key :'$ref', :Pet
484
+ end
485
+ end
486
+ end
487
+ example :Cat do
488
+ value do
489
+ key :id, 1
490
+ key :name, 'Felicity'
491
+ end
492
+ key :summary, 'An example cat response'
493
+ end
494
+ security_scheme :OAuth2 do
495
+ key :type, :oauth2
496
+ flow :authorizationCode do
497
+ key :authorizationUrl, 'https://example.com/oauth/authorize'
498
+ key :tokenUrl, 'https://example.com/oauth/token'
499
+ scopes do
500
+ key :read, 'Grants read access'
501
+ key :write, 'Grants write access'
502
+ key :admin, 'Grants access to admin operations'
503
+ end
504
+ end
505
+ end
506
+ end
507
+ end
508
+
509
+ describe 'Swagger::Blocks v3' do
510
+ describe 'build_json' do
511
+ it 'outputs the correct data' do
512
+ swaggered_classes = [
513
+ PetControllerV3,
514
+ PetV3,
515
+ ErrorModelV3,
516
+ AuxiliaryModelV3
517
+ ]
518
+ actual = Swagger::Blocks.build_root_json(swaggered_classes)
519
+ actual = JSON.parse(actual.to_json) # For access consistency.
520
+ data = JSON.parse(RESOURCE_LISTING_JSON_V3)
521
+
522
+ # Multiple expectations for better test diff output.
523
+ expect(actual['info']).to eq(data['info'])
524
+ expect(actual['x-tagGroups']).to eq(data['x-tagGroups'])
525
+ expect(actual['paths']).to be
526
+ expect(actual['paths']['/pets']).to be
527
+ expect(actual['paths']['/pets']).to eq(data['paths']['/pets'])
528
+ expect(actual['paths']['/pets/{petId}']).to be
529
+ expect(actual['paths']['/pets/{petId}']['get']).to be
530
+ expect(actual['paths']['/pets/{petId}']['get']).to eq(data['paths']['/pets/{petId}']['get'])
531
+ expect(actual['paths']).to eq(data['paths'])
532
+ expect(actual['components']).to eq(data['components'])
533
+ expect(actual).to eq(data)
534
+ end
535
+
536
+ it 'is idempotent' do
537
+ swaggered_classes = [PetControllerV3, PetV3, ErrorModelV3, AuxiliaryModelV3]
538
+ actual = JSON.parse(Swagger::Blocks.build_root_json(swaggered_classes).to_json)
539
+ data = JSON.parse(RESOURCE_LISTING_JSON_V3)
540
+ expect(actual).to eq(data)
541
+ end
542
+
543
+ it 'errors if no swagger_root is declared' do
544
+ expect do
545
+ Swagger::Blocks.build_root_json([])
546
+ end.to raise_error(Swagger::Blocks::DeclarationError)
547
+ end
548
+
549
+ it 'errors if multiple swagger_roots are declared' do
550
+ expect do
551
+ Swagger::Blocks.build_root_json([PetControllerV3, PetControllerV3])
552
+ end.to raise_error(Swagger::Blocks::DeclarationError)
553
+ end
554
+ end
555
+ end