swagger-blocks 1.0.1 → 1.1
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.
- checksums.yaml +5 -13
- data/README.md +168 -75
- data/README_v1_2.md +141 -0
- data/lib/swagger/blocks.rb +432 -90
- data/lib/swagger/blocks/version.rb +1 -1
- data/spec/lib/swagger_blocks_spec.rb +2 -2
- data/spec/lib/swagger_v2_api_declaration.json +224 -0
- data/spec/lib/swagger_v2_blocks_spec.rb +256 -0
- metadata +21 -16
@@ -276,7 +276,7 @@ end
|
|
276
276
|
class BlankController; end
|
277
277
|
|
278
278
|
|
279
|
-
describe Swagger::Blocks do
|
279
|
+
describe 'Swagger::Blocks v1' do
|
280
280
|
describe 'build_root_json' do
|
281
281
|
it 'outputs the correct data' do
|
282
282
|
swaggered_classes = [
|
@@ -362,7 +362,7 @@ describe Swagger::Blocks do
|
|
362
362
|
Swagger::Blocks.build_root_json([])
|
363
363
|
}.to raise_error(Swagger::Blocks::DeclarationError)
|
364
364
|
end
|
365
|
-
it 'errors if
|
365
|
+
it 'errors if multiple swagger_roots are declared' do
|
366
366
|
expect {
|
367
367
|
Swagger::Blocks.build_root_json([PetController, PetController])
|
368
368
|
}.to raise_error(Swagger::Blocks::DeclarationError)
|
@@ -0,0 +1,224 @@
|
|
1
|
+
{
|
2
|
+
"swagger": "2.0",
|
3
|
+
"info": {
|
4
|
+
"version": "1.0.0",
|
5
|
+
"title": "Swagger Petstore",
|
6
|
+
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
|
7
|
+
"termsOfService": "http://helloreverb.com/terms/",
|
8
|
+
"contact": {
|
9
|
+
"name": "Wordnik API Team"
|
10
|
+
},
|
11
|
+
"license": {
|
12
|
+
"name": "MIT"
|
13
|
+
}
|
14
|
+
},
|
15
|
+
"host": "petstore.swagger.wordnik.com",
|
16
|
+
"basePath": "/api",
|
17
|
+
"schemes": [
|
18
|
+
"http"
|
19
|
+
],
|
20
|
+
"consumes": [
|
21
|
+
"application/json"
|
22
|
+
],
|
23
|
+
"produces": [
|
24
|
+
"application/json"
|
25
|
+
],
|
26
|
+
"paths": {
|
27
|
+
"/pets": {
|
28
|
+
"get": {
|
29
|
+
"description": "Returns all pets from the system that the user has access to",
|
30
|
+
"operationId": "findPets",
|
31
|
+
"produces": [
|
32
|
+
"application/json",
|
33
|
+
"application/xml",
|
34
|
+
"text/xml",
|
35
|
+
"text/html"
|
36
|
+
],
|
37
|
+
"parameters": [
|
38
|
+
{
|
39
|
+
"name": "tags",
|
40
|
+
"in": "query",
|
41
|
+
"description": "tags to filter by",
|
42
|
+
"required": false,
|
43
|
+
"type": "array",
|
44
|
+
"items": {
|
45
|
+
"type": "string"
|
46
|
+
},
|
47
|
+
"collectionFormat": "csv"
|
48
|
+
},
|
49
|
+
{
|
50
|
+
"name": "limit",
|
51
|
+
"in": "query",
|
52
|
+
"description": "maximum number of results to return",
|
53
|
+
"required": false,
|
54
|
+
"type": "integer",
|
55
|
+
"format": "int32"
|
56
|
+
}
|
57
|
+
],
|
58
|
+
"responses": {
|
59
|
+
"200": {
|
60
|
+
"description": "pet response",
|
61
|
+
"schema": {
|
62
|
+
"type": "array",
|
63
|
+
"items": {
|
64
|
+
"$ref": "#/definitions/Pet"
|
65
|
+
}
|
66
|
+
}
|
67
|
+
},
|
68
|
+
"default": {
|
69
|
+
"description": "unexpected error",
|
70
|
+
"schema": {
|
71
|
+
"$ref": "#/definitions/ErrorModel"
|
72
|
+
}
|
73
|
+
}
|
74
|
+
}
|
75
|
+
},
|
76
|
+
"post": {
|
77
|
+
"description": "Creates a new pet in the store. Duplicates are allowed",
|
78
|
+
"operationId": "addPet",
|
79
|
+
"produces": [
|
80
|
+
"application/json"
|
81
|
+
],
|
82
|
+
"parameters": [
|
83
|
+
{
|
84
|
+
"name": "pet",
|
85
|
+
"in": "body",
|
86
|
+
"description": "Pet to add to the store",
|
87
|
+
"required": true,
|
88
|
+
"schema": {
|
89
|
+
"$ref": "#/definitions/PetInput"
|
90
|
+
}
|
91
|
+
}
|
92
|
+
],
|
93
|
+
"responses": {
|
94
|
+
"200": {
|
95
|
+
"description": "pet response",
|
96
|
+
"schema": {
|
97
|
+
"$ref": "#/definitions/Pet"
|
98
|
+
}
|
99
|
+
},
|
100
|
+
"default": {
|
101
|
+
"description": "unexpected error",
|
102
|
+
"schema": {
|
103
|
+
"$ref": "#/definitions/ErrorModel"
|
104
|
+
}
|
105
|
+
}
|
106
|
+
}
|
107
|
+
}
|
108
|
+
},
|
109
|
+
"/pets/{id}": {
|
110
|
+
"get": {
|
111
|
+
"description": "Returns a user based on a single ID, if the user does not have access to the pet",
|
112
|
+
"operationId": "findPetById",
|
113
|
+
"produces": [
|
114
|
+
"application/json",
|
115
|
+
"application/xml",
|
116
|
+
"text/xml",
|
117
|
+
"text/html"
|
118
|
+
],
|
119
|
+
"parameters": [
|
120
|
+
{
|
121
|
+
"name": "id",
|
122
|
+
"in": "path",
|
123
|
+
"description": "ID of pet to fetch",
|
124
|
+
"required": true,
|
125
|
+
"type": "integer",
|
126
|
+
"format": "int64"
|
127
|
+
}
|
128
|
+
],
|
129
|
+
"responses": {
|
130
|
+
"200": {
|
131
|
+
"description": "pet response",
|
132
|
+
"schema": {
|
133
|
+
"$ref": "#/definitions/Pet"
|
134
|
+
}
|
135
|
+
},
|
136
|
+
"default": {
|
137
|
+
"description": "unexpected error",
|
138
|
+
"schema": {
|
139
|
+
"$ref": "#/definitions/ErrorModel"
|
140
|
+
}
|
141
|
+
}
|
142
|
+
}
|
143
|
+
},
|
144
|
+
"delete": {
|
145
|
+
"description": "deletes a single pet based on the ID supplied",
|
146
|
+
"operationId": "deletePet",
|
147
|
+
"parameters": [
|
148
|
+
{
|
149
|
+
"name": "id",
|
150
|
+
"in": "path",
|
151
|
+
"description": "ID of pet to delete",
|
152
|
+
"required": true,
|
153
|
+
"type": "integer",
|
154
|
+
"format": "int64"
|
155
|
+
}
|
156
|
+
],
|
157
|
+
"responses": {
|
158
|
+
"204": {
|
159
|
+
"description": "pet deleted"
|
160
|
+
},
|
161
|
+
"default": {
|
162
|
+
"description": "unexpected error",
|
163
|
+
"schema": {
|
164
|
+
"$ref": "#/definitions/ErrorModel"
|
165
|
+
}
|
166
|
+
}
|
167
|
+
}
|
168
|
+
}
|
169
|
+
}
|
170
|
+
},
|
171
|
+
"definitions": {
|
172
|
+
"Pet": {
|
173
|
+
"required": [
|
174
|
+
"id",
|
175
|
+
"name"
|
176
|
+
],
|
177
|
+
"properties": {
|
178
|
+
"id": {
|
179
|
+
"type": "integer",
|
180
|
+
"format": "int64"
|
181
|
+
},
|
182
|
+
"name": {
|
183
|
+
"type": "string"
|
184
|
+
},
|
185
|
+
"tag": {
|
186
|
+
"type": "string"
|
187
|
+
}
|
188
|
+
}
|
189
|
+
},
|
190
|
+
"PetInput": {
|
191
|
+
"allOf": [
|
192
|
+
{
|
193
|
+
"$ref": "#/definitions/Pet"
|
194
|
+
},
|
195
|
+
{
|
196
|
+
"required": [
|
197
|
+
"name"
|
198
|
+
],
|
199
|
+
"properties": {
|
200
|
+
"id": {
|
201
|
+
"type": "integer",
|
202
|
+
"format": "int64"
|
203
|
+
}
|
204
|
+
}
|
205
|
+
}
|
206
|
+
]
|
207
|
+
},
|
208
|
+
"ErrorModel": {
|
209
|
+
"required": [
|
210
|
+
"code",
|
211
|
+
"message"
|
212
|
+
],
|
213
|
+
"properties": {
|
214
|
+
"code": {
|
215
|
+
"type": "integer",
|
216
|
+
"format": "int32"
|
217
|
+
},
|
218
|
+
"message": {
|
219
|
+
"type": "string"
|
220
|
+
}
|
221
|
+
}
|
222
|
+
}
|
223
|
+
}
|
224
|
+
}
|
@@ -0,0 +1,256 @@
|
|
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_V2 = open(File.expand_path('../swagger_v2_api_declaration.json', __FILE__)).read
|
7
|
+
|
8
|
+
class PetControllerV2
|
9
|
+
include Swagger::Blocks
|
10
|
+
|
11
|
+
swagger_root do
|
12
|
+
key :swagger, '2.0'
|
13
|
+
info do
|
14
|
+
key :version, '1.0.0'
|
15
|
+
key :title, 'Swagger Petstore'
|
16
|
+
key :description, 'A sample API that uses a petstore as an example to ' \
|
17
|
+
'demonstrate features in the swagger-2.0 specification'
|
18
|
+
key :termsOfService, 'http://helloreverb.com/terms/'
|
19
|
+
contact do
|
20
|
+
key :name, 'Wordnik API Team'
|
21
|
+
end
|
22
|
+
license do
|
23
|
+
key :name, 'MIT'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
key :host, 'petstore.swagger.wordnik.com'
|
27
|
+
key :basePath, '/api'
|
28
|
+
key :schemes, ['http']
|
29
|
+
key :consumes, ['application/json']
|
30
|
+
key :produces, ['application/json']
|
31
|
+
end
|
32
|
+
|
33
|
+
swagger_path '/pets' do
|
34
|
+
operation :get do
|
35
|
+
key :description, 'Returns all pets from the system that the user has access to'
|
36
|
+
key :operationId, 'findPets'
|
37
|
+
key :produces, [
|
38
|
+
'application/json',
|
39
|
+
'application/xml',
|
40
|
+
'text/xml',
|
41
|
+
'text/html',
|
42
|
+
]
|
43
|
+
parameter do
|
44
|
+
key :name, :tags
|
45
|
+
key :in, :query
|
46
|
+
key :description, 'tags to filter by'
|
47
|
+
key :required, false
|
48
|
+
key :type, :array
|
49
|
+
items do
|
50
|
+
key :type, :string
|
51
|
+
end
|
52
|
+
key :collectionFormat, :csv
|
53
|
+
end
|
54
|
+
parameter do
|
55
|
+
key :name, :limit
|
56
|
+
key :in, :query
|
57
|
+
key :description, 'maximum number of results to return'
|
58
|
+
key :required, false
|
59
|
+
key :type, :integer
|
60
|
+
key :format, :int32
|
61
|
+
end
|
62
|
+
response 200 do
|
63
|
+
key :description, 'pet response'
|
64
|
+
schema do
|
65
|
+
key :type, :array
|
66
|
+
items do
|
67
|
+
key :'$ref', :Pet
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
response :default do
|
72
|
+
key :description, 'unexpected error'
|
73
|
+
schema do
|
74
|
+
key :'$ref', :ErrorModel
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
operation :post do
|
79
|
+
key :description, 'Creates a new pet in the store. Duplicates are allowed'
|
80
|
+
key :operationId, 'addPet'
|
81
|
+
key :produces, [
|
82
|
+
'application/json'
|
83
|
+
]
|
84
|
+
parameter do
|
85
|
+
key :name, :pet
|
86
|
+
key :in, :body
|
87
|
+
key :description, 'Pet to add to the store'
|
88
|
+
key :required, true
|
89
|
+
schema do
|
90
|
+
key :'$ref', :PetInput
|
91
|
+
end
|
92
|
+
end
|
93
|
+
response 200 do
|
94
|
+
key :description, 'pet response'
|
95
|
+
schema do
|
96
|
+
key :'$ref', :Pet
|
97
|
+
end
|
98
|
+
end
|
99
|
+
response :default do
|
100
|
+
key :description, 'unexpected error'
|
101
|
+
schema do
|
102
|
+
key :'$ref', :ErrorModel
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
swagger_path '/pets/{id}' do
|
109
|
+
operation :get do
|
110
|
+
key :description, 'Returns a user based on a single ID, if the user does not have access to the pet'
|
111
|
+
key :operationId, 'findPetById'
|
112
|
+
key :produces, [
|
113
|
+
'application/json',
|
114
|
+
'application/xml',
|
115
|
+
'text/xml',
|
116
|
+
'text/html',
|
117
|
+
]
|
118
|
+
parameter do
|
119
|
+
key :name, :id
|
120
|
+
key :in, :path
|
121
|
+
key :description, 'ID of pet to fetch'
|
122
|
+
key :required, true
|
123
|
+
key :type, :integer
|
124
|
+
key :format, :int64
|
125
|
+
end
|
126
|
+
response 200 do
|
127
|
+
key :description, 'pet response'
|
128
|
+
schema do
|
129
|
+
key :'$ref', :Pet
|
130
|
+
end
|
131
|
+
end
|
132
|
+
response :default do
|
133
|
+
key :description, 'unexpected error'
|
134
|
+
schema do
|
135
|
+
key :'$ref', :ErrorModel
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
operation :delete do
|
140
|
+
key :description, 'deletes a single pet based on the ID supplied'
|
141
|
+
key :operationId, 'deletePet'
|
142
|
+
parameter do
|
143
|
+
key :name, :id
|
144
|
+
key :in, :path
|
145
|
+
key :description, 'ID of pet to delete'
|
146
|
+
key :required, true
|
147
|
+
key :type, :integer
|
148
|
+
key :format, :int64
|
149
|
+
end
|
150
|
+
response 204 do
|
151
|
+
key :description, 'pet deleted'
|
152
|
+
end
|
153
|
+
response :default do
|
154
|
+
key :description, 'unexpected error'
|
155
|
+
schema do
|
156
|
+
key :'$ref', :ErrorModel
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
|
164
|
+
class PetV2
|
165
|
+
include Swagger::Blocks
|
166
|
+
|
167
|
+
swagger_schema :Pet do
|
168
|
+
key :required, [:id, :name]
|
169
|
+
property :id do
|
170
|
+
key :type, :integer
|
171
|
+
key :format, :int64
|
172
|
+
end
|
173
|
+
property :name do
|
174
|
+
key :type, :string
|
175
|
+
end
|
176
|
+
property :tag do
|
177
|
+
key :type, :string
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
swagger_schema :PetInput do
|
182
|
+
allOf do
|
183
|
+
schema do
|
184
|
+
key :'$ref', :Pet
|
185
|
+
end
|
186
|
+
schema do
|
187
|
+
key :required, [:name]
|
188
|
+
property :id do
|
189
|
+
key :type, :integer
|
190
|
+
key :format, :int64
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
class ErrorModelV2
|
198
|
+
include Swagger::Blocks
|
199
|
+
|
200
|
+
swagger_schema :ErrorModel do
|
201
|
+
key :required, [:code, :message]
|
202
|
+
property :code do
|
203
|
+
key :type, :integer
|
204
|
+
key :format, :int32
|
205
|
+
end
|
206
|
+
property :message do
|
207
|
+
key :type, :string
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
describe 'Swagger::Blocks v2' do
|
213
|
+
describe 'build_json' do
|
214
|
+
it 'outputs the correct data' do
|
215
|
+
swaggered_classes = [
|
216
|
+
PetControllerV2,
|
217
|
+
PetV2,
|
218
|
+
ErrorModelV2
|
219
|
+
]
|
220
|
+
actual = Swagger::Blocks.build_root_json(swaggered_classes)
|
221
|
+
actual = JSON.parse(actual.to_json) # For access consistency.
|
222
|
+
data = JSON.parse(RESOURCE_LISTING_JSON_V2)
|
223
|
+
|
224
|
+
# Multiple expectations for better test diff output.
|
225
|
+
expect(actual['info']).to eq(data['info'])
|
226
|
+
expect(actual['paths']).to be
|
227
|
+
expect(actual['paths']['/pets']).to be
|
228
|
+
expect(actual['paths']['/pets']).to eq(data['paths']['/pets'])
|
229
|
+
expect(actual['paths']).to eq(data['paths'])
|
230
|
+
expect(actual['definitions']).to eq(data['definitions'])
|
231
|
+
expect(actual).to eq(data)
|
232
|
+
end
|
233
|
+
it 'is idempotent' do
|
234
|
+
swaggered_classes = [PetControllerV2, PetV2, ErrorModelV2]
|
235
|
+
actual = JSON.parse(Swagger::Blocks.build_root_json(swaggered_classes).to_json)
|
236
|
+
actual = JSON.parse(Swagger::Blocks.build_root_json(swaggered_classes).to_json)
|
237
|
+
data = JSON.parse(RESOURCE_LISTING_JSON_V2)
|
238
|
+
expect(actual).to eq(data)
|
239
|
+
end
|
240
|
+
it 'errors if no swagger_root is declared' do
|
241
|
+
expect {
|
242
|
+
Swagger::Blocks.build_root_json([])
|
243
|
+
}.to raise_error(Swagger::Blocks::DeclarationError)
|
244
|
+
end
|
245
|
+
it 'errors if mulitple swagger_roots are declared' do
|
246
|
+
expect {
|
247
|
+
Swagger::Blocks.build_root_json([PetControllerV2, PetControllerV2])
|
248
|
+
}.to raise_error(Swagger::Blocks::DeclarationError)
|
249
|
+
end
|
250
|
+
it 'errors if calling build_api_json' do
|
251
|
+
expect {
|
252
|
+
Swagger::Blocks.build_api_json('fake', [PetControllerV2])
|
253
|
+
}.to raise_error(Swagger::Blocks::NotSupportedError)
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|