-
Notifications
You must be signed in to change notification settings - Fork 98
/
swagger_v2_blocks_spec.rb
337 lines (321 loc) · 8.84 KB
/
swagger_v2_blocks_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
require 'json'
require 'swagger/blocks'
# TODO Test data originally based on the Swagger UI example data
RESOURCE_LISTING_JSON_V2 = open(File.expand_path('../swagger_v2_api_declaration.json', __FILE__)).read
class PetControllerV2
include Swagger::Blocks
swagger_root host: 'petstore.swagger.wordnik.com' do
key :swagger, '2.0'
info version: '1.0.0' do
key :title, 'Swagger Petstore'
key :description, 'A sample API that uses a petstore as an example to ' \
'demonstrate features in the swagger-2.0 specification'
key :termsOfService, 'http://helloreverb.com/terms/'
contact do
key :name, 'Wordnik API Team'
end
license do
key :name, 'MIT'
end
end
key :basePath, '/api'
key :schemes, ['http']
key :consumes, ['application/json']
key :produces, ['application/json']
security_definition :api_key, type: :apiKey do
key :name, :api_key
key :in, :header
end
security_definition :petstore_auth do
key :type, :oauth2
key :authorizationUrl, 'http://swagger.io/api/oauth/dialog'
key :flow, :implicit
scopes 'write:pets' => 'modify pets in your account' do
key 'read:pets', 'read your pets'
end
end
externalDocs description: 'Find more info here' do
key :url, 'https://swagger.io'
end
tag name: 'pet' do
key :description, 'Pets operations'
externalDocs description: 'Find more info here' do
key :url, 'https://swagger.io'
end
end
parameter :species do
key :name, :species
key :in, :body
key :description, 'Species of this pet'
key :type, :string
end
end
swagger_path '/pets' do
operation :get do
key :description, 'Returns all pets from the system that the user has access to'
key :operationId, 'findPets'
key :produces, [
'application/json',
'application/xml',
'text/xml',
'text/html',
]
parameter do
key :name, :tags
key :in, :query
key :description, 'tags to filter by'
key :required, false
key :type, :array
items do
key :type, :string
end
key :collectionFormat, :csv
end
parameter do
key :name, :limit
key :in, :query
key :description, 'maximum number of results to return'
key :required, false
key :type, :integer
key :format, :int32
end
response 200 do
key :description, 'pet response'
schema type: :array do
items do
key :'$ref', :Pet
end
end
end
response :default do
key :description, 'unexpected error'
schema do
key :'$ref', :ErrorModel
end
end
end
operation :post do
key :description, 'Creates a new pet in the store. Duplicates are allowed'
key :operationId, 'addPet'
key :produces, [
'application/json'
]
parameter do
key :name, :pet
key :in, :body
key :description, 'Pet to add to the store'
key :required, true
schema do
key :'$ref', :PetInput
end
end
parameter :species
response 200 do
key :description, 'pet response'
schema do
# Wrong form here, but checks that #/ strings are not transformed.
key :'$ref', '#/parameters/Pet'
end
end
response :default, description: 'unexpected error' do
schema do
key :'$ref', 'http://example.com/schema.json#/definitions/ErrorModel'
end
end
end
end
swagger_path '/pets/{id}' do
parameter do
key :name, :id
key :in, :path
key :description, 'ID of pet'
key :required, true
key :type, :integer
key :format, :int64
end
operation :put do
key :description, 'Update a pet in the store.'
key :operationId, 'updatePet'
key :produces, [
'application/json'
]
parameter do
key :name, :pet
key :in, :body
key :description, 'Pet to update in the store'
key :required, true
schema do
key :'$ref', :PetInput
end
end
parameter :species
response 200 do
key :description, 'pet response'
schema do
# Wrong form here, but checks that #/ strings are not transformed.
key :'$ref', '#/parameters/Pet'
end
end
response :default, description: 'unexpected error' do
schema do
key :'$ref', 'http://example.com/schema.json#/definitions/ErrorModel'
end
end
end
operation :get do
key :description, 'Returns a user based on a single ID, if the user does not have access to the pet'
key :operationId, 'findPetById'
key :produces, [
'application/json',
'application/xml',
'text/xml',
'text/html',
]
response 200 do
key :description, 'pet response'
schema do
key :'$ref', :Pet
end
end
response :default do
key :description, 'unexpected error'
schema do
key :'$ref', :ErrorModel
end
end
security api_key: []
security do
key :petstore_auth, ['write:pets', 'read:pets']
end
end
operation :delete do
key :description, 'deletes a single pet based on the ID supplied'
key :operationId, 'deletePet'
response 204 do
key :description, 'pet deleted'
end
response :default do
key :description, 'unexpected error'
schema do
key :'$ref', :ErrorModel
end
end
end
end
end
class PetV2
include Swagger::Blocks
swagger_schema :Pet, required: [:id, :name] do
property :id do
key :type, :integer
key :format, :int64
end
property :name do
key :type, :string
end
property :colors do
key :type, :array
items do
key :type, :string
end
end
end
swagger_schema :PetInput do
allOf do
schema do
key :'$ref', :Pet
end
schema do
key :required, [:name]
property :id do
key :type, :integer
key :format, :int64
end
property :name do
key :type, :string
end
property :nestedObject do
key :type, :object
property :name do
key :type, :string
end
end
property :arrayOfObjects do
key :type, :array
items do
key :type, :object
property :name do
key :type, :string
end
property :age do
key :type, :integer
end
end
end
property :arrayOfArrays do
key :type, :array
items do
key :type, :array
items do
key :type, :integer
end
end
end
end
end
end
end
class ErrorModelV2
include Swagger::Blocks
swagger_schema :ErrorModel do
key :required, [:code, :message]
property :code do
key :type, :integer
key :format, :int32
end
property :message do
key :type, :string
end
end
end
describe 'Swagger::Blocks v2' do
describe 'build_json' do
it 'outputs the correct data' do
swaggered_classes = [
PetControllerV2,
PetV2,
ErrorModelV2
]
actual = Swagger::Blocks.build_root_json(swaggered_classes)
actual = JSON.parse(actual.to_json) # For access consistency.
data = JSON.parse(RESOURCE_LISTING_JSON_V2)
# Multiple expectations for better test diff output.
expect(actual['info']).to eq(data['info'])
expect(actual['paths']).to be
expect(actual['paths']['/pets']).to be
expect(actual['paths']['/pets']).to eq(data['paths']['/pets'])
expect(actual['paths']['/pets/{id}']).to be
expect(actual['paths']['/pets/{id}']['get']).to be
expect(actual['paths']['/pets/{id}']['get']).to eq(data['paths']['/pets/{id}']['get'])
expect(actual['paths']).to eq(data['paths'])
expect(actual['definitions']).to eq(data['definitions'])
expect(actual).to eq(data)
end
it 'is idempotent' do
swaggered_classes = [PetControllerV2, PetV2, ErrorModelV2]
actual = JSON.parse(Swagger::Blocks.build_root_json(swaggered_classes).to_json)
data = JSON.parse(RESOURCE_LISTING_JSON_V2)
expect(actual).to eq(data)
end
it 'errors if no swagger_root is declared' do
expect {
Swagger::Blocks.build_root_json([])
}.to raise_error(Swagger::Blocks::DeclarationError)
end
it 'errors if multiple swagger_roots are declared' do
expect {
Swagger::Blocks.build_root_json([PetControllerV2, PetControllerV2])
}.to raise_error(Swagger::Blocks::DeclarationError)
end
end
end