serverless-openapi-documenter 0.0.119 → 0.0.121

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,923 +0,0 @@
1
- "use strict";
2
-
3
- const fs = require("fs").promises;
4
- const path = require("path");
5
-
6
- const expect = require("chai").expect;
7
- const nock = require("nock");
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");
16
-
17
- describe(`SchemaHandler`, function () {
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);
58
- });
59
-
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" } },
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);
152
-
153
- expect(expected.models).to.be.an("array");
154
- expect(expected.models.length).to.be.equal(2);
155
-
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
- });
163
-
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);
185
-
186
- expect(expected.models).to.be.an("array");
187
- expect(expected.models.length).to.be.equal(2);
188
-
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
- });
196
-
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
- });
221
-
222
- const expected = new SchemaHandler(mockServerless, openAPI, logger);
223
-
224
- expect(expected.models).to.be.an("array");
225
- expect(expected.models.length).to.be.equal(2);
226
-
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" });
231
-
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
- });
241
-
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
- });
261
-
262
- expected = new SchemaHandler(mockServerless, openAPI, logger);
263
- expect(expected.refParserOptions).to.be.an("object");
264
- expect(expected.refParserOptions).to.have.property("continueOnError");
265
-
266
- await fs.rm(path.resolve("options/ref-parser.js")).catch((err) => {
267
- console.error(err);
268
- throw err;
269
- });
270
-
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
- });
358
-
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" } },
426
- });
427
-
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" } },
439
- });
440
- });
441
- });
442
-
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
- },
546
- });
547
-
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
- });
590
- });
591
-
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
- },
629
- });
630
-
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
- });
682
-
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
- });
707
- });
708
- });
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);
719
-
720
- await schemaHandler.addModelsToOpenAPI();
721
-
722
- const expected = await schemaHandler.createSchema("ErrorResponse");
723
-
724
- expect(expected).to.be.equal("#/components/schemas/ErrorResponse");
725
- });
726
-
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
- );
743
- });
744
-
745
- expect(expected).to.be.undefined;
746
- });
747
-
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
- });
769
-
770
- expect(expected).to.be.equal("#/components/schemas/PUTRequest");
771
- expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(
772
- schema
773
- );
774
- });
775
-
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;
796
- });
797
-
798
- expect(expected).to.be.equal("#/components/schemas/PUTRequest");
799
- expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(
800
- schema
801
- );
802
-
803
- expected = await schemaHandler
804
- .createSchema("PUTRequest", schema)
805
- .catch((err) => {
806
- expect(err).to.be.undefined;
807
- });
808
-
809
- expect(expected).to.be.equal("#/components/schemas/PUTRequest");
810
- expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(
811
- schema
812
- );
813
- });
814
-
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;
835
- });
836
-
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;
855
- });
856
-
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
- });
864
-
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;
894
- });
895
-
896
- expect(expected).to.be.equal("#/components/schemas/PUTRequest");
897
- expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(
898
- schemaObj
899
- );
900
- });
901
-
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);
908
-
909
- await schemaHandler.addModelsToOpenAPI();
910
- const schema = "https://google.com/build/LicensedMember.json";
911
-
912
- nock("https://google.com").get("/build/LicensedMember.json").reply(404);
913
-
914
- let expected = await schemaHandler
915
- .createSchema("PUTRequest", schema)
916
- .catch((err) => {
917
- expect(err).to.not.undefined;
918
- });
919
-
920
- expect(expected).to.be.undefined;
921
- });
922
- });
923
- });