swagger-blocks 1.4.0 → 2.0.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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -6
  3. data/lib/swagger/blocks.rb +28 -775
  4. data/lib/swagger/blocks/class_methods.rb +65 -0
  5. data/lib/swagger/blocks/errors.rb +8 -0
  6. data/lib/swagger/blocks/internal_helpers.rb +52 -0
  7. data/lib/swagger/blocks/node.rb +64 -0
  8. data/lib/swagger/blocks/nodes/all_of_node.rb +42 -0
  9. data/lib/swagger/blocks/nodes/contact_node.rb +9 -0
  10. data/lib/swagger/blocks/nodes/example_node.rb +9 -0
  11. data/lib/swagger/blocks/nodes/external_docs_node.rb +9 -0
  12. data/lib/swagger/blocks/nodes/header_node.rb +12 -0
  13. data/lib/swagger/blocks/nodes/info_node.rb +16 -0
  14. data/lib/swagger/blocks/nodes/items_node.rb +14 -0
  15. data/lib/swagger/blocks/nodes/license_node.rb +9 -0
  16. data/lib/swagger/blocks/nodes/operation_node.rb +30 -0
  17. data/lib/swagger/blocks/nodes/parameter_node.rb +16 -0
  18. data/lib/swagger/blocks/nodes/path_node.rb +24 -0
  19. data/lib/swagger/blocks/nodes/properties_node.rb +11 -0
  20. data/lib/swagger/blocks/nodes/property_node.rb +17 -0
  21. data/lib/swagger/blocks/nodes/response_node.rb +24 -0
  22. data/lib/swagger/blocks/nodes/root_node.rb +53 -0
  23. data/lib/swagger/blocks/nodes/schema_node.rb +30 -0
  24. data/lib/swagger/blocks/nodes/scopes_node.rb +9 -0
  25. data/lib/swagger/blocks/nodes/security_requirement_node.rb +9 -0
  26. data/lib/swagger/blocks/nodes/security_scheme_node.rb +14 -0
  27. data/lib/swagger/blocks/nodes/tag_node.rb +15 -0
  28. data/lib/swagger/blocks/nodes/xml_node.rb +9 -0
  29. data/lib/swagger/blocks/root.rb +25 -0
  30. data/lib/swagger/blocks/version.rb +1 -1
  31. data/spec/lib/swagger_v2_blocks_spec.rb +0 -5
  32. metadata +28 -9
  33. data/README_v1_2.md +0 -143
  34. data/spec/lib/swagger_api_declaration.json +0 -201
  35. data/spec/lib/swagger_blocks_spec.rb +0 -349
  36. data/spec/lib/swagger_resource_listing.json +0 -60
@@ -1,349 +0,0 @@
1
- require 'json'
2
- require 'swagger/blocks'
3
-
4
- # Test data originally based on the Swagger UI example data:
5
- # https://github.com/wordnik/swagger-codegen/blob/master/src/test/resources/petstore-1.2/api-docs
6
- RESOURCE_LISTING_JSON = open(File.expand_path('../swagger_resource_listing.json', __FILE__)).read
7
- # https://github.com/wordnik/swagger-codegen/blob/master/src/test/resources/petstore-1.2/pet
8
- API_DECLARATION_JSON = open(File.expand_path('../swagger_api_declaration.json', __FILE__)).read
9
-
10
- class PetController
11
- include Swagger::Blocks
12
-
13
- swagger_root swaggerVersion: '1.2'do
14
- key :apiVersion, '1.0.0'
15
- info title: 'Swagger Sample App' do
16
- key :description, "This is a sample server Petstore server. You can find out more about Swagger \n at <a href=\"http://swagger.wordnik.com\">http://swagger.wordnik.com</a> or on irc.freenode.net, #swagger. For this sample,\n you can use the api key \"special-key\" to test the authorization filters"
17
- key :termsOfServiceUrl, 'http://helloreverb.com/terms/'
18
- keys contact: 'apiteam@wordnik.com',
19
- license: 'Apache 2.0',
20
- licenseUrl: 'http://www.apache.org/licenses/LICENSE-2.0.html'
21
- end
22
- api path: '/pet', description: 'Operations about pets'
23
- api do
24
- key :path, '/user'
25
- key :description, 'Operations about user'
26
- end
27
- api do
28
- key :path, '/store'
29
- key :description, 'Operations about store'
30
- end
31
- authorization :oauth2, type: 'oauth2' do
32
- scope scope: 'email', description: 'Access to your email address'
33
- scope do
34
- key :scope, 'pets'
35
- key :description, 'Access to your pets'
36
- end
37
- grant_type :implicit, tokenName: 'access_token' do
38
- login_endpoint do
39
- key :url, 'http://petstore.swagger.wordnik.com/oauth/dialog'
40
- end
41
- end
42
- grant_type :authorization_code do
43
- token_request_endpoint clientSecretName: 'client_secret' do
44
- key :url, 'http://petstore.swagger.wordnik.com/oauth/requestToken'
45
- key :clientIdName, 'client_id'
46
- end
47
- token_endpoint tokenName: 'access_code' do
48
- key :url, 'http://petstore.swagger.wordnik.com/oauth/token'
49
- end
50
- end
51
- end
52
- end
53
-
54
- # All swagger_api_root declarations with the same key will be merged.
55
- swagger_api_root :pets, swaggerVersion: '1.2' do
56
- key :apiVersion, '1.0.0'
57
- key :basePath, 'http://petstore.swagger.wordnik.com/api'
58
- key :resourcePath, '/pet'
59
- key :produces, [
60
- 'application/json',
61
- 'application/xml',
62
- 'text/plain',
63
- 'text/html',
64
- ]
65
- api do
66
- key :path, '/pet/{petId}'
67
- operation do
68
- key :method, 'GET'
69
- key :summary, 'Find pet by ID'
70
- key :notes, 'Returns a pet based on ID'
71
- key :type, :Pet
72
- key :nickname, :getPetById
73
- parameter do
74
- key :paramType, :path
75
- key :name, :petId
76
- key :description, 'ID of pet that needs to be fetched'
77
- key :required, true
78
- key :type, :integer
79
- key :format, :int64
80
- key :minimum, '1.0'
81
- key :maximum, '100000.0'
82
- end
83
- response_message do
84
- key :code, 400
85
- key :message, 'Invalid ID supplied'
86
- end
87
- response_message do
88
- key :code, 404
89
- key :message, 'Pet not found'
90
- end
91
- end
92
- end
93
- end
94
-
95
- swagger_api_root :pets do
96
- api do
97
- key :path, '/pet/{petId}'
98
- operation do
99
- key :method, 'PATCH'
100
- key :summary, 'partial updates to a pet'
101
- key :notes, ''
102
- key :type, :array
103
- key :nickname, :partialUpdate
104
- items do
105
- key :'$ref', :Pet
106
- end
107
- key :produces, [
108
- 'application/json',
109
- 'application/xml',
110
- ]
111
- key :consumes, [
112
- 'application/json',
113
- 'application/xml',
114
- ]
115
- authorization :oauth2 do
116
- scope do
117
- key :scope, 'test:anything'
118
- key :description, 'anything'
119
- end
120
- end
121
- parameter paramType: :path do
122
- key :name, :petId
123
- key :description, 'ID of pet that needs to be fetched'
124
- key :required, true
125
- key :type, :string
126
- end
127
- parameter do
128
- key :paramType, :body
129
- key :name, :body
130
- key :description, 'Pet object that needs to be added to the store'
131
- key :required, true
132
- key :type, :Pet
133
- end
134
- response_message code: 400 do
135
- key :message, 'Invalid tag value'
136
- end
137
- end
138
- end
139
- end
140
-
141
- swagger_api_root :pets do
142
- api do
143
- key :path, '/pet/findByStatus'
144
- operation method: 'GET' do
145
- key :summary, 'Finds Pets by status'
146
- key :notes, 'Multiple status values can be provided with comma seperated strings'
147
- key :type, :array
148
- key :nickname, :findPetsByStatus
149
- items :'$ref' => :Pet
150
- parameter do
151
- key :paramType, :query
152
- key :name, :status
153
- key :description, 'Status values that need to be considered for filter'
154
- key :defaultValue, 'available'
155
- key :required, true
156
- key :type, :string
157
- key :enum, [
158
- 'available',
159
- 'pending',
160
- 'sold',
161
- ]
162
- end
163
- response_message code: 400, message: 'Invalid status value'
164
- end
165
- end
166
- end
167
- end
168
-
169
-
170
- class StoreController
171
- include Swagger::Blocks
172
-
173
- swagger_api_root :stores do
174
- key :path, '/store'
175
- end
176
- end
177
-
178
-
179
- class UserController
180
- include Swagger::Blocks
181
-
182
- swagger_api_root :users do
183
- key :path, '/user'
184
- end
185
- end
186
-
187
-
188
- class TagModel
189
- include Swagger::Blocks
190
-
191
- swagger_model :Tag do
192
- key :id, :Tag
193
- property :id do
194
- key :type, :integer
195
- key :format, :int64
196
- end
197
- property :name do
198
- key :type, :string
199
- end
200
- end
201
- end
202
-
203
-
204
- class OtherModelsContainer
205
- include Swagger::Blocks
206
-
207
- swagger_model :Pet, id: :Pet do
208
- key :required, [:id, :name]
209
- property :id do
210
- key :type, :integer
211
- key :format, :int64
212
- key :description, 'unique identifier for the pet'
213
- key :minimum, '0.0'
214
- key :maximum, '100.0'
215
- end
216
- property :category do
217
- key :'$ref', :Category
218
- end
219
- property :name do
220
- key :type, :string
221
- end
222
- property :photoUrls do
223
- key :type, :array
224
- items do
225
- key :type, :string
226
- end
227
- end
228
- property :tags do
229
- key :type, :array
230
- items do
231
- key :'$ref', :Tag
232
- end
233
- end
234
- property :status do
235
- key :type, :string
236
- key :description, 'pet status in the store'
237
- key :enum, [:available, :pending, :sold]
238
- end
239
- end
240
-
241
- swagger_model :Category do
242
- key :id, :Category
243
- property :id do
244
- key :type, :integer
245
- key :format, :int64
246
- end
247
- property :name do
248
- key :type, :string
249
- end
250
- end
251
- end
252
-
253
-
254
- class BlankController; end
255
-
256
-
257
- describe 'Swagger::Blocks v1' do
258
- describe 'build_root_json' do
259
- it 'outputs the correct data' do
260
- swaggered_classes = [
261
- PetController,
262
- UserController,
263
- StoreController,
264
- TagModel,
265
- OtherModelsContainer,
266
- ]
267
- actual = Swagger::Blocks.build_root_json(swaggered_classes)
268
-
269
- # Multiple expectations for better test diff output.
270
- actual = JSON.parse(actual.to_json) # For access consistency.
271
- data = JSON.parse(RESOURCE_LISTING_JSON)
272
- expect(actual['info']).to eq(data['info'])
273
- expect(actual['authorizations']).to eq(data['authorizations'])
274
- actual['apis'].each_with_index do |api_data, i|
275
- expect(api_data).to eq(data['apis'][i])
276
- end
277
- expect(actual).to eq(data)
278
- end
279
- it 'is idempotent' do
280
- swaggered_classes = [PetController, UserController, StoreController]
281
- actual = JSON.parse(Swagger::Blocks.build_root_json(swaggered_classes).to_json)
282
- actual = JSON.parse(Swagger::Blocks.build_root_json(swaggered_classes).to_json)
283
- data = JSON.parse(RESOURCE_LISTING_JSON)
284
- expect(actual).to eq(data)
285
- end
286
- it 'errors if no swagger_root is declared' do
287
- expect {
288
- Swagger::Blocks.build_root_json([])
289
- }.to raise_error(Swagger::Blocks::DeclarationError)
290
- end
291
- it 'errors if mulitple swagger_roots are declared' do
292
- expect {
293
- Swagger::Blocks.build_root_json([PetController, PetController])
294
- }.to raise_error(Swagger::Blocks::DeclarationError)
295
- end
296
- it 'does not error if given non-swaggered classes' do
297
- Swagger::Blocks.build_root_json([PetController, BlankController])
298
- end
299
- end
300
- describe 'build_api_json' do
301
- it 'outputs the correct data' do
302
- swaggered_classes = [
303
- PetController,
304
- UserController,
305
- StoreController,
306
- TagModel,
307
- OtherModelsContainer,
308
- ]
309
- actual = Swagger::Blocks.build_api_json(:pets, swaggered_classes)
310
-
311
- # Multiple expectations for better test diff output.
312
- actual = JSON.parse(actual.to_json) # For access consistency.
313
- data = JSON.parse(API_DECLARATION_JSON)
314
- expect(actual['apis'][0]).to be
315
- expect(actual['apis'][0]['operations']).to be
316
- expect(actual['apis'][0]['operations'][0]).to be
317
- expect(actual['apis'][0]['operations'][1]).to be
318
- expect(actual['apis'][0]['operations'][0]).to eq(data['apis'][0]['operations'][0])
319
- expect(actual['apis'][0]['operations'][1]).to eq(data['apis'][0]['operations'][1])
320
- expect(actual['apis'][0]['operations']).to eq(data['apis'][0]['operations'])
321
- expect(actual['apis']).to eq(data['apis'])
322
- expect(actual['models']).to eq(data['models'])
323
- expect(actual).to eq(data)
324
- end
325
- it 'is idempotent' do
326
- swaggered_classes = [
327
- PetController,
328
- UserController,
329
- StoreController,
330
- TagModel,
331
- OtherModelsContainer,
332
- ]
333
- actual = JSON.parse(Swagger::Blocks.build_api_json(:pets, swaggered_classes).to_json)
334
- actual = JSON.parse(Swagger::Blocks.build_api_json(:pets, swaggered_classes).to_json)
335
- data = JSON.parse(API_DECLARATION_JSON)
336
- expect(actual).to eq(data)
337
- end
338
- it 'errors if no swagger_root is declared' do
339
- expect {
340
- Swagger::Blocks.build_root_json([])
341
- }.to raise_error(Swagger::Blocks::DeclarationError)
342
- end
343
- it 'errors if multiple swagger_roots are declared' do
344
- expect {
345
- Swagger::Blocks.build_root_json([PetController, PetController])
346
- }.to raise_error(Swagger::Blocks::DeclarationError)
347
- end
348
- end
349
- end
@@ -1,60 +0,0 @@
1
- {
2
- "apiVersion": "1.0.0",
3
- "swaggerVersion": "1.2",
4
- "apis": [
5
- {
6
- "path": "/pet",
7
- "description": "Operations about pets"
8
- },
9
- {
10
- "path": "/user",
11
- "description": "Operations about user"
12
- },
13
- {
14
- "path": "/store",
15
- "description": "Operations about store"
16
- }
17
- ],
18
- "authorizations": {
19
- "oauth2": {
20
- "type": "oauth2",
21
- "scopes": [
22
- {
23
- "scope": "email",
24
- "description": "Access to your email address"
25
- },
26
- {
27
- "scope": "pets",
28
- "description": "Access to your pets"
29
- }
30
- ],
31
- "grantTypes": {
32
- "implicit": {
33
- "loginEndpoint": {
34
- "url": "http://petstore.swagger.wordnik.com/oauth/dialog"
35
- },
36
- "tokenName": "access_token"
37
- },
38
- "authorization_code": {
39
- "tokenRequestEndpoint": {
40
- "url": "http://petstore.swagger.wordnik.com/oauth/requestToken",
41
- "clientIdName": "client_id",
42
- "clientSecretName": "client_secret"
43
- },
44
- "tokenEndpoint": {
45
- "url": "http://petstore.swagger.wordnik.com/oauth/token",
46
- "tokenName": "access_code"
47
- }
48
- }
49
- }
50
- }
51
- },
52
- "info": {
53
- "title": "Swagger Sample App",
54
- "description": "This is a sample server Petstore server. You can find out more about Swagger \n at <a href=\"http://swagger.wordnik.com\">http://swagger.wordnik.com</a> or on irc.freenode.net, #swagger. For this sample,\n you can use the api key \"special-key\" to test the authorization filters",
55
- "termsOfServiceUrl": "http://helloreverb.com/terms/",
56
- "contact": "apiteam@wordnik.com",
57
- "license": "Apache 2.0",
58
- "licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.html"
59
- }
60
- }