serverless-openapi-documenter 0.0.110 → 0.0.112

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