serverless-openapi-documenter 0.0.111 → 0.0.113
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.
- package/package.json +1 -2
- package/src/definitionGenerator.js +7 -3
- package/src/openAPIGenerator.js +1 -1
- package/src/schemaHandler.js +63 -25
- package/test/unit/definitionGenerator.spec.js +3 -7
- package/test/unit/openAPIGenerator.spec.js +4 -9
- package/test/unit/owasp.spec.js +1 -1
- package/test/unit/schemaHandler.spec.js +856 -633
|
@@ -1,700 +1,923 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
|
-
const fs = require(
|
|
4
|
-
const path = require(
|
|
3
|
+
const fs = require("fs").promises;
|
|
4
|
+
const path = require("path");
|
|
5
5
|
|
|
6
|
-
const expect = require(
|
|
7
|
-
const nock = require(
|
|
6
|
+
const expect = require("chai").expect;
|
|
7
|
+
const nock = require("nock");
|
|
8
8
|
|
|
9
|
-
const modelsDocumentOG = require(
|
|
10
|
-
const modelsAltDocumentOG = require(
|
|
11
|
-
const modelsListDocumentOG = require(
|
|
12
|
-
const modelsListAltDocumentOG = require(
|
|
13
|
-
|
|
14
|
-
const serverlessMock = require('../helpers/serverless')
|
|
15
|
-
const SchemaHandler = require('../../src/schemaHandler')
|
|
9
|
+
const modelsDocumentOG = require("../models/models/models.json");
|
|
10
|
+
const modelsAltDocumentOG = require("../models/models/models-alt.json");
|
|
11
|
+
const modelsListDocumentOG = require("../models/models/modelsList.json");
|
|
12
|
+
const modelsListAltDocumentOG = require("../models/models/modelsList-alt.json");
|
|
16
13
|
|
|
14
|
+
const serverlessMock = require("../helpers/serverless");
|
|
15
|
+
const SchemaHandler = require("../../src/schemaHandler");
|
|
17
16
|
|
|
18
17
|
describe(`SchemaHandler`, function () {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
18
|
+
let mockServerless;
|
|
19
|
+
let openAPI;
|
|
20
|
+
let modelsDocument,
|
|
21
|
+
modelsAltDocument,
|
|
22
|
+
modelsListDocument,
|
|
23
|
+
modelsListAltDocument;
|
|
24
|
+
|
|
25
|
+
const logger = {
|
|
26
|
+
verbose: (str) => {
|
|
27
|
+
console.log(str);
|
|
28
|
+
},
|
|
29
|
+
warn: (str) => {
|
|
30
|
+
console.log(str);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const v4 = new RegExp(
|
|
35
|
+
/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const openAPISchema = {
|
|
39
|
+
version: "3.0.3",
|
|
40
|
+
components: {
|
|
41
|
+
schemas: {},
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
beforeEach(function () {
|
|
46
|
+
mockServerless = structuredClone(serverlessMock);
|
|
47
|
+
modelsDocument = structuredClone(modelsDocumentOG);
|
|
48
|
+
modelsAltDocument = structuredClone(modelsAltDocumentOG);
|
|
49
|
+
modelsListDocument = structuredClone(modelsListDocumentOG);
|
|
50
|
+
modelsListAltDocument = structuredClone(modelsListAltDocumentOG);
|
|
51
|
+
openAPI = structuredClone(openAPISchema);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe(`constuctor`, function () {
|
|
55
|
+
it("should return an instance of SchemaHandler", function () {
|
|
56
|
+
const expected = new SchemaHandler(mockServerless, openAPI, logger);
|
|
57
|
+
expect(expected).to.be.an.instanceOf(SchemaHandler);
|
|
37
58
|
});
|
|
38
59
|
|
|
39
|
-
describe(`
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
60
|
+
describe(`standardising the models`, function () {
|
|
61
|
+
it(`should standardise models syntax in to the correct format`, function () {
|
|
62
|
+
Object.assign(
|
|
63
|
+
mockServerless.service.custom.documentation,
|
|
64
|
+
modelsDocument
|
|
65
|
+
);
|
|
66
|
+
const expected = new SchemaHandler(mockServerless, openAPI, logger);
|
|
67
|
+
|
|
68
|
+
expect(expected.models).to.be.an("array");
|
|
69
|
+
expect(expected.models.length).to.be.equal(1);
|
|
70
|
+
|
|
71
|
+
expect(expected.models[0].name).to.equal("ErrorResponse");
|
|
72
|
+
expect(expected.models[0]).to.have.property("contentType");
|
|
73
|
+
expect(expected.models[0]).to.have.property("schema");
|
|
74
|
+
expect(expected.models[0].schema).to.be.eql({
|
|
75
|
+
type: "object",
|
|
76
|
+
properties: { error: { type: "string" } },
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it(`should standardise alternative models syntax in to the correct format`, function () {
|
|
81
|
+
Object.assign(
|
|
82
|
+
mockServerless.service.custom.documentation,
|
|
83
|
+
modelsAltDocument
|
|
84
|
+
);
|
|
85
|
+
const expected = new SchemaHandler(mockServerless, openAPI, logger);
|
|
86
|
+
|
|
87
|
+
expect(expected.models).to.be.an("array");
|
|
88
|
+
expect(expected.models.length).to.be.equal(1);
|
|
89
|
+
|
|
90
|
+
expect(expected.models[0].name).to.equal("ErrorResponse");
|
|
91
|
+
expect(expected.models[0]).to.have.property("contentType");
|
|
92
|
+
expect(expected.models[0]).to.have.property("schema");
|
|
93
|
+
expect(expected.models[0].schema).to.be.eql({
|
|
94
|
+
type: "object",
|
|
95
|
+
properties: { error: { type: "string" } },
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it(`should standardise modelsList syntax in to the correct format`, function () {
|
|
100
|
+
Object.assign(
|
|
101
|
+
mockServerless.service.custom.documentation,
|
|
102
|
+
modelsListDocument
|
|
103
|
+
);
|
|
104
|
+
const expected = new SchemaHandler(mockServerless, openAPI, logger);
|
|
105
|
+
|
|
106
|
+
expect(expected.models).to.be.an("array");
|
|
107
|
+
expect(expected.models.length).to.be.equal(1);
|
|
108
|
+
|
|
109
|
+
expect(expected.models[0].name).to.equal("ErrorResponse");
|
|
110
|
+
expect(expected.models[0]).to.have.property("contentType");
|
|
111
|
+
expect(expected.models[0]).to.have.property("schema");
|
|
112
|
+
expect(expected.models[0].schema).to.be.eql({
|
|
113
|
+
type: "object",
|
|
114
|
+
properties: { error: { type: "string" } },
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it(`should standardise alternative modelsList syntax in to the correct format`, function () {
|
|
119
|
+
Object.assign(
|
|
120
|
+
mockServerless.service.custom.documentation,
|
|
121
|
+
modelsListAltDocument
|
|
122
|
+
);
|
|
123
|
+
const expected = new SchemaHandler(mockServerless, openAPI, logger);
|
|
124
|
+
|
|
125
|
+
expect(expected.models).to.be.an("array");
|
|
126
|
+
expect(expected.models.length).to.be.equal(1);
|
|
127
|
+
|
|
128
|
+
expect(expected.models[0].name).to.equal("ErrorResponse");
|
|
129
|
+
expect(expected.models[0]).to.have.property("contentType");
|
|
130
|
+
expect(expected.models[0]).to.have.property("schema");
|
|
131
|
+
expect(expected.models[0].schema).to.be.eql({
|
|
132
|
+
type: "object",
|
|
133
|
+
properties: { error: { type: "string" } },
|
|
43
134
|
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it(`should standardise mixed models syntax in to the correct format`, function () {
|
|
138
|
+
const newModelsDocument = structuredClone(modelsDocument);
|
|
139
|
+
Object.assign(
|
|
140
|
+
mockServerless.service.custom.documentation,
|
|
141
|
+
newModelsDocument
|
|
142
|
+
);
|
|
143
|
+
mockServerless.service.custom.documentation.models.push({
|
|
144
|
+
name: "SuccessResponse",
|
|
145
|
+
description: "A success response",
|
|
146
|
+
contentType: "application/json",
|
|
147
|
+
schema: {
|
|
148
|
+
type: "string",
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
const expected = new SchemaHandler(mockServerless, openAPI, logger);
|
|
44
152
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
Object.assign(mockServerless.service.custom.documentation, modelsDocument)
|
|
48
|
-
const expected = new SchemaHandler(mockServerless, openAPI)
|
|
153
|
+
expect(expected.models).to.be.an("array");
|
|
154
|
+
expect(expected.models.length).to.be.equal(2);
|
|
49
155
|
|
|
50
|
-
|
|
51
|
-
|
|
156
|
+
expect(expected.models[0].name).to.equal("ErrorResponse");
|
|
157
|
+
expect(expected.models[0]).to.have.property("contentType");
|
|
158
|
+
expect(expected.models[0]).to.have.property("schema");
|
|
159
|
+
expect(expected.models[0].schema).to.be.eql({
|
|
160
|
+
type: "object",
|
|
161
|
+
properties: { error: { type: "string" } },
|
|
162
|
+
});
|
|
52
163
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
164
|
+
expect(expected.models[1].name).to.equal("SuccessResponse");
|
|
165
|
+
expect(expected.models[1]).to.have.property("contentType");
|
|
166
|
+
expect(expected.models[1]).to.have.property("schema");
|
|
167
|
+
expect(expected.models[1].schema).to.be.eql({ type: "string" });
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it(`should standardise mixed modelsList syntax in to the correct format`, function () {
|
|
171
|
+
const newModelsDocument = structuredClone(modelsListDocument);
|
|
172
|
+
Object.assign(
|
|
173
|
+
mockServerless.service.custom.documentation,
|
|
174
|
+
newModelsDocument
|
|
175
|
+
);
|
|
176
|
+
mockServerless.service.custom.documentation.modelsList.push({
|
|
177
|
+
name: "SuccessResponse",
|
|
178
|
+
description: "A success response",
|
|
179
|
+
contentType: "application/json",
|
|
180
|
+
schema: {
|
|
181
|
+
type: "string",
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
const expected = new SchemaHandler(mockServerless, openAPI, logger);
|
|
58
185
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const expected = new SchemaHandler(mockServerless, openAPI)
|
|
186
|
+
expect(expected.models).to.be.an("array");
|
|
187
|
+
expect(expected.models.length).to.be.equal(2);
|
|
62
188
|
|
|
63
|
-
|
|
64
|
-
|
|
189
|
+
expect(expected.models[0].name).to.equal("ErrorResponse");
|
|
190
|
+
expect(expected.models[0]).to.have.property("contentType");
|
|
191
|
+
expect(expected.models[0]).to.have.property("schema");
|
|
192
|
+
expect(expected.models[0].schema).to.be.eql({
|
|
193
|
+
type: "object",
|
|
194
|
+
properties: { error: { type: "string" } },
|
|
195
|
+
});
|
|
65
196
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
197
|
+
expect(expected.models[1].name).to.equal("SuccessResponse");
|
|
198
|
+
expect(expected.models[1]).to.have.property("contentType");
|
|
199
|
+
expect(expected.models[1]).to.have.property("schema");
|
|
200
|
+
expect(expected.models[1].schema).to.be.eql({ type: "string" });
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it(`should standardise mixed models and modelsList syntax in to the correct format`, function () {
|
|
204
|
+
const newModelsDocument = structuredClone(modelsListDocument);
|
|
205
|
+
Object.assign(
|
|
206
|
+
mockServerless.service.custom.documentation,
|
|
207
|
+
newModelsDocument
|
|
208
|
+
);
|
|
209
|
+
Object.assign(mockServerless.service.custom.documentation, {
|
|
210
|
+
models: [
|
|
211
|
+
{
|
|
212
|
+
name: "SuccessResponse",
|
|
213
|
+
description: "A success response",
|
|
214
|
+
contentType: "application/json",
|
|
215
|
+
schema: {
|
|
216
|
+
type: "string",
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
],
|
|
220
|
+
});
|
|
71
221
|
|
|
72
|
-
|
|
73
|
-
Object.assign(mockServerless.service.custom.documentation, modelsListDocument)
|
|
74
|
-
const expected = new SchemaHandler(mockServerless, openAPI)
|
|
222
|
+
const expected = new SchemaHandler(mockServerless, openAPI, logger);
|
|
75
223
|
|
|
76
|
-
|
|
77
|
-
|
|
224
|
+
expect(expected.models).to.be.an("array");
|
|
225
|
+
expect(expected.models.length).to.be.equal(2);
|
|
78
226
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
});
|
|
227
|
+
expect(expected.models[0].name).to.equal("SuccessResponse");
|
|
228
|
+
expect(expected.models[0]).to.have.property("contentType");
|
|
229
|
+
expect(expected.models[0]).to.have.property("schema");
|
|
230
|
+
expect(expected.models[0].schema).to.be.eql({ type: "string" });
|
|
84
231
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
232
|
+
expect(expected.models[1].name).to.equal("ErrorResponse");
|
|
233
|
+
expect(expected.models[1]).to.have.property("contentType");
|
|
234
|
+
expect(expected.models[1]).to.have.property("schema");
|
|
235
|
+
expect(expected.models[1].schema).to.be.eql({
|
|
236
|
+
type: "object",
|
|
237
|
+
properties: { error: { type: "string" } },
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
});
|
|
88
241
|
|
|
89
|
-
|
|
90
|
-
|
|
242
|
+
it(`should correctly resolve the RefParserOptions`, async function () {
|
|
243
|
+
let expected = new SchemaHandler(mockServerless, openAPI, logger);
|
|
244
|
+
expect(expected.refParserOptions).to.be.an("object");
|
|
245
|
+
expect(expected.refParserOptions).to.be.empty;
|
|
246
|
+
|
|
247
|
+
await fs.mkdir(path.resolve("options")).catch((err) => {
|
|
248
|
+
console.error(err);
|
|
249
|
+
throw err;
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
await fs
|
|
253
|
+
.copyFile(
|
|
254
|
+
path.resolve("test/helpers/ref-parser.js"),
|
|
255
|
+
path.resolve("options/ref-parser.js")
|
|
256
|
+
)
|
|
257
|
+
.catch((err) => {
|
|
258
|
+
console.error(err);
|
|
259
|
+
throw err;
|
|
260
|
+
});
|
|
91
261
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
expect(expected.models[0].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
|
|
96
|
-
});
|
|
262
|
+
expected = new SchemaHandler(mockServerless, openAPI, logger);
|
|
263
|
+
expect(expected.refParserOptions).to.be.an("object");
|
|
264
|
+
expect(expected.refParserOptions).to.have.property("continueOnError");
|
|
97
265
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
{
|
|
103
|
-
name: 'SuccessResponse',
|
|
104
|
-
description: 'A success response',
|
|
105
|
-
contentType: 'application/json',
|
|
106
|
-
schema: {
|
|
107
|
-
type: 'string'
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
)
|
|
111
|
-
const expected = new SchemaHandler(mockServerless, openAPI)
|
|
112
|
-
|
|
113
|
-
expect(expected.models).to.be.an('array')
|
|
114
|
-
expect(expected.models.length).to.be.equal(2)
|
|
115
|
-
|
|
116
|
-
expect(expected.models[0].name).to.equal('ErrorResponse')
|
|
117
|
-
expect(expected.models[0]).to.have.property('contentType')
|
|
118
|
-
expect(expected.models[0]).to.have.property('schema')
|
|
119
|
-
expect(expected.models[0].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
|
|
120
|
-
|
|
121
|
-
expect(expected.models[1].name).to.equal('SuccessResponse')
|
|
122
|
-
expect(expected.models[1]).to.have.property('contentType')
|
|
123
|
-
expect(expected.models[1]).to.have.property('schema')
|
|
124
|
-
expect(expected.models[1].schema).to.be.eql({type: 'string'})
|
|
125
|
-
});
|
|
266
|
+
await fs.rm(path.resolve("options/ref-parser.js")).catch((err) => {
|
|
267
|
+
console.error(err);
|
|
268
|
+
throw err;
|
|
269
|
+
});
|
|
126
270
|
|
|
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
|
-
|
|
271
|
+
await fs.rmdir(path.resolve("options")).catch((err) => {
|
|
272
|
+
console.error(err);
|
|
273
|
+
throw err;
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
describe(`addModelsToOpenAPI`, function () {
|
|
279
|
+
describe(`embedded simple schemas`, function () {
|
|
280
|
+
it(`should add the model to the openAPI schema`, async function () {
|
|
281
|
+
Object.assign(
|
|
282
|
+
mockServerless.service.custom.documentation,
|
|
283
|
+
modelsDocument
|
|
284
|
+
);
|
|
285
|
+
const schemaHandler = new SchemaHandler(
|
|
286
|
+
mockServerless,
|
|
287
|
+
openAPI,
|
|
288
|
+
logger
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
292
|
+
|
|
293
|
+
expect(schemaHandler.openAPI).to.have.property("components");
|
|
294
|
+
expect(schemaHandler.openAPI.components).to.have.property("schemas");
|
|
295
|
+
expect(schemaHandler.openAPI.components.schemas).to.have.property(
|
|
296
|
+
"ErrorResponse"
|
|
297
|
+
);
|
|
298
|
+
expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an(
|
|
299
|
+
"object"
|
|
300
|
+
);
|
|
301
|
+
expect(
|
|
302
|
+
schemaHandler.openAPI.components.schemas.ErrorResponse
|
|
303
|
+
).to.be.eql({
|
|
304
|
+
type: "object",
|
|
305
|
+
properties: { error: { type: "string" } },
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it(`should add a model with references to the openAPI schema`, async function () {
|
|
310
|
+
Object.assign(
|
|
311
|
+
mockServerless.service.custom.documentation,
|
|
312
|
+
modelsDocument
|
|
313
|
+
);
|
|
314
|
+
mockServerless.service.custom.documentation.models.push({
|
|
315
|
+
name: "SuccessResponse",
|
|
316
|
+
contentType: "application/json",
|
|
317
|
+
schema: {
|
|
318
|
+
type: "object",
|
|
319
|
+
properties: {
|
|
320
|
+
name: {
|
|
321
|
+
$ref: "#/definitions/nameObject",
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
definitions: {
|
|
325
|
+
nameObject: {
|
|
326
|
+
type: "object",
|
|
327
|
+
properties: {
|
|
328
|
+
firstName: {
|
|
329
|
+
type: "string",
|
|
330
|
+
},
|
|
331
|
+
},
|
|
332
|
+
},
|
|
333
|
+
},
|
|
334
|
+
},
|
|
335
|
+
});
|
|
336
|
+
const schemaHandler = new SchemaHandler(
|
|
337
|
+
mockServerless,
|
|
338
|
+
openAPI,
|
|
339
|
+
logger
|
|
340
|
+
);
|
|
341
|
+
|
|
342
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
343
|
+
|
|
344
|
+
expect(schemaHandler.openAPI).to.have.property("components");
|
|
345
|
+
expect(schemaHandler.openAPI.components).to.have.property("schemas");
|
|
346
|
+
expect(schemaHandler.openAPI.components.schemas).to.have.property(
|
|
347
|
+
"ErrorResponse"
|
|
348
|
+
);
|
|
349
|
+
expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an(
|
|
350
|
+
"object"
|
|
351
|
+
);
|
|
352
|
+
expect(
|
|
353
|
+
schemaHandler.openAPI.components.schemas.ErrorResponse
|
|
354
|
+
).to.be.eql({
|
|
355
|
+
type: "object",
|
|
356
|
+
properties: { error: { type: "string" } },
|
|
357
|
+
});
|
|
155
358
|
|
|
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
|
-
|
|
359
|
+
expect(schemaHandler.openAPI.components.schemas).to.have.property(
|
|
360
|
+
"SuccessResponse"
|
|
361
|
+
);
|
|
362
|
+
expect(
|
|
363
|
+
schemaHandler.openAPI.components.schemas.SuccessResponse
|
|
364
|
+
).to.be.an("object");
|
|
365
|
+
expect(
|
|
366
|
+
schemaHandler.openAPI.components.schemas.SuccessResponse
|
|
367
|
+
).to.be.eql({
|
|
368
|
+
type: "object",
|
|
369
|
+
properties: {
|
|
370
|
+
name: {
|
|
371
|
+
type: "object",
|
|
372
|
+
properties: {
|
|
373
|
+
firstName: {
|
|
374
|
+
type: "string",
|
|
375
|
+
},
|
|
376
|
+
},
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it(`should add a model with poorly dereferenced references to the openAPI schema`, async function () {
|
|
383
|
+
Object.assign(
|
|
384
|
+
mockServerless.service.custom.documentation,
|
|
385
|
+
modelsDocument
|
|
386
|
+
);
|
|
387
|
+
mockServerless.service.custom.documentation.models.push({
|
|
388
|
+
name: "SuccessResponse",
|
|
389
|
+
contentType: "application/json",
|
|
390
|
+
schema: {
|
|
391
|
+
type: "object",
|
|
392
|
+
$ref: "#/definitions/nameObject",
|
|
393
|
+
definitions: {
|
|
394
|
+
nameObject: {
|
|
395
|
+
type: "object",
|
|
396
|
+
properties: {
|
|
397
|
+
firstName: {
|
|
398
|
+
type: "string",
|
|
399
|
+
},
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
},
|
|
403
|
+
},
|
|
404
|
+
});
|
|
405
|
+
const schemaHandler = new SchemaHandler(
|
|
406
|
+
mockServerless,
|
|
407
|
+
openAPI,
|
|
408
|
+
logger
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
412
|
+
|
|
413
|
+
expect(schemaHandler.openAPI).to.have.property("components");
|
|
414
|
+
expect(schemaHandler.openAPI.components).to.have.property("schemas");
|
|
415
|
+
expect(schemaHandler.openAPI.components.schemas).to.have.property(
|
|
416
|
+
"ErrorResponse"
|
|
417
|
+
);
|
|
418
|
+
expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an(
|
|
419
|
+
"object"
|
|
420
|
+
);
|
|
421
|
+
expect(
|
|
422
|
+
schemaHandler.openAPI.components.schemas.ErrorResponse
|
|
423
|
+
).to.be.eql({
|
|
424
|
+
type: "object",
|
|
425
|
+
properties: { error: { type: "string" } },
|
|
190
426
|
});
|
|
191
427
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
await fs.copyFile(path.resolve('test/helpers/ref-parser.js'), path.resolve('options/ref-parser.js'))
|
|
204
|
-
.catch(err => {
|
|
205
|
-
console.error(err)
|
|
206
|
-
throw err
|
|
207
|
-
})
|
|
208
|
-
|
|
209
|
-
expected = new SchemaHandler(mockServerless, openAPI)
|
|
210
|
-
expect(expected.refParserOptions).to.be.an('object')
|
|
211
|
-
expect(expected.refParserOptions).to.have.property('continueOnError')
|
|
212
|
-
|
|
213
|
-
await fs.rm(path.resolve('options/ref-parser.js'))
|
|
214
|
-
.catch(err => {
|
|
215
|
-
console.error(err)
|
|
216
|
-
throw err
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
await fs.rmdir(path.resolve('options'))
|
|
220
|
-
.catch(err => {
|
|
221
|
-
console.error(err)
|
|
222
|
-
throw err
|
|
223
|
-
})
|
|
428
|
+
expect(schemaHandler.openAPI.components.schemas).to.have.property(
|
|
429
|
+
"SuccessResponse"
|
|
430
|
+
);
|
|
431
|
+
expect(
|
|
432
|
+
schemaHandler.openAPI.components.schemas.SuccessResponse
|
|
433
|
+
).to.be.an("object");
|
|
434
|
+
expect(
|
|
435
|
+
schemaHandler.openAPI.components.schemas.SuccessResponse
|
|
436
|
+
).to.be.eql({
|
|
437
|
+
type: "object",
|
|
438
|
+
properties: { firstName: { type: "string" } },
|
|
224
439
|
});
|
|
440
|
+
});
|
|
225
441
|
});
|
|
226
442
|
|
|
227
|
-
describe(`
|
|
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
|
-
|
|
443
|
+
describe(`schemas with references`, function () {
|
|
444
|
+
describe(`file references`, function () {
|
|
445
|
+
it(`should add schemas with file references to the openAPI schema`, async function () {});
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
describe(`component references`, function () {
|
|
449
|
+
it(`should add schemas with component references to the openAPI schema`, async function () {
|
|
450
|
+
Object.assign(
|
|
451
|
+
mockServerless.service.custom.documentation,
|
|
452
|
+
modelsDocument
|
|
453
|
+
);
|
|
454
|
+
mockServerless.service.custom.documentation.models.push({
|
|
455
|
+
name: "SuccessResponse",
|
|
456
|
+
contentType: "application/json",
|
|
457
|
+
schema: {
|
|
458
|
+
type: "array",
|
|
459
|
+
items: {
|
|
460
|
+
$ref: "#/components/schemas/Agency",
|
|
461
|
+
},
|
|
462
|
+
},
|
|
463
|
+
});
|
|
464
|
+
mockServerless.service.custom.documentation.models.push({
|
|
465
|
+
name: "Agency",
|
|
466
|
+
contentType: "application/json",
|
|
467
|
+
schema: {
|
|
468
|
+
type: "string",
|
|
469
|
+
},
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
const schemaHandler = new SchemaHandler(
|
|
473
|
+
mockServerless,
|
|
474
|
+
openAPI,
|
|
475
|
+
logger
|
|
476
|
+
);
|
|
477
|
+
|
|
478
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
479
|
+
|
|
480
|
+
expect(schemaHandler.openAPI).to.have.property("components");
|
|
481
|
+
expect(schemaHandler.openAPI.components).to.have.property("schemas");
|
|
482
|
+
expect(schemaHandler.openAPI.components.schemas).to.have.property(
|
|
483
|
+
"ErrorResponse"
|
|
484
|
+
);
|
|
485
|
+
expect(
|
|
486
|
+
schemaHandler.openAPI.components.schemas.ErrorResponse
|
|
487
|
+
).to.be.an("object");
|
|
488
|
+
expect(
|
|
489
|
+
schemaHandler.openAPI.components.schemas.ErrorResponse
|
|
490
|
+
).to.be.eql({
|
|
491
|
+
type: "object",
|
|
492
|
+
properties: { error: { type: "string" } },
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
expect(schemaHandler.openAPI.components.schemas).to.have.property(
|
|
496
|
+
"SuccessResponse"
|
|
497
|
+
);
|
|
498
|
+
expect(
|
|
499
|
+
schemaHandler.openAPI.components.schemas.SuccessResponse
|
|
500
|
+
).to.be.an("object");
|
|
501
|
+
expect(
|
|
502
|
+
schemaHandler.openAPI.components.schemas.SuccessResponse
|
|
503
|
+
).to.be.eql({
|
|
504
|
+
type: "array",
|
|
505
|
+
items: {
|
|
506
|
+
$ref: "#/components/schemas/Agency",
|
|
507
|
+
},
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
expect(schemaHandler.openAPI.components.schemas).to.have.property(
|
|
511
|
+
"Agency"
|
|
512
|
+
);
|
|
513
|
+
expect(schemaHandler.openAPI.components.schemas.Agency).to.be.an(
|
|
514
|
+
"object"
|
|
515
|
+
);
|
|
516
|
+
expect(schemaHandler.openAPI.components.schemas.Agency).to.be.eql({
|
|
517
|
+
type: "string",
|
|
518
|
+
});
|
|
519
|
+
});
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
describe(`other references`, function () {
|
|
523
|
+
it(`should add a model that is a webUrl to the openAPI schema`, async function () {
|
|
524
|
+
Object.assign(
|
|
525
|
+
mockServerless.service.custom.documentation,
|
|
526
|
+
modelsDocument
|
|
527
|
+
);
|
|
528
|
+
mockServerless.service.custom.documentation.models.push({
|
|
529
|
+
name: "SuccessResponse",
|
|
530
|
+
contentType: "application/json",
|
|
531
|
+
schema: "https://google.com/build/LicensedMember.json",
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
nock("https://google.com")
|
|
535
|
+
.get("/build/LicensedMember.json")
|
|
536
|
+
.reply(200, {
|
|
537
|
+
type: "object",
|
|
538
|
+
properties: {
|
|
539
|
+
memberId: {
|
|
540
|
+
type: "string",
|
|
541
|
+
},
|
|
542
|
+
createdAt: {
|
|
543
|
+
type: "integer",
|
|
544
|
+
},
|
|
545
|
+
},
|
|
293
546
|
});
|
|
294
547
|
|
|
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
|
-
|
|
548
|
+
const schemaHandler = new SchemaHandler(
|
|
549
|
+
mockServerless,
|
|
550
|
+
openAPI,
|
|
551
|
+
logger
|
|
552
|
+
);
|
|
553
|
+
|
|
554
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
555
|
+
|
|
556
|
+
expect(schemaHandler.openAPI).to.have.property("components");
|
|
557
|
+
expect(schemaHandler.openAPI.components).to.have.property("schemas");
|
|
558
|
+
expect(schemaHandler.openAPI.components.schemas).to.have.property(
|
|
559
|
+
"ErrorResponse"
|
|
560
|
+
);
|
|
561
|
+
expect(
|
|
562
|
+
schemaHandler.openAPI.components.schemas.ErrorResponse
|
|
563
|
+
).to.be.an("object");
|
|
564
|
+
expect(
|
|
565
|
+
schemaHandler.openAPI.components.schemas.ErrorResponse
|
|
566
|
+
).to.be.eql({
|
|
567
|
+
type: "object",
|
|
568
|
+
properties: { error: { type: "string" } },
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
expect(schemaHandler.openAPI.components.schemas).to.have.property(
|
|
572
|
+
"SuccessResponse"
|
|
573
|
+
);
|
|
574
|
+
expect(
|
|
575
|
+
schemaHandler.openAPI.components.schemas.SuccessResponse
|
|
576
|
+
).to.be.an("object");
|
|
577
|
+
expect(
|
|
578
|
+
schemaHandler.openAPI.components.schemas.SuccessResponse
|
|
579
|
+
).to.be.eql({
|
|
580
|
+
type: "object",
|
|
581
|
+
properties: {
|
|
582
|
+
memberId: {
|
|
583
|
+
type: "string",
|
|
584
|
+
},
|
|
585
|
+
createdAt: {
|
|
586
|
+
type: "integer",
|
|
587
|
+
},
|
|
588
|
+
},
|
|
589
|
+
});
|
|
334
590
|
});
|
|
335
591
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
592
|
+
it(`should add a complex model that is a webUrl to the openAPI schema`, async function () {
|
|
593
|
+
Object.assign(
|
|
594
|
+
mockServerless.service.custom.documentation,
|
|
595
|
+
modelsDocument
|
|
596
|
+
);
|
|
597
|
+
mockServerless.service.custom.documentation.models.push({
|
|
598
|
+
name: "SuccessResponse",
|
|
599
|
+
contentType: "application/json",
|
|
600
|
+
schema: "https://google.com/build/LicensedMember.json",
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
nock("https://google.com")
|
|
604
|
+
.get("/build/LicensedMember.json")
|
|
605
|
+
.reply(200, {
|
|
606
|
+
type: "object",
|
|
607
|
+
properties: {
|
|
608
|
+
street_address: {
|
|
609
|
+
type: "string",
|
|
610
|
+
},
|
|
611
|
+
country: {
|
|
612
|
+
default: "United States of America",
|
|
613
|
+
enum: ["United States of America", "Canada"],
|
|
614
|
+
},
|
|
615
|
+
},
|
|
616
|
+
if: {
|
|
617
|
+
properties: { country: { const: "United States of America" } },
|
|
618
|
+
},
|
|
619
|
+
then: {
|
|
620
|
+
properties: {
|
|
621
|
+
postal_code: { pattern: "[0-9]{5}(-[0-9]{4})?" },
|
|
622
|
+
},
|
|
623
|
+
},
|
|
624
|
+
else: {
|
|
625
|
+
properties: {
|
|
626
|
+
postal_code: { pattern: "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" },
|
|
627
|
+
},
|
|
628
|
+
},
|
|
341
629
|
});
|
|
342
630
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
631
|
+
const schemaHandler = new SchemaHandler(
|
|
632
|
+
mockServerless,
|
|
633
|
+
openAPI,
|
|
634
|
+
logger
|
|
635
|
+
);
|
|
636
|
+
|
|
637
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
638
|
+
|
|
639
|
+
expect(schemaHandler.openAPI).to.have.property("components");
|
|
640
|
+
expect(schemaHandler.openAPI.components).to.have.property("schemas");
|
|
641
|
+
expect(schemaHandler.openAPI.components.schemas).to.have.property(
|
|
642
|
+
"ErrorResponse"
|
|
643
|
+
);
|
|
644
|
+
expect(
|
|
645
|
+
schemaHandler.openAPI.components.schemas.ErrorResponse
|
|
646
|
+
).to.be.an("object");
|
|
647
|
+
expect(
|
|
648
|
+
schemaHandler.openAPI.components.schemas.ErrorResponse
|
|
649
|
+
).to.be.eql({
|
|
650
|
+
type: "object",
|
|
651
|
+
properties: { error: { type: "string" } },
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
expect(schemaHandler.openAPI.components.schemas).to.have.property(
|
|
655
|
+
"SuccessResponse"
|
|
656
|
+
);
|
|
657
|
+
expect(
|
|
658
|
+
schemaHandler.openAPI.components.schemas.SuccessResponse
|
|
659
|
+
).to.be.an("object");
|
|
660
|
+
expect(
|
|
661
|
+
schemaHandler.openAPI.components.schemas.SuccessResponse.properties
|
|
662
|
+
).to.be.eql({
|
|
663
|
+
street_address: {
|
|
664
|
+
type: "string",
|
|
665
|
+
},
|
|
666
|
+
country: {
|
|
667
|
+
default: "United States of America",
|
|
668
|
+
enum: ["United States of America", "Canada"],
|
|
669
|
+
},
|
|
670
|
+
});
|
|
671
|
+
expect(
|
|
672
|
+
schemaHandler.openAPI.components.schemas.SuccessResponse
|
|
673
|
+
).to.have.property("oneOf");
|
|
674
|
+
expect(
|
|
675
|
+
schemaHandler.openAPI.components.schemas.SuccessResponse.oneOf
|
|
676
|
+
.length
|
|
677
|
+
).to.be.equal(2);
|
|
678
|
+
expect(
|
|
679
|
+
Object.keys(schemaHandler.openAPI.components.schemas).length
|
|
680
|
+
).to.be.equal(3);
|
|
681
|
+
});
|
|
394
682
|
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
const schemaHandler = new SchemaHandler(mockServerless, openAPI)
|
|
421
|
-
|
|
422
|
-
await schemaHandler.addModelsToOpenAPI()
|
|
423
|
-
|
|
424
|
-
expect(schemaHandler.openAPI).to.have.property('components')
|
|
425
|
-
expect(schemaHandler.openAPI.components).to.have.property('schemas')
|
|
426
|
-
expect(schemaHandler.openAPI.components.schemas).to.have.property('ErrorResponse')
|
|
427
|
-
expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an('object')
|
|
428
|
-
expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
|
|
429
|
-
|
|
430
|
-
expect(schemaHandler.openAPI.components.schemas).to.have.property('SuccessResponse')
|
|
431
|
-
expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.an('object')
|
|
432
|
-
expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.eql({
|
|
433
|
-
type: 'object',
|
|
434
|
-
properties: {
|
|
435
|
-
memberId: {
|
|
436
|
-
type: 'string'
|
|
437
|
-
},
|
|
438
|
-
createdAt: {
|
|
439
|
-
type: 'integer'
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
})
|
|
443
|
-
});
|
|
444
|
-
|
|
445
|
-
it(`should add a complex model that is a webUrl to the openAPI schema`, async function() {
|
|
446
|
-
Object.assign(mockServerless.service.custom.documentation, modelsDocument)
|
|
447
|
-
mockServerless.service.custom.documentation.models.push(
|
|
448
|
-
{
|
|
449
|
-
name: 'SuccessResponse',
|
|
450
|
-
contentType: 'application/json',
|
|
451
|
-
schema: 'https://google.com/build/LicensedMember.json'
|
|
452
|
-
}
|
|
453
|
-
)
|
|
454
|
-
|
|
455
|
-
nock('https://google.com')
|
|
456
|
-
.get('/build/LicensedMember.json')
|
|
457
|
-
.reply(200, {
|
|
458
|
-
"type": "object",
|
|
459
|
-
"properties": {
|
|
460
|
-
"street_address": {
|
|
461
|
-
"type": "string"
|
|
462
|
-
},
|
|
463
|
-
"country": {
|
|
464
|
-
"default": "United States of America",
|
|
465
|
-
"enum": ["United States of America", "Canada"]
|
|
466
|
-
}
|
|
467
|
-
},
|
|
468
|
-
"if": {
|
|
469
|
-
"properties": { "country": { "const": "United States of America" } }
|
|
470
|
-
},
|
|
471
|
-
"then": {
|
|
472
|
-
"properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
|
|
473
|
-
},
|
|
474
|
-
"else": {
|
|
475
|
-
"properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
|
|
476
|
-
}
|
|
477
|
-
})
|
|
478
|
-
|
|
479
|
-
const schemaHandler = new SchemaHandler(mockServerless, openAPI)
|
|
480
|
-
|
|
481
|
-
await schemaHandler.addModelsToOpenAPI()
|
|
482
|
-
|
|
483
|
-
expect(schemaHandler.openAPI).to.have.property('components')
|
|
484
|
-
expect(schemaHandler.openAPI.components).to.have.property('schemas')
|
|
485
|
-
expect(schemaHandler.openAPI.components.schemas).to.have.property('ErrorResponse')
|
|
486
|
-
expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an('object')
|
|
487
|
-
expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
|
|
488
|
-
|
|
489
|
-
expect(schemaHandler.openAPI.components.schemas).to.have.property('SuccessResponse')
|
|
490
|
-
expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.an('object')
|
|
491
|
-
expect(schemaHandler.openAPI.components.schemas.SuccessResponse.properties).to.be.eql({
|
|
492
|
-
"street_address": {
|
|
493
|
-
"type": "string"
|
|
494
|
-
},
|
|
495
|
-
"country": {
|
|
496
|
-
"default": "United States of America",
|
|
497
|
-
"enum": ["United States of America", "Canada"]
|
|
498
|
-
}
|
|
499
|
-
})
|
|
500
|
-
expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.have.property('oneOf')
|
|
501
|
-
expect(schemaHandler.openAPI.components.schemas.SuccessResponse.oneOf.length).to.be.equal(2)
|
|
502
|
-
expect(Object.keys(schemaHandler.openAPI.components.schemas).length).to.be.equal(3)
|
|
503
|
-
});
|
|
504
|
-
|
|
505
|
-
it(`should throw when a webUrl returns a 404`, async function() {
|
|
506
|
-
Object.assign(mockServerless.service.custom.documentation, modelsDocument)
|
|
507
|
-
mockServerless.service.custom.documentation.models.push(
|
|
508
|
-
{
|
|
509
|
-
name: 'SuccessResponse',
|
|
510
|
-
contentType: 'application/json',
|
|
511
|
-
schema: 'https://google.com/build/LicensedMember.json'
|
|
512
|
-
}
|
|
513
|
-
)
|
|
514
|
-
|
|
515
|
-
nock('https://google.com')
|
|
516
|
-
.get('/build/LicensedMember.json')
|
|
517
|
-
.reply(404, {body: 'Bad Request'})
|
|
518
|
-
|
|
519
|
-
const schemaHandler = new SchemaHandler(mockServerless, openAPI)
|
|
520
|
-
|
|
521
|
-
await schemaHandler.addModelsToOpenAPI()
|
|
522
|
-
.catch(err => {
|
|
523
|
-
expect(err).to.not.be.undefined
|
|
524
|
-
})
|
|
525
|
-
});
|
|
526
|
-
});
|
|
683
|
+
it(`should throw when a webUrl returns a 404`, async function () {
|
|
684
|
+
Object.assign(
|
|
685
|
+
mockServerless.service.custom.documentation,
|
|
686
|
+
modelsDocument
|
|
687
|
+
);
|
|
688
|
+
mockServerless.service.custom.documentation.models.push({
|
|
689
|
+
name: "SuccessResponse",
|
|
690
|
+
contentType: "application/json",
|
|
691
|
+
schema: "https://google.com/build/LicensedMember.json",
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
nock("https://google.com")
|
|
695
|
+
.get("/build/LicensedMember.json")
|
|
696
|
+
.reply(404, { body: "Bad Request" });
|
|
697
|
+
|
|
698
|
+
const schemaHandler = new SchemaHandler(
|
|
699
|
+
mockServerless,
|
|
700
|
+
openAPI,
|
|
701
|
+
logger
|
|
702
|
+
);
|
|
703
|
+
|
|
704
|
+
await schemaHandler.addModelsToOpenAPI().catch((err) => {
|
|
705
|
+
expect(err).to.not.be.undefined;
|
|
706
|
+
});
|
|
527
707
|
});
|
|
708
|
+
});
|
|
528
709
|
});
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
describe(`createSchema`, function () {
|
|
713
|
+
it(`returns a reference to the schema when the schema already exists in components and we don't pass through a schema`, async function () {
|
|
714
|
+
Object.assign(
|
|
715
|
+
mockServerless.service.custom.documentation,
|
|
716
|
+
modelsDocument
|
|
717
|
+
);
|
|
718
|
+
const schemaHandler = new SchemaHandler(mockServerless, openAPI, logger);
|
|
529
719
|
|
|
530
|
-
|
|
531
|
-
it(`returns a reference to the schema when the schema already exists in components and we don't pass through a schema`, async function() {
|
|
532
|
-
Object.assign(mockServerless.service.custom.documentation, modelsDocument)
|
|
533
|
-
const schemaHandler = new SchemaHandler(mockServerless, openAPI)
|
|
720
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
534
721
|
|
|
535
|
-
|
|
722
|
+
const expected = await schemaHandler.createSchema("ErrorResponse");
|
|
536
723
|
|
|
537
|
-
|
|
724
|
+
expect(expected).to.be.equal("#/components/schemas/ErrorResponse");
|
|
725
|
+
});
|
|
538
726
|
|
|
539
|
-
|
|
727
|
+
it(`throws an error when the name of the schema does not exist in components and we don't pass through a schema`, async function () {
|
|
728
|
+
Object.assign(
|
|
729
|
+
mockServerless.service.custom.documentation,
|
|
730
|
+
modelsDocument
|
|
731
|
+
);
|
|
732
|
+
const schemaHandler = new SchemaHandler(mockServerless, openAPI, logger);
|
|
733
|
+
|
|
734
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
735
|
+
|
|
736
|
+
const expected = await schemaHandler
|
|
737
|
+
.createSchema("PUTRequest")
|
|
738
|
+
.catch((err) => {
|
|
739
|
+
expect(err).to.not.be.undefined;
|
|
740
|
+
expect(err.message).to.be.equal(
|
|
741
|
+
"Expected a file path, URL, or object. Got undefined"
|
|
742
|
+
);
|
|
540
743
|
});
|
|
541
744
|
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
const schemaHandler = new SchemaHandler(mockServerless, openAPI)
|
|
745
|
+
expect(expected).to.be.undefined;
|
|
746
|
+
});
|
|
545
747
|
|
|
546
|
-
|
|
748
|
+
it(`returns a reference to a schema when the schema does not exist in components already`, async function () {
|
|
749
|
+
Object.assign(
|
|
750
|
+
mockServerless.service.custom.documentation,
|
|
751
|
+
modelsDocument
|
|
752
|
+
);
|
|
753
|
+
const schemaHandler = new SchemaHandler(mockServerless, openAPI, logger);
|
|
754
|
+
|
|
755
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
756
|
+
const schema = {
|
|
757
|
+
type: "object",
|
|
758
|
+
properties: {
|
|
759
|
+
createdAt: {
|
|
760
|
+
type: "number",
|
|
761
|
+
},
|
|
762
|
+
},
|
|
763
|
+
};
|
|
764
|
+
const expected = await schemaHandler
|
|
765
|
+
.createSchema("PUTRequest", schema)
|
|
766
|
+
.catch((err) => {
|
|
767
|
+
expect(err).to.be.undefined;
|
|
768
|
+
});
|
|
547
769
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
770
|
+
expect(expected).to.be.equal("#/components/schemas/PUTRequest");
|
|
771
|
+
expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(
|
|
772
|
+
schema
|
|
773
|
+
);
|
|
774
|
+
});
|
|
553
775
|
|
|
554
|
-
|
|
776
|
+
it(`returns a reference to a schema when the schema exists in components already and the same schema is being passed through`, async function () {
|
|
777
|
+
Object.assign(
|
|
778
|
+
mockServerless.service.custom.documentation,
|
|
779
|
+
modelsDocument
|
|
780
|
+
);
|
|
781
|
+
const schemaHandler = new SchemaHandler(mockServerless, openAPI, logger);
|
|
782
|
+
|
|
783
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
784
|
+
const schema = {
|
|
785
|
+
type: "object",
|
|
786
|
+
properties: {
|
|
787
|
+
createdAt: {
|
|
788
|
+
type: "number",
|
|
789
|
+
},
|
|
790
|
+
},
|
|
791
|
+
};
|
|
792
|
+
let expected = await schemaHandler
|
|
793
|
+
.createSchema("PUTRequest", schema)
|
|
794
|
+
.catch((err) => {
|
|
795
|
+
expect(err).to.be.undefined;
|
|
555
796
|
});
|
|
556
797
|
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
798
|
+
expect(expected).to.be.equal("#/components/schemas/PUTRequest");
|
|
799
|
+
expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(
|
|
800
|
+
schema
|
|
801
|
+
);
|
|
560
802
|
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
createdAt: {
|
|
566
|
-
type: 'number'
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
const expected = await schemaHandler.createSchema('PUTRequest', schema)
|
|
571
|
-
.catch(err => {
|
|
572
|
-
expect(err).to.be.undefined
|
|
573
|
-
})
|
|
574
|
-
|
|
575
|
-
expect(expected).to.be.equal('#/components/schemas/PUTRequest')
|
|
576
|
-
expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(schema)
|
|
803
|
+
expected = await schemaHandler
|
|
804
|
+
.createSchema("PUTRequest", schema)
|
|
805
|
+
.catch((err) => {
|
|
806
|
+
expect(err).to.be.undefined;
|
|
577
807
|
});
|
|
578
808
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
809
|
+
expect(expected).to.be.equal("#/components/schemas/PUTRequest");
|
|
810
|
+
expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(
|
|
811
|
+
schema
|
|
812
|
+
);
|
|
813
|
+
});
|
|
582
814
|
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
})
|
|
604
|
-
|
|
605
|
-
expect(expected).to.be.equal('#/components/schemas/PUTRequest')
|
|
606
|
-
expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(schema)
|
|
815
|
+
it(`returns a reference to a new schema when the schema exists in components already and a different schema is being passed through`, async function () {
|
|
816
|
+
Object.assign(
|
|
817
|
+
mockServerless.service.custom.documentation,
|
|
818
|
+
modelsDocument
|
|
819
|
+
);
|
|
820
|
+
const schemaHandler = new SchemaHandler(mockServerless, openAPI, logger);
|
|
821
|
+
|
|
822
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
823
|
+
const schema = {
|
|
824
|
+
type: "object",
|
|
825
|
+
properties: {
|
|
826
|
+
createdAt: {
|
|
827
|
+
type: "number",
|
|
828
|
+
},
|
|
829
|
+
},
|
|
830
|
+
};
|
|
831
|
+
let expected = await schemaHandler
|
|
832
|
+
.createSchema("PUTRequest", schema)
|
|
833
|
+
.catch((err) => {
|
|
834
|
+
expect(err).to.be.undefined;
|
|
607
835
|
});
|
|
608
836
|
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
expect(expected).to.be.equal('#/components/schemas/PUTRequest')
|
|
628
|
-
expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(schema)
|
|
629
|
-
|
|
630
|
-
const differentSchema = {
|
|
631
|
-
type: 'object',
|
|
632
|
-
properties: {
|
|
633
|
-
updatedAt: {
|
|
634
|
-
type: 'number'
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
expected = await schemaHandler.createSchema('PUTRequest', differentSchema)
|
|
640
|
-
.catch(err => {
|
|
641
|
-
expect(err).to.be.undefined
|
|
642
|
-
})
|
|
643
|
-
|
|
644
|
-
const splitPath = expected.split('/')
|
|
645
|
-
expect(v4.test(splitPath[3].split('PUTRequest-')[1])).to.be.true
|
|
646
|
-
expect(expected).to.be.equal(`#/components/schemas/${splitPath[3]}`)
|
|
647
|
-
expect(schemaHandler.openAPI.components.schemas[splitPath[3]]).to.be.eql(differentSchema)
|
|
837
|
+
expect(expected).to.be.equal("#/components/schemas/PUTRequest");
|
|
838
|
+
expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(
|
|
839
|
+
schema
|
|
840
|
+
);
|
|
841
|
+
|
|
842
|
+
const differentSchema = {
|
|
843
|
+
type: "object",
|
|
844
|
+
properties: {
|
|
845
|
+
updatedAt: {
|
|
846
|
+
type: "number",
|
|
847
|
+
},
|
|
848
|
+
},
|
|
849
|
+
};
|
|
850
|
+
|
|
851
|
+
expected = await schemaHandler
|
|
852
|
+
.createSchema("PUTRequest", differentSchema)
|
|
853
|
+
.catch((err) => {
|
|
854
|
+
expect(err).to.be.undefined;
|
|
648
855
|
});
|
|
649
856
|
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
857
|
+
const splitPath = expected.split("/");
|
|
858
|
+
expect(v4.test(splitPath[3].split("PUTRequest-")[1])).to.be.true;
|
|
859
|
+
expect(expected).to.be.equal(`#/components/schemas/${splitPath[3]}`);
|
|
860
|
+
expect(schemaHandler.openAPI.components.schemas[splitPath[3]]).to.be.eql(
|
|
861
|
+
differentSchema
|
|
862
|
+
);
|
|
863
|
+
});
|
|
653
864
|
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
865
|
+
it(`returns a reference to a new schema when the schema passed through is a URL`, async function () {
|
|
866
|
+
Object.assign(
|
|
867
|
+
mockServerless.service.custom.documentation,
|
|
868
|
+
modelsDocument
|
|
869
|
+
);
|
|
870
|
+
const schemaHandler = new SchemaHandler(mockServerless, openAPI, logger);
|
|
871
|
+
|
|
872
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
873
|
+
const schema = "https://google.com/build/LicensedMember.json";
|
|
874
|
+
const schemaObj = {
|
|
875
|
+
type: "object",
|
|
876
|
+
properties: {
|
|
877
|
+
name: {
|
|
878
|
+
type: "string",
|
|
879
|
+
},
|
|
880
|
+
address: {
|
|
881
|
+
type: "string",
|
|
882
|
+
},
|
|
883
|
+
},
|
|
884
|
+
};
|
|
885
|
+
|
|
886
|
+
nock("https://google.com")
|
|
887
|
+
.get("/build/LicensedMember.json")
|
|
888
|
+
.reply(200, schemaObj);
|
|
889
|
+
|
|
890
|
+
let expected = await schemaHandler
|
|
891
|
+
.createSchema("PUTRequest", schema)
|
|
892
|
+
.catch((err) => {
|
|
893
|
+
expect(err).to.be.undefined;
|
|
679
894
|
});
|
|
680
895
|
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
896
|
+
expect(expected).to.be.equal("#/components/schemas/PUTRequest");
|
|
897
|
+
expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(
|
|
898
|
+
schemaObj
|
|
899
|
+
);
|
|
900
|
+
});
|
|
684
901
|
|
|
685
|
-
|
|
686
|
-
|
|
902
|
+
it(`should throw an error when a schema as a URL can not be resolved correctly`, async function () {
|
|
903
|
+
Object.assign(
|
|
904
|
+
mockServerless.service.custom.documentation,
|
|
905
|
+
modelsDocument
|
|
906
|
+
);
|
|
907
|
+
const schemaHandler = new SchemaHandler(mockServerless, openAPI, logger);
|
|
687
908
|
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
.reply(404)
|
|
909
|
+
await schemaHandler.addModelsToOpenAPI();
|
|
910
|
+
const schema = "https://google.com/build/LicensedMember.json";
|
|
691
911
|
|
|
692
|
-
|
|
693
|
-
.catch(err => {
|
|
694
|
-
expect(err).to.not.undefined
|
|
695
|
-
})
|
|
912
|
+
nock("https://google.com").get("/build/LicensedMember.json").reply(404);
|
|
696
913
|
|
|
697
|
-
|
|
914
|
+
let expected = await schemaHandler
|
|
915
|
+
.createSchema("PUTRequest", schema)
|
|
916
|
+
.catch((err) => {
|
|
917
|
+
expect(err).to.not.undefined;
|
|
698
918
|
});
|
|
919
|
+
|
|
920
|
+
expect(expected).to.be.undefined;
|
|
699
921
|
});
|
|
922
|
+
});
|
|
700
923
|
});
|