@scalar/oas-utils 0.3.2 → 0.4.1

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.
Files changed (80) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/entities/spec/collection.d.ts +6 -12
  3. package/dist/entities/spec/collection.d.ts.map +1 -1
  4. package/dist/entities/spec/collection.js +1 -1
  5. package/dist/entities/spec/collection.js.map +2 -2
  6. package/dist/helpers/http-methods.js +9 -9
  7. package/dist/helpers/http-methods.js.map +1 -1
  8. package/dist/helpers/pretty-print-json.d.ts.map +1 -1
  9. package/dist/helpers/pretty-print-json.js.map +2 -2
  10. package/dist/transforms/import-spec.d.ts +8 -3
  11. package/dist/transforms/import-spec.d.ts.map +1 -1
  12. package/dist/transforms/import-spec.js +30 -9
  13. package/dist/transforms/import-spec.js.map +2 -2
  14. package/package.json +5 -5
  15. package/dist/entities/spec/operation.test.js +0 -43
  16. package/dist/entities/spec/operation.test.js.map +0 -7
  17. package/dist/entities/spec/parameters.test.js +0 -80
  18. package/dist/entities/spec/parameters.test.js.map +0 -7
  19. package/dist/entities/spec/request-example.test.js +0 -633
  20. package/dist/entities/spec/request-example.test.js.map +0 -7
  21. package/dist/entities/spec/server.test.js +0 -120
  22. package/dist/entities/spec/server.test.js.map +0 -7
  23. package/dist/entities/spec/spec-objects.test.js +0 -205
  24. package/dist/entities/spec/spec-objects.test.js.map +0 -7
  25. package/dist/entities/spec/x-scalar-environments.test.js +0 -11
  26. package/dist/entities/spec/x-scalar-environments.test.js.map +0 -7
  27. package/dist/entities/spec/x-scalar-secrets.test.js +0 -11
  28. package/dist/entities/spec/x-scalar-secrets.test.js.map +0 -7
  29. package/dist/helpers/ensure-protocol.test.js +0 -70
  30. package/dist/helpers/ensure-protocol.test.js.map +0 -7
  31. package/dist/helpers/fetch-document.test.js +0 -63
  32. package/dist/helpers/fetch-document.test.js.map +0 -7
  33. package/dist/helpers/find-variables.test.js +0 -20
  34. package/dist/helpers/find-variables.test.js.map +0 -7
  35. package/dist/helpers/is-defined.test.js +0 -37
  36. package/dist/helpers/is-defined.test.js.map +0 -7
  37. package/dist/helpers/is-local-url.test.js +0 -40
  38. package/dist/helpers/is-local-url.test.js.map +0 -7
  39. package/dist/helpers/is-valid-url.test.js +0 -17
  40. package/dist/helpers/is-valid-url.test.js.map +0 -7
  41. package/dist/helpers/json2xml.test.js +0 -19
  42. package/dist/helpers/json2xml.test.js.map +0 -7
  43. package/dist/helpers/make-url-absolute.test.js +0 -61
  44. package/dist/helpers/make-url-absolute.test.js.map +0 -7
  45. package/dist/helpers/merge-urls.test.js +0 -339
  46. package/dist/helpers/merge-urls.test.js.map +0 -7
  47. package/dist/helpers/normalize-mime-type-object.test.js +0 -53
  48. package/dist/helpers/normalize-mime-type-object.test.js.map +0 -7
  49. package/dist/helpers/normalize-mime-type.test.js +0 -33
  50. package/dist/helpers/normalize-mime-type.test.js.map +0 -7
  51. package/dist/helpers/omit-undefined-values.test.js +0 -89
  52. package/dist/helpers/omit-undefined-values.test.js.map +0 -7
  53. package/dist/helpers/parse.test.js +0 -45
  54. package/dist/helpers/parse.test.js.map +0 -7
  55. package/dist/helpers/pretty-print-json.test.js +0 -28
  56. package/dist/helpers/pretty-print-json.test.js.map +0 -7
  57. package/dist/helpers/redirect-to-proxy.test.js +0 -54
  58. package/dist/helpers/redirect-to-proxy.test.js.map +0 -7
  59. package/dist/helpers/regex-helpers.test.js +0 -154
  60. package/dist/helpers/regex-helpers.test.js.map +0 -7
  61. package/dist/helpers/replace-variables.test.js +0 -30
  62. package/dist/helpers/replace-variables.test.js.map +0 -7
  63. package/dist/helpers/security/get-schemes.test.js +0 -71
  64. package/dist/helpers/security/get-schemes.test.js.map +0 -7
  65. package/dist/helpers/security/has-token.test.js +0 -157
  66. package/dist/helpers/security/has-token.test.js.map +0 -7
  67. package/dist/migrations/semver.test.js +0 -21
  68. package/dist/migrations/semver.test.js.map +0 -7
  69. package/dist/migrations/v-2.4.0/migration.test.js +0 -90
  70. package/dist/migrations/v-2.4.0/migration.test.js.map +0 -7
  71. package/dist/migrations/v-2.5.0/migration.test.js +0 -108
  72. package/dist/migrations/v-2.5.0/migration.test.js.map +0 -7
  73. package/dist/spec-getters/get-example-from-schema.test.js +0 -1092
  74. package/dist/spec-getters/get-example-from-schema.test.js.map +0 -7
  75. package/dist/spec-getters/get-parameters-from-operation.test.js +0 -178
  76. package/dist/spec-getters/get-parameters-from-operation.test.js.map +0 -7
  77. package/dist/spec-getters/get-request-body-from-operation.test.js +0 -289
  78. package/dist/spec-getters/get-request-body-from-operation.test.js.map +0 -7
  79. package/dist/transforms/import-spec.test.js +0 -1124
  80. package/dist/transforms/import-spec.test.js.map +0 -7
@@ -1,1092 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { getExampleFromSchema } from "./get-example-from-schema.js";
3
- describe("getExampleFromSchema", () => {
4
- it("sets example values", () => {
5
- expect(
6
- getExampleFromSchema({
7
- example: 10
8
- })
9
- ).toMatchObject(10);
10
- });
11
- it("uses first example, if multiple are configured", () => {
12
- expect(
13
- getExampleFromSchema({
14
- examples: [10]
15
- })
16
- ).toMatchObject(10);
17
- });
18
- it("takes the first enum as example", () => {
19
- expect(
20
- getExampleFromSchema({
21
- enum: ["available", "pending", "sold"]
22
- })
23
- ).toBe("available");
24
- });
25
- it("uses empty quotes as a fallback for strings", () => {
26
- expect(
27
- getExampleFromSchema({
28
- type: "string"
29
- })
30
- ).toBe("");
31
- });
32
- it("only includes required attributes and attributes with example values", () => {
33
- expect(
34
- getExampleFromSchema(
35
- {
36
- type: "object",
37
- required: ["first_name"],
38
- properties: {
39
- first_name: {
40
- type: "string"
41
- },
42
- last_name: {
43
- type: "string",
44
- required: true
45
- },
46
- position: {
47
- type: "string",
48
- examples: ["Developer"]
49
- },
50
- description: {
51
- type: "string",
52
- example: "A developer"
53
- },
54
- age: {
55
- type: "number"
56
- }
57
- }
58
- },
59
- {
60
- omitEmptyAndOptionalProperties: true
61
- }
62
- )
63
- ).toStrictEqual({
64
- first_name: "",
65
- last_name: "",
66
- position: "Developer",
67
- description: "A developer"
68
- });
69
- });
70
- it("includes every available attributes", () => {
71
- expect(
72
- getExampleFromSchema(
73
- {
74
- type: "object",
75
- required: ["first_name"],
76
- properties: {
77
- first_name: {
78
- type: "string"
79
- },
80
- last_name: {
81
- type: "string",
82
- required: true
83
- },
84
- position: {
85
- type: "string",
86
- examples: ["Developer"]
87
- },
88
- description: {
89
- type: "string",
90
- example: "A developer"
91
- },
92
- age: {
93
- type: "number"
94
- }
95
- }
96
- },
97
- {
98
- omitEmptyAndOptionalProperties: false
99
- }
100
- )
101
- ).toStrictEqual({
102
- first_name: "",
103
- last_name: "",
104
- position: "Developer",
105
- description: "A developer",
106
- age: 1
107
- });
108
- });
109
- it("uses example value for first type in non-null union types", () => {
110
- expect(
111
- getExampleFromSchema({
112
- type: ["string", "number"]
113
- })
114
- ).toBe("");
115
- });
116
- it("uses null for nullable union types", () => {
117
- expect(
118
- getExampleFromSchema({
119
- type: ["string", "null"]
120
- })
121
- ).toBeNull();
122
- });
123
- it("sets example values", () => {
124
- expect(
125
- getExampleFromSchema({
126
- example: 10
127
- })
128
- ).toMatchObject(10);
129
- });
130
- it("goes through properties recursively with objects", () => {
131
- expect(
132
- getExampleFromSchema({
133
- type: "object",
134
- properties: {
135
- category: {
136
- type: "object",
137
- properties: {
138
- id: {
139
- example: 1
140
- },
141
- name: {
142
- example: "Dogs"
143
- },
144
- attributes: {
145
- type: "object",
146
- properties: {
147
- size: {
148
- enum: ["small", "medium", "large"]
149
- }
150
- }
151
- }
152
- }
153
- }
154
- }
155
- })
156
- ).toMatchObject({
157
- category: {
158
- id: 1,
159
- name: "Dogs",
160
- attributes: {
161
- size: "small"
162
- }
163
- }
164
- });
165
- });
166
- it("goes through properties recursively with arrays", () => {
167
- expect(
168
- getExampleFromSchema({
169
- type: "object",
170
- properties: {
171
- tags: {
172
- type: "array",
173
- items: {
174
- type: "object",
175
- properties: {
176
- id: {
177
- example: 1
178
- }
179
- }
180
- }
181
- }
182
- }
183
- })
184
- ).toMatchObject({
185
- tags: [
186
- {
187
- id: 1
188
- }
189
- ]
190
- });
191
- });
192
- it("uses empty [] as a fallback for arrays", () => {
193
- expect(
194
- getExampleFromSchema({
195
- type: "object",
196
- properties: {
197
- title: {
198
- type: "array"
199
- }
200
- }
201
- })
202
- ).toMatchObject({
203
- title: []
204
- });
205
- });
206
- it("uses given fallback for strings", () => {
207
- expect(
208
- getExampleFromSchema(
209
- {
210
- type: "string"
211
- },
212
- {
213
- emptyString: "\u2026"
214
- }
215
- )
216
- ).toBe("\u2026");
217
- });
218
- it("returns emails as an example value", () => {
219
- const result = getExampleFromSchema(
220
- {
221
- type: "string",
222
- format: "email"
223
- },
224
- {
225
- emptyString: "\u2026"
226
- }
227
- );
228
- function isEmail(text) {
229
- return !!text.match(/^.+@.+\..+$/);
230
- }
231
- expect(isEmail(result)).toBe(true);
232
- });
233
- it("uses variables as an example value", () => {
234
- expect(
235
- getExampleFromSchema(
236
- {
237
- "type": "string",
238
- "x-variable": "id"
239
- },
240
- {
241
- variables: {
242
- id: "foobar"
243
- }
244
- }
245
- )
246
- ).toBe("foobar");
247
- });
248
- it("uses true as a fallback for booleans", () => {
249
- expect(
250
- getExampleFromSchema({
251
- type: "boolean"
252
- })
253
- ).toMatchObject(true);
254
- });
255
- it("uses 1 as a fallback for integers", () => {
256
- expect(
257
- getExampleFromSchema({
258
- type: "integer"
259
- })
260
- ).toMatchObject(1);
261
- });
262
- it("returns an array if the schema type is array", () => {
263
- expect(
264
- getExampleFromSchema({
265
- type: "array"
266
- })
267
- ).toMatchObject([]);
268
- });
269
- it("uses array example values", () => {
270
- expect(
271
- getExampleFromSchema({
272
- type: "array",
273
- example: ["foobar"],
274
- items: {
275
- type: "string"
276
- }
277
- })
278
- ).toMatchObject(["foobar"]);
279
- });
280
- it("uses specified object as array default", () => {
281
- expect(
282
- getExampleFromSchema({
283
- type: "array",
284
- items: {
285
- type: "object",
286
- properties: {
287
- foo: {
288
- type: "number"
289
- },
290
- bar: {
291
- type: "string"
292
- }
293
- }
294
- }
295
- })
296
- ).toMatchObject([
297
- {
298
- foo: 1,
299
- bar: ""
300
- }
301
- ]);
302
- });
303
- it("uses the first example in object anyOf", () => {
304
- expect(
305
- getExampleFromSchema({
306
- type: "object",
307
- anyOf: [
308
- {
309
- type: "object",
310
- properties: {
311
- foo: { type: "number" }
312
- }
313
- },
314
- {
315
- type: "object",
316
- properties: {
317
- bar: { type: "string" }
318
- }
319
- }
320
- ]
321
- })
322
- ).toMatchObject({ foo: 1 });
323
- });
324
- it("uses the first example in object oneOf", () => {
325
- expect(
326
- getExampleFromSchema({
327
- type: "object",
328
- oneOf: [
329
- {
330
- type: "object",
331
- properties: {
332
- foo: { type: "number" }
333
- }
334
- },
335
- {
336
- type: "object",
337
- properties: {
338
- bar: { type: "string" }
339
- }
340
- }
341
- ]
342
- })
343
- ).toMatchObject({ foo: 1 });
344
- });
345
- it("uses the first example in object anyOf when type is not defined", () => {
346
- expect(
347
- getExampleFromSchema({
348
- anyOf: [
349
- {
350
- type: "object",
351
- properties: {
352
- foo: { type: "number" }
353
- }
354
- },
355
- {
356
- type: "object",
357
- properties: {
358
- bar: { type: "string" }
359
- }
360
- }
361
- ]
362
- })
363
- ).toMatchObject({ foo: 1 });
364
- });
365
- it("uses the first example in object oneOf when type is not defined", () => {
366
- expect(
367
- getExampleFromSchema({
368
- oneOf: [
369
- {
370
- type: "object",
371
- properties: {
372
- foo: { type: "number" }
373
- }
374
- },
375
- {
376
- type: "object",
377
- properties: {
378
- bar: { type: "string" }
379
- }
380
- }
381
- ]
382
- })
383
- ).toMatchObject({ foo: 1 });
384
- });
385
- it("uses all examples in object allOf", () => {
386
- expect(
387
- getExampleFromSchema({
388
- allOf: [
389
- {
390
- type: "object",
391
- properties: {
392
- foo: { type: "number" }
393
- }
394
- },
395
- {
396
- type: "object",
397
- properties: {
398
- bar: { type: "string" }
399
- }
400
- }
401
- ]
402
- })
403
- ).toMatchObject({ foo: 1, bar: "" });
404
- });
405
- it("merges allOf items in arrays", () => {
406
- expect(
407
- getExampleFromSchema({
408
- type: "array",
409
- items: {
410
- allOf: [
411
- {
412
- type: "object",
413
- properties: {
414
- foobar: { type: "string" },
415
- foo: { type: "number" }
416
- }
417
- },
418
- {
419
- type: "object",
420
- properties: {
421
- bar: { type: "string" }
422
- }
423
- }
424
- ]
425
- }
426
- })
427
- ).toMatchObject([{ foobar: "", foo: 1, bar: "" }]);
428
- });
429
- it("handles array items with allOf containing objects", () => {
430
- expect(
431
- getExampleFromSchema({
432
- type: "array",
433
- items: {
434
- allOf: [
435
- {
436
- type: "object",
437
- properties: {
438
- id: { type: "number", example: 1 }
439
- }
440
- },
441
- {
442
- type: "object",
443
- properties: {
444
- name: { type: "string", example: "test" }
445
- }
446
- }
447
- ]
448
- }
449
- })
450
- ).toMatchObject([
451
- {
452
- id: 1,
453
- name: "test"
454
- }
455
- ]);
456
- });
457
- it("uses the first example in array anyOf", () => {
458
- expect(
459
- getExampleFromSchema({
460
- type: "array",
461
- items: {
462
- anyOf: [
463
- {
464
- type: "string",
465
- example: "foobar"
466
- },
467
- {
468
- type: "string",
469
- example: "barfoo"
470
- }
471
- ]
472
- }
473
- })
474
- ).toMatchObject(["foobar"]);
475
- });
476
- it("uses one example in array oneOf", () => {
477
- expect(
478
- getExampleFromSchema({
479
- type: "array",
480
- items: {
481
- oneOf: [
482
- {
483
- type: "string",
484
- example: "foobar"
485
- },
486
- {
487
- type: "string",
488
- example: "barfoo"
489
- }
490
- ]
491
- }
492
- })
493
- ).toMatchObject(["foobar"]);
494
- });
495
- it("uses all examples in array allOf", () => {
496
- expect(
497
- getExampleFromSchema({
498
- type: "array",
499
- items: {
500
- allOf: [
501
- {
502
- type: "string",
503
- example: "foobar"
504
- },
505
- {
506
- type: "string",
507
- example: "barfoo"
508
- }
509
- ]
510
- }
511
- })
512
- ).toMatchObject(["foobar", "barfoo"]);
513
- });
514
- it("uses the default value", () => {
515
- const schema = {
516
- type: "string",
517
- default: "BAD_REQUEST_EXCEPTION"
518
- };
519
- expect(getExampleFromSchema(schema)).toBe("BAD_REQUEST_EXCEPTION");
520
- });
521
- it("uses 1 as the default for a number", () => {
522
- expect(
523
- getExampleFromSchema({
524
- type: "number"
525
- })
526
- ).toMatchObject(1);
527
- });
528
- it("uses min as the default for a number", () => {
529
- expect(
530
- getExampleFromSchema({
531
- type: "number",
532
- min: 200
533
- })
534
- ).toMatchObject(200);
535
- });
536
- it("returns plaintext", () => {
537
- expect(
538
- getExampleFromSchema({
539
- type: "string",
540
- example: "foobar"
541
- })
542
- ).toEqual("foobar");
543
- });
544
- it("converts a whole schema to an example response", () => {
545
- const schema = {
546
- required: ["name", "photoUrls"],
547
- type: "object",
548
- properties: {
549
- id: {
550
- type: "integer",
551
- format: "int64",
552
- example: 10
553
- },
554
- name: {
555
- type: "string",
556
- example: "doggie"
557
- },
558
- category: {
559
- type: "object",
560
- properties: {
561
- id: {
562
- type: "integer",
563
- format: "int64",
564
- example: 1
565
- },
566
- name: {
567
- type: "string",
568
- example: "Dogs"
569
- }
570
- },
571
- xml: {
572
- name: "category"
573
- }
574
- },
575
- photoUrls: {
576
- type: "array",
577
- xml: {
578
- wrapped: true
579
- },
580
- items: {
581
- type: "string",
582
- xml: {
583
- name: "photoUrl"
584
- }
585
- }
586
- },
587
- tags: {
588
- type: "array",
589
- xml: {
590
- wrapped: true
591
- },
592
- items: {
593
- type: "object",
594
- properties: {
595
- id: {
596
- type: "integer",
597
- format: "int64"
598
- },
599
- name: {
600
- type: "string"
601
- }
602
- },
603
- xml: {
604
- name: "tag"
605
- }
606
- }
607
- },
608
- status: {
609
- type: "string",
610
- description: "pet status in the store",
611
- enum: ["available", "pending", "sold"]
612
- }
613
- },
614
- xml: {
615
- name: "pet"
616
- }
617
- };
618
- expect(getExampleFromSchema(schema)).toMatchObject({
619
- id: 10,
620
- name: "doggie",
621
- category: {
622
- id: 1,
623
- name: "Dogs"
624
- },
625
- photoUrls: [""],
626
- tags: [
627
- {
628
- id: 1,
629
- name: ""
630
- }
631
- ],
632
- status: "available"
633
- });
634
- });
635
- it("outputs XML", () => {
636
- expect(
637
- getExampleFromSchema(
638
- {
639
- type: "object",
640
- properties: {
641
- id: {
642
- example: 1,
643
- xml: {
644
- name: "foo"
645
- }
646
- }
647
- }
648
- },
649
- { xml: true }
650
- )
651
- ).toMatchObject({
652
- foo: 1
653
- });
654
- });
655
- it("add XML wrappers where needed", () => {
656
- expect(
657
- getExampleFromSchema(
658
- {
659
- type: "object",
660
- properties: {
661
- photoUrls: {
662
- type: "array",
663
- xml: {
664
- wrapped: true
665
- },
666
- items: {
667
- type: "string",
668
- example: "https://example.com",
669
- xml: {
670
- name: "photoUrl"
671
- }
672
- }
673
- }
674
- }
675
- },
676
- { xml: true }
677
- )
678
- ).toMatchObject({
679
- photoUrls: [{ photoUrl: "https://example.com" }]
680
- });
681
- });
682
- it("doesn\u2019t wrap items when not needed", () => {
683
- expect(
684
- getExampleFromSchema(
685
- {
686
- type: "object",
687
- properties: {
688
- photoUrls: {
689
- type: "array",
690
- items: {
691
- type: "string",
692
- example: "https://example.com",
693
- xml: {
694
- name: "photoUrl"
695
- }
696
- }
697
- }
698
- }
699
- },
700
- { xml: true }
701
- )
702
- ).toMatchObject({
703
- photoUrls: ["https://example.com"]
704
- });
705
- });
706
- it("use the first item of oneOf", () => {
707
- expect(
708
- getExampleFromSchema({
709
- oneOf: [
710
- {
711
- maxLength: 255,
712
- type: "string"
713
- },
714
- {
715
- type: "null"
716
- }
717
- ]
718
- })
719
- ).toBe("");
720
- });
721
- it("works with allOf", () => {
722
- expect(
723
- getExampleFromSchema({
724
- allOf: [
725
- {
726
- type: "string"
727
- }
728
- ]
729
- })
730
- ).toBe("");
731
- });
732
- it("uses all schemas in allOf", () => {
733
- expect(
734
- getExampleFromSchema({
735
- allOf: [
736
- {
737
- type: "object",
738
- properties: {
739
- id: {
740
- example: 10
741
- }
742
- }
743
- },
744
- {
745
- type: "object",
746
- properties: {
747
- title: {
748
- example: "Foobar"
749
- }
750
- }
751
- }
752
- ]
753
- })
754
- ).toMatchObject({
755
- id: 10,
756
- title: "Foobar"
757
- });
758
- });
759
- it("returns null for unknown types", () => {
760
- expect(
761
- getExampleFromSchema({
762
- type: "fantasy"
763
- })
764
- ).toBe(null);
765
- });
766
- it("returns readOnly attributes by default", () => {
767
- expect(
768
- getExampleFromSchema({
769
- example: "foobar",
770
- readOnly: true
771
- })
772
- ).toBe("foobar");
773
- });
774
- it("returns readOnly attributes in read mode", () => {
775
- expect(
776
- getExampleFromSchema(
777
- {
778
- example: "foobar",
779
- readOnly: true
780
- },
781
- {
782
- mode: "read"
783
- }
784
- )
785
- ).toBe("foobar");
786
- });
787
- it("doesn\u2019t return readOnly attributes in write mode", () => {
788
- expect(
789
- getExampleFromSchema(
790
- {
791
- example: "foobar",
792
- readOnly: true
793
- },
794
- {
795
- mode: "write"
796
- }
797
- )
798
- ).toBe(void 0);
799
- });
800
- it("returns writeOnly attributes by default", () => {
801
- expect(
802
- getExampleFromSchema({
803
- example: "foobar",
804
- writeOnly: true
805
- })
806
- ).toBe("foobar");
807
- });
808
- it("returns writeOnly attributes in write mode", () => {
809
- expect(
810
- getExampleFromSchema(
811
- {
812
- example: "foobar",
813
- writeOnly: true
814
- },
815
- {
816
- mode: "write"
817
- }
818
- )
819
- ).toBe("foobar");
820
- });
821
- it("doesn\u2019t return writeOnly attributes in read mode", () => {
822
- expect(
823
- getExampleFromSchema(
824
- {
825
- example: "foobar",
826
- writeOnly: true
827
- },
828
- {
829
- mode: "read"
830
- }
831
- )
832
- ).toBe(void 0);
833
- });
834
- it("allows any additonalProperty", () => {
835
- expect(
836
- getExampleFromSchema({
837
- type: "object",
838
- additionalProperties: {}
839
- })
840
- ).toMatchObject({
841
- ANY_ADDITIONAL_PROPERTY: "anything"
842
- });
843
- expect(
844
- getExampleFromSchema({
845
- type: "object",
846
- additionalProperties: true
847
- })
848
- ).toMatchObject({
849
- ANY_ADDITIONAL_PROPERTY: "anything"
850
- });
851
- });
852
- it("adds an additionalProperty with specific types", () => {
853
- expect(
854
- getExampleFromSchema({
855
- type: "object",
856
- additionalProperties: {
857
- type: "integer"
858
- }
859
- })
860
- ).toMatchObject({
861
- ANY_ADDITIONAL_PROPERTY: 1
862
- });
863
- expect(
864
- getExampleFromSchema({
865
- type: "object",
866
- additionalProperties: {
867
- type: "boolean"
868
- }
869
- })
870
- ).toMatchObject({
871
- ANY_ADDITIONAL_PROPERTY: true
872
- });
873
- expect(
874
- getExampleFromSchema({
875
- type: "object",
876
- additionalProperties: {
877
- type: "boolean",
878
- default: false
879
- }
880
- })
881
- ).toMatchObject({
882
- ANY_ADDITIONAL_PROPERTY: false
883
- });
884
- expect(
885
- getExampleFromSchema({
886
- type: "object",
887
- additionalProperties: {
888
- type: "string"
889
- }
890
- })
891
- ).toMatchObject({
892
- ANY_ADDITIONAL_PROPERTY: ""
893
- });
894
- expect(
895
- getExampleFromSchema({
896
- type: "object",
897
- additionalProperties: {
898
- type: "object",
899
- properties: {
900
- foo: {
901
- type: "string"
902
- }
903
- }
904
- }
905
- })
906
- ).toMatchObject({
907
- ANY_ADDITIONAL_PROPERTY: {
908
- foo: ""
909
- }
910
- });
911
- });
912
- it("works with anyOf", () => {
913
- expect(
914
- getExampleFromSchema({
915
- title: "Foo",
916
- type: "object",
917
- anyOf: [
918
- {
919
- type: "object",
920
- required: ["a"],
921
- properties: {
922
- a: {
923
- type: "integer",
924
- format: "int32"
925
- }
926
- }
927
- },
928
- {
929
- type: "object",
930
- required: ["b"],
931
- properties: {
932
- b: {
933
- type: "string"
934
- }
935
- }
936
- }
937
- ],
938
- required: ["c"],
939
- properties: {
940
- c: {
941
- type: "boolean"
942
- }
943
- }
944
- })
945
- ).toStrictEqual({
946
- a: 1,
947
- c: true
948
- });
949
- });
950
- it("handles patternProperties", () => {
951
- expect(
952
- getExampleFromSchema({
953
- type: "object",
954
- patternProperties: {
955
- "^(.*)$": {
956
- type: "object",
957
- properties: {
958
- dataId: {
959
- type: "string"
960
- },
961
- link: {
962
- anyOf: [
963
- {
964
- format: "uri",
965
- type: "string",
966
- example: "https://example.com"
967
- },
968
- {
969
- type: "null"
970
- }
971
- ]
972
- }
973
- },
974
- required: ["dataId", "link"]
975
- }
976
- }
977
- })
978
- ).toStrictEqual({
979
- "^(.*)$": {
980
- dataId: "",
981
- link: "https://example.com"
982
- }
983
- });
984
- });
985
- describe("circular references", () => {
986
- it("deals with circular references", () => {
987
- const schema = {
988
- type: "object",
989
- properties: {
990
- foobar: {}
991
- }
992
- };
993
- schema.properties.foobar = schema;
994
- expect(getExampleFromSchema(schema)).toStrictEqual({
995
- foobar: {
996
- foobar: {
997
- foobar: {
998
- foobar: {
999
- foobar: {
1000
- foobar: "[Circular Reference]"
1001
- }
1002
- }
1003
- }
1004
- }
1005
- }
1006
- });
1007
- });
1008
- it("deals with circular references that expand horizontally", () => {
1009
- const schema = {
1010
- type: "object",
1011
- properties: {
1012
- a: {},
1013
- b: {},
1014
- c: {},
1015
- d: {},
1016
- e: {},
1017
- f: {},
1018
- g: {},
1019
- h: {},
1020
- i: {},
1021
- j: {},
1022
- k: {},
1023
- l: {},
1024
- m: {},
1025
- n: {},
1026
- o: {},
1027
- p: {},
1028
- q: {},
1029
- r: {},
1030
- s: {},
1031
- t: {},
1032
- u: {},
1033
- v: {},
1034
- w: {},
1035
- x: {},
1036
- y: {},
1037
- z: {}
1038
- }
1039
- };
1040
- schema.properties.a = schema;
1041
- schema.properties.b = schema;
1042
- schema.properties.c = schema;
1043
- schema.properties.d = schema;
1044
- schema.properties.e = schema;
1045
- schema.properties.f = schema;
1046
- schema.properties.g = schema;
1047
- schema.properties.h = schema;
1048
- schema.properties.i = schema;
1049
- schema.properties.j = schema;
1050
- schema.properties.k = schema;
1051
- schema.properties.l = schema;
1052
- schema.properties.m = schema;
1053
- schema.properties.n = schema;
1054
- schema.properties.o = schema;
1055
- schema.properties.p = schema;
1056
- schema.properties.q = schema;
1057
- schema.properties.r = schema;
1058
- schema.properties.s = schema;
1059
- schema.properties.t = schema;
1060
- schema.properties.u = schema;
1061
- schema.properties.v = schema;
1062
- schema.properties.w = schema;
1063
- schema.properties.x = schema;
1064
- schema.properties.y = schema;
1065
- schema.properties.z = schema;
1066
- const example = getExampleFromSchema(schema);
1067
- expect(example).toBeInstanceOf(Object);
1068
- expect(Object.keys(example).length).toBe(26);
1069
- });
1070
- });
1071
- it("omits deprecated properties", () => {
1072
- expect(
1073
- getExampleFromSchema({
1074
- type: "object",
1075
- properties: {
1076
- name: {
1077
- type: "string",
1078
- example: "test"
1079
- },
1080
- oldField: {
1081
- type: "string",
1082
- example: "should not appear",
1083
- deprecated: true
1084
- }
1085
- }
1086
- })
1087
- ).toStrictEqual({
1088
- name: "test"
1089
- });
1090
- });
1091
- });
1092
- //# sourceMappingURL=get-example-from-schema.test.js.map