serverless-openapi-documenter 0.0.120-beta.1 → 0.0.123

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