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.
@@ -1,700 +1,923 @@
1
- 'use strict'
1
+ "use strict";
2
2
 
3
- const fs = require('fs').promises
4
- const path = require('path')
3
+ const fs = require("fs").promises;
4
+ const path = require("path");
5
5
 
6
- const expect = require('chai').expect
7
- const nock = require('nock')
6
+ const expect = require("chai").expect;
7
+ const nock = require("nock");
8
8
 
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')
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
- let mockServerless
20
- let openAPI
21
- let modelsDocument, modelsAltDocument, modelsListDocument, modelsListAltDocument
22
- const v4 = new RegExp(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i)
23
- const openAPISchema = {
24
- version: '3.0.3',
25
- components: {
26
- schemas: {}
27
- }
28
- }
29
-
30
- beforeEach(function() {
31
- mockServerless = JSON.parse(JSON.stringify(serverlessMock))
32
- modelsDocument = JSON.parse(JSON.stringify(modelsDocumentOG))
33
- modelsAltDocument = JSON.parse(JSON.stringify(modelsAltDocumentOG))
34
- modelsListDocument = JSON.parse(JSON.stringify(modelsListDocumentOG))
35
- modelsListAltDocument = JSON.parse(JSON.stringify(modelsListAltDocumentOG))
36
- openAPI = JSON.parse(JSON.stringify(openAPISchema))
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(`constuctor`, function () {
40
- it('should return an instance of SchemaHandler', function() {
41
- const expected = new SchemaHandler(mockServerless, openAPI)
42
- expect(expected).to.be.an.instanceOf(SchemaHandler)
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
- describe(`standardising the models`, function () {
46
- it(`should standardise models syntax in to the correct format`, function() {
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
- expect(expected.models).to.be.an('array')
51
- expect(expected.models.length).to.be.equal(1)
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
- expect(expected.models[0].name).to.equal('ErrorResponse')
54
- expect(expected.models[0]).to.have.property('contentType')
55
- expect(expected.models[0]).to.have.property('schema')
56
- expect(expected.models[0].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
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
- it(`should standardise alternative models syntax in to the correct format`, function() {
60
- Object.assign(mockServerless.service.custom.documentation, modelsAltDocument)
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
- expect(expected.models).to.be.an('array')
64
- expect(expected.models.length).to.be.equal(1)
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
- expect(expected.models[0].name).to.equal('ErrorResponse')
67
- expect(expected.models[0]).to.have.property('contentType')
68
- expect(expected.models[0]).to.have.property('schema')
69
- expect(expected.models[0].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
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
- it(`should standardise modelsList syntax in to the correct format`, function() {
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
- expect(expected.models).to.be.an('array')
77
- expect(expected.models.length).to.be.equal(1)
224
+ expect(expected.models).to.be.an("array");
225
+ expect(expected.models.length).to.be.equal(2);
78
226
 
79
- expect(expected.models[0].name).to.equal('ErrorResponse')
80
- expect(expected.models[0]).to.have.property('contentType')
81
- expect(expected.models[0]).to.have.property('schema')
82
- expect(expected.models[0].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
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
- it(`should standardise alternative modelsList syntax in to the correct format`, function() {
86
- Object.assign(mockServerless.service.custom.documentation, modelsListAltDocument)
87
- const expected = new SchemaHandler(mockServerless, openAPI)
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
- expect(expected.models).to.be.an('array')
90
- expect(expected.models.length).to.be.equal(1)
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
- expect(expected.models[0].name).to.equal('ErrorResponse')
93
- expect(expected.models[0]).to.have.property('contentType')
94
- expect(expected.models[0]).to.have.property('schema')
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
- it(`should standardise mixed models syntax in to the correct format`, function() {
99
- const newModelsDocument = JSON.parse(JSON.stringify(modelsDocument))
100
- Object.assign(mockServerless.service.custom.documentation, newModelsDocument)
101
- mockServerless.service.custom.documentation.models.push(
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
- it(`should standardise mixed modelsList syntax in to the correct format`, function() {
128
- const newModelsDocument = JSON.parse(JSON.stringify(modelsListDocument))
129
- Object.assign(mockServerless.service.custom.documentation, newModelsDocument)
130
- mockServerless.service.custom.documentation.modelsList.push(
131
- {
132
- name: 'SuccessResponse',
133
- description: 'A success response',
134
- contentType: 'application/json',
135
- schema: {
136
- type: 'string'
137
- }
138
- }
139
- )
140
- const expected = new SchemaHandler(mockServerless, openAPI)
141
-
142
- expect(expected.models).to.be.an('array')
143
- expect(expected.models.length).to.be.equal(2)
144
-
145
- expect(expected.models[0].name).to.equal('ErrorResponse')
146
- expect(expected.models[0]).to.have.property('contentType')
147
- expect(expected.models[0]).to.have.property('schema')
148
- expect(expected.models[0].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
149
-
150
- expect(expected.models[1].name).to.equal('SuccessResponse')
151
- expect(expected.models[1]).to.have.property('contentType')
152
- expect(expected.models[1]).to.have.property('schema')
153
- expect(expected.models[1].schema).to.be.eql({type: 'string'})
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
- it(`should standardise mixed models and modelsList syntax in to the correct format`, function() {
157
- const newModelsDocument = JSON.parse(JSON.stringify(modelsListDocument))
158
- Object.assign(mockServerless.service.custom.documentation, newModelsDocument)
159
- Object.assign(
160
- mockServerless.service.custom.documentation,
161
- {
162
- models: [
163
- {
164
- name: 'SuccessResponse',
165
- description: 'A success response',
166
- contentType: 'application/json',
167
- schema: {
168
- type: 'string'
169
- }
170
- }
171
- ]
172
- }
173
- )
174
-
175
- const expected = new SchemaHandler(mockServerless, openAPI)
176
-
177
- expect(expected.models).to.be.an('array')
178
- expect(expected.models.length).to.be.equal(2)
179
-
180
- expect(expected.models[0].name).to.equal('SuccessResponse')
181
- expect(expected.models[0]).to.have.property('contentType')
182
- expect(expected.models[0]).to.have.property('schema')
183
- expect(expected.models[0].schema).to.be.eql({type: 'string'})
184
-
185
- expect(expected.models[1].name).to.equal('ErrorResponse')
186
- expect(expected.models[1]).to.have.property('contentType')
187
- expect(expected.models[1]).to.have.property('schema')
188
- expect(expected.models[1].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
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
- it(`should correctly resolve the RefParserOptions`, async function() {
193
- let expected = new SchemaHandler(mockServerless, openAPI)
194
- expect(expected.refParserOptions).to.be.an('object')
195
- expect(expected.refParserOptions).to.be.empty
196
-
197
- await fs.mkdir(path.resolve('options'))
198
- .catch(err => {
199
- console.error(err)
200
- throw err
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(`addModelsToOpenAPI`, function () {
228
- describe(`embedded simple schemas`, function () {
229
- it(`should add the model to the openAPI schema`, async function() {
230
- Object.assign(mockServerless.service.custom.documentation, modelsDocument)
231
- const schemaHandler = new SchemaHandler(mockServerless, openAPI)
232
-
233
- await schemaHandler.addModelsToOpenAPI()
234
-
235
- expect(schemaHandler.openAPI).to.have.property('components')
236
- expect(schemaHandler.openAPI.components).to.have.property('schemas')
237
- expect(schemaHandler.openAPI.components.schemas).to.have.property('ErrorResponse')
238
- expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an('object')
239
- expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
240
- });
241
-
242
- it(`should add a model with references to the openAPI schema`, async function() {
243
- Object.assign(mockServerless.service.custom.documentation, modelsDocument)
244
- mockServerless.service.custom.documentation.models.push(
245
- {
246
- name: 'SuccessResponse',
247
- contentType: 'application/json',
248
- schema: {
249
- type: 'object',
250
- properties: {
251
- name: {
252
- '$ref': '#/definitions/nameObject'
253
- }
254
- },
255
- definitions: {
256
- nameObject: {
257
- type: 'object',
258
- properties: {
259
- firstName: {
260
- type: 'string'
261
- }
262
- }
263
- }
264
- }
265
- }
266
- }
267
- )
268
- const schemaHandler = new SchemaHandler(mockServerless, openAPI)
269
-
270
- await schemaHandler.addModelsToOpenAPI()
271
-
272
- expect(schemaHandler.openAPI).to.have.property('components')
273
- expect(schemaHandler.openAPI.components).to.have.property('schemas')
274
- expect(schemaHandler.openAPI.components.schemas).to.have.property('ErrorResponse')
275
- expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an('object')
276
- expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
277
-
278
- expect(schemaHandler.openAPI.components.schemas).to.have.property('SuccessResponse')
279
- expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.an('object')
280
- expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.eql({
281
- type: 'object',
282
- properties: {
283
- name: {
284
- type: 'object',
285
- properties: {
286
- firstName: {
287
- type: 'string'
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
- it(`should add a model with poorly dereferenced references to the openAPI schema`, async function() {
296
- Object.assign(mockServerless.service.custom.documentation, modelsDocument)
297
- mockServerless.service.custom.documentation.models.push(
298
- {
299
- name: 'SuccessResponse',
300
- contentType: 'application/json',
301
- schema: {
302
- type: 'object',
303
- '$ref': '#/definitions/nameObject',
304
- definitions: {
305
- nameObject: {
306
- type: 'object',
307
- properties: {
308
- firstName: {
309
- type: 'string'
310
- }
311
- }
312
- }
313
- }
314
- }
315
- }
316
- )
317
- const schemaHandler = new SchemaHandler(mockServerless, openAPI)
318
-
319
- await schemaHandler.addModelsToOpenAPI()
320
-
321
- expect(schemaHandler.openAPI).to.have.property('components')
322
- expect(schemaHandler.openAPI.components).to.have.property('schemas')
323
- expect(schemaHandler.openAPI.components.schemas).to.have.property('ErrorResponse')
324
- expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an('object')
325
- expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
326
-
327
- expect(schemaHandler.openAPI.components.schemas).to.have.property('SuccessResponse')
328
- expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.an('object')
329
- expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.eql({
330
- type: 'object',
331
- properties: { firstName: { type: 'string' } }
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
- describe(`schemas with references`, function () {
337
- describe(`file references`, function () {
338
- it(`should add schemas with file references to the openAPI schema`, async function() {
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
- describe(`component references`, function () {
344
- it(`should add schemas with component references to the openAPI schema`, async function() {
345
- Object.assign(mockServerless.service.custom.documentation, modelsDocument)
346
- mockServerless.service.custom.documentation.models.push(
347
- {
348
- name: 'SuccessResponse',
349
- contentType: 'application/json',
350
- schema: {
351
- type: 'array',
352
- items: {
353
- $ref: '#/components/schemas/Agency'
354
- }
355
- }
356
- }
357
- )
358
- mockServerless.service.custom.documentation.models.push(
359
- {
360
- name: 'Agency',
361
- contentType: 'application/json',
362
- schema: {
363
- type: 'string'
364
- }
365
- }
366
- )
367
-
368
- const schemaHandler = new SchemaHandler(mockServerless, openAPI)
369
-
370
- await schemaHandler.addModelsToOpenAPI()
371
-
372
- expect(schemaHandler.openAPI).to.have.property('components')
373
- expect(schemaHandler.openAPI.components).to.have.property('schemas')
374
- expect(schemaHandler.openAPI.components.schemas).to.have.property('ErrorResponse')
375
- expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an('object')
376
- expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
377
-
378
- expect(schemaHandler.openAPI.components.schemas).to.have.property('SuccessResponse')
379
- expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.an('object')
380
- expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.eql({
381
- type: 'array',
382
- items: {
383
- $ref: '#/components/schemas/Agency'
384
- }
385
- })
386
-
387
- expect(schemaHandler.openAPI.components.schemas).to.have.property('Agency')
388
- expect(schemaHandler.openAPI.components.schemas.Agency).to.be.an('object')
389
- expect(schemaHandler.openAPI.components.schemas.Agency).to.be.eql({
390
- type: 'string',
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
- describe(`other references`, function () {
396
- it(`should add a model that is a webUrl to the openAPI schema`, async function() {
397
- Object.assign(mockServerless.service.custom.documentation, modelsDocument)
398
- mockServerless.service.custom.documentation.models.push(
399
- {
400
- name: 'SuccessResponse',
401
- contentType: 'application/json',
402
- schema: 'https://google.com/build/LicensedMember.json'
403
- }
404
- )
405
-
406
- nock('https://google.com')
407
- .get('/build/LicensedMember.json')
408
- .reply(200, {
409
- type: 'object',
410
- properties: {
411
- memberId: {
412
- type: 'string'
413
- },
414
- createdAt: {
415
- type: 'integer'
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
- describe(`createSchema`, function () {
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
- await schemaHandler.addModelsToOpenAPI()
722
+ const expected = await schemaHandler.createSchema("ErrorResponse");
536
723
 
537
- const expected = await schemaHandler.createSchema('ErrorResponse')
724
+ expect(expected).to.be.equal("#/components/schemas/ErrorResponse");
725
+ });
538
726
 
539
- expect(expected).to.be.equal('#/components/schemas/ErrorResponse')
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
- 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() {
543
- Object.assign(mockServerless.service.custom.documentation, modelsDocument)
544
- const schemaHandler = new SchemaHandler(mockServerless, openAPI)
745
+ expect(expected).to.be.undefined;
746
+ });
545
747
 
546
- await schemaHandler.addModelsToOpenAPI()
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
- const expected = await schemaHandler.createSchema('PUTRequest')
549
- .catch(err => {
550
- expect(err).to.not.be.undefined
551
- expect(err.message).to.be.equal('Expected a file path, URL, or object. Got undefined')
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
- expect(expected).to.be.undefined
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
- it(`returns a reference to a schema when the schema does not exist in components already`, async function() {
558
- Object.assign(mockServerless.service.custom.documentation, modelsDocument)
559
- const schemaHandler = new SchemaHandler(mockServerless, openAPI)
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
- await schemaHandler.addModelsToOpenAPI()
562
- const schema = {
563
- type: 'object',
564
- properties: {
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
- it(`returns a reference to a schema when the schema exists in components already and the same schema is being passed through`, async function() {
580
- Object.assign(mockServerless.service.custom.documentation, modelsDocument)
581
- const schemaHandler = new SchemaHandler(mockServerless, openAPI)
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
- await schemaHandler.addModelsToOpenAPI()
584
- const schema = {
585
- type: 'object',
586
- properties: {
587
- createdAt: {
588
- type: 'number'
589
- }
590
- }
591
- }
592
- let expected = await schemaHandler.createSchema('PUTRequest', schema)
593
- .catch(err => {
594
- expect(err).to.be.undefined
595
- })
596
-
597
- expect(expected).to.be.equal('#/components/schemas/PUTRequest')
598
- expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(schema)
599
-
600
- expected = await schemaHandler.createSchema('PUTRequest', schema)
601
- .catch(err => {
602
- expect(err).to.be.undefined
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
- 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() {
610
- Object.assign(mockServerless.service.custom.documentation, modelsDocument)
611
- const schemaHandler = new SchemaHandler(mockServerless, openAPI)
612
-
613
- await schemaHandler.addModelsToOpenAPI()
614
- const schema = {
615
- type: 'object',
616
- properties: {
617
- createdAt: {
618
- type: 'number'
619
- }
620
- }
621
- }
622
- let expected = await schemaHandler.createSchema('PUTRequest', schema)
623
- .catch(err => {
624
- expect(err).to.be.undefined
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
- it(`returns a reference to a new schema when the schema passed through is a URL`, async function() {
651
- Object.assign(mockServerless.service.custom.documentation, modelsDocument)
652
- const schemaHandler = new SchemaHandler(mockServerless, openAPI)
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
- await schemaHandler.addModelsToOpenAPI()
655
- const schema = 'https://google.com/build/LicensedMember.json'
656
- const schemaObj = {
657
- type: 'object',
658
- properties: {
659
- name: {
660
- type: 'string'
661
- },
662
- address: {
663
- type: 'string'
664
- }
665
- }
666
- }
667
-
668
- nock('https://google.com')
669
- .get('/build/LicensedMember.json')
670
- .reply(200, schemaObj)
671
-
672
- let expected = await schemaHandler.createSchema('PUTRequest', schema)
673
- .catch(err => {
674
- expect(err).to.be.undefined
675
- })
676
-
677
- expect(expected).to.be.equal('#/components/schemas/PUTRequest')
678
- expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(schemaObj)
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
- it(`should throw an error when a schema as a URL can not be resolved correctly`, async function() {
682
- Object.assign(mockServerless.service.custom.documentation, modelsDocument)
683
- const schemaHandler = new SchemaHandler(mockServerless, openAPI)
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
- await schemaHandler.addModelsToOpenAPI()
686
- const schema = 'https://google.com/build/LicensedMember.json'
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
- nock('https://google.com')
689
- .get('/build/LicensedMember.json')
690
- .reply(404)
909
+ await schemaHandler.addModelsToOpenAPI();
910
+ const schema = "https://google.com/build/LicensedMember.json";
691
911
 
692
- let expected = await schemaHandler.createSchema('PUTRequest', schema)
693
- .catch(err => {
694
- expect(err).to.not.undefined
695
- })
912
+ nock("https://google.com").get("/build/LicensedMember.json").reply(404);
696
913
 
697
- expect(expected).to.be.undefined
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
  });