docusaurus-plugin-openapi-docs 0.0.0-613 → 0.0.0-616

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.
@@ -5,910 +5,18 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  * ========================================================================== */
7
7
 
8
- import { MediaTypeObject, SchemaObject } from "../openapi/types";
9
- import {
10
- createClosingArrayBracket,
11
- createOpeningArrayBracket,
12
- } from "./createArrayBracket";
8
+ import { MediaTypeObject } from "../openapi/types";
13
9
  import { createDescription } from "./createDescription";
14
10
  import { createDetails } from "./createDetails";
15
11
  import { createDetailsSummary } from "./createDetailsSummary";
12
+ import { createNodes } from "./createSchema";
16
13
  import {
17
14
  createExampleFromSchema,
18
15
  createResponseExample,
19
16
  createResponseExamples,
20
17
  } from "./createStatusCodes";
21
- import { getQualifierMessage, getSchemaName } from "./schema";
22
18
  import { create, guard } from "./utils";
23
19
 
24
- const jsonSchemaMergeAllOf = require("json-schema-merge-allof");
25
-
26
- /**
27
- * Returns a merged representation of allOf array of schemas.
28
- */
29
- export function mergeAllOf(allOf: SchemaObject[]) {
30
- const mergedSchemas = jsonSchemaMergeAllOf(allOf, {
31
- resolvers: {
32
- readOnly: function () {
33
- return true;
34
- },
35
- example: function () {
36
- return true;
37
- },
38
- "x-examples": function () {
39
- return true;
40
- },
41
- },
42
- ignoreAdditionalProperties: true,
43
- });
44
-
45
- const required = allOf.reduce((acc, cur) => {
46
- if (Array.isArray(cur.required)) {
47
- const next = [...acc, ...cur.required];
48
- return next;
49
- }
50
- return acc;
51
- }, [] as any);
52
-
53
- return { mergedSchemas, required };
54
- }
55
-
56
- /**
57
- * For handling nested anyOf/oneOf.
58
- */
59
- function createAnyOneOf(schema: SchemaObject): any {
60
- const type = schema.oneOf ? "oneOf" : "anyOf";
61
- return create("div", {
62
- children: [
63
- create("span", {
64
- className: "badge badge--info",
65
- children: type,
66
- }),
67
- create("SchemaTabs", {
68
- children: schema[type]!.map((anyOneSchema, index) => {
69
- const label = anyOneSchema.title
70
- ? anyOneSchema.title
71
- : `MOD${index + 1}`;
72
- const anyOneChildren = [];
73
-
74
- if (anyOneSchema.properties !== undefined) {
75
- anyOneChildren.push(createProperties(anyOneSchema));
76
- delete anyOneSchema.properties;
77
- }
78
-
79
- if (anyOneSchema.allOf !== undefined) {
80
- anyOneChildren.push(createNodes(anyOneSchema));
81
- delete anyOneSchema.allOf;
82
- }
83
-
84
- if (anyOneSchema.items !== undefined) {
85
- anyOneChildren.push(createItems(anyOneSchema));
86
- delete anyOneSchema.items;
87
- }
88
-
89
- if (
90
- anyOneSchema.type === "string" ||
91
- anyOneSchema.type === "number" ||
92
- anyOneSchema.type === "integer" ||
93
- anyOneSchema.type === "boolean"
94
- ) {
95
- anyOneChildren.push(createNodes(anyOneSchema));
96
- }
97
-
98
- if (anyOneChildren.length) {
99
- if (schema.type === "array") {
100
- return create("TabItem", {
101
- label: label,
102
- value: `${index}-item-properties`,
103
- children: [
104
- createOpeningArrayBracket(),
105
- anyOneChildren,
106
- createClosingArrayBracket(),
107
- ]
108
- .filter(Boolean)
109
- .flat(),
110
- });
111
- }
112
- return create("TabItem", {
113
- label: label,
114
- value: `${index}-item-properties`,
115
- children: anyOneChildren,
116
- });
117
- }
118
-
119
- return undefined;
120
- }),
121
- }),
122
- ],
123
- });
124
- }
125
-
126
- function createProperties(schema: SchemaObject) {
127
- const discriminator = schema.discriminator;
128
- return Object.entries(schema.properties!).map(([key, val]) => {
129
- return createEdges({
130
- name: key,
131
- schema: val,
132
- required: Array.isArray(schema.required)
133
- ? schema.required.includes(key)
134
- : false,
135
- discriminator,
136
- });
137
- });
138
- }
139
-
140
- function createAdditionalProperties(schema: SchemaObject) {
141
- // TODO?:
142
- // {
143
- // description: 'Integration configuration. See \n' +
144
- // '[Integration Configurations](https://prisma.pan.dev/api/cloud/api-integration-config/).\n',
145
- // example: { webhookUrl: 'https://hooks.slack.com/abcdef' },
146
- // externalDocs: { url: 'https://prisma.pan.dev/api/cloud/api-integration-config' },
147
- // type: 'object'
148
- // }
149
-
150
- // TODO?:
151
- // {
152
- // items: {
153
- // properties: {
154
- // aliasField: [Object],
155
- // displayName: [Object],
156
- // fieldName: [Object],
157
- // maxLength: [Object],
158
- // options: [Object],
159
- // redlockMapping: [Object],
160
- // required: [Object],
161
- // type: [Object],
162
- // typeaheadUri: [Object],
163
- // value: [Object]
164
- // },
165
- // type: 'object'
166
- // },
167
- // type: 'array'
168
- // }
169
- const additionalProperties = schema.additionalProperties;
170
- const type: string | unknown = additionalProperties?.type;
171
- // Handle free-form objects
172
- if (String(additionalProperties) === "true" && schema.type === "object") {
173
- return create("SchemaItem", {
174
- name: "property name*",
175
- required: false,
176
- schemaName: "any",
177
- qualifierMessage: getQualifierMessage(schema.additionalProperties),
178
- schema: schema,
179
- collapsible: false,
180
- discriminator: false,
181
- });
182
- }
183
- if (
184
- (type === "object" || type === "array") &&
185
- (additionalProperties?.properties ||
186
- additionalProperties?.items ||
187
- additionalProperties?.allOf ||
188
- additionalProperties?.additionalProperties ||
189
- additionalProperties?.oneOf ||
190
- additionalProperties?.anyOf)
191
- ) {
192
- const title = additionalProperties.title as string;
193
- const schemaName = getSchemaName(additionalProperties);
194
- const required = schema.required ?? false;
195
- return createDetailsNode(
196
- "property name*",
197
- title ?? schemaName,
198
- additionalProperties,
199
- required,
200
- schema.nullable
201
- );
202
- }
203
-
204
- if (
205
- (schema.additionalProperties?.type as string) === "string" ||
206
- (schema.additionalProperties?.type as string) === "object" ||
207
- (schema.additionalProperties?.type as string) === "boolean" ||
208
- (schema.additionalProperties?.type as string) === "integer" ||
209
- (schema.additionalProperties?.type as string) === "number"
210
- ) {
211
- const additionalProperties =
212
- schema.additionalProperties?.additionalProperties;
213
- if (additionalProperties !== undefined) {
214
- const type = schema.additionalProperties?.additionalProperties?.type;
215
- const schemaName = getSchemaName(
216
- schema.additionalProperties?.additionalProperties!
217
- );
218
- return create("SchemaItem", {
219
- name: "property name*",
220
- required: false,
221
- schemaName: schemaName ?? type,
222
- qualifierMessage:
223
- schema.additionalProperties ??
224
- getQualifierMessage(schema.additionalProperties),
225
- schema: schema,
226
- collapsible: false,
227
- discriminator: false,
228
- });
229
- }
230
- const schemaName = getSchemaName(schema.additionalProperties!);
231
- return create("SchemaItem", {
232
- name: "property name*",
233
- required: false,
234
- schemaName: schemaName,
235
- qualifierMessage: getQualifierMessage(schema),
236
- schema: schema.additionalProperties,
237
- collapsible: false,
238
- discriminator: false,
239
- });
240
- }
241
- return Object.entries(schema.additionalProperties!).map(([key, val]) =>
242
- createEdges({
243
- name: key,
244
- schema: val,
245
- required: Array.isArray(schema.required)
246
- ? schema.required.includes(key)
247
- : false,
248
- })
249
- );
250
- }
251
-
252
- // TODO: figure out how to handle array of objects
253
- function createItems(schema: SchemaObject) {
254
- if (schema.items?.properties !== undefined) {
255
- return [
256
- createOpeningArrayBracket(),
257
- createProperties(schema.items),
258
- createClosingArrayBracket(),
259
- ].flat();
260
- }
261
-
262
- if (schema.items?.additionalProperties !== undefined) {
263
- return [
264
- createOpeningArrayBracket(),
265
- createAdditionalProperties(schema.items),
266
- createClosingArrayBracket(),
267
- ].flat();
268
- }
269
-
270
- if (schema.items?.oneOf !== undefined || schema.items?.anyOf !== undefined) {
271
- return [
272
- createOpeningArrayBracket(),
273
- createAnyOneOf(schema.items!),
274
- createClosingArrayBracket(),
275
- ].flat();
276
- }
277
-
278
- if (schema.items?.allOf !== undefined) {
279
- // TODO: figure out if and how we should pass merged required array
280
- const {
281
- mergedSchemas,
282
- }: { mergedSchemas: SchemaObject; required: string[] } = mergeAllOf(
283
- schema.items?.allOf
284
- );
285
-
286
- // Handles combo anyOf/oneOf + properties
287
- if (
288
- (mergedSchemas.oneOf !== undefined ||
289
- mergedSchemas.anyOf !== undefined) &&
290
- mergedSchemas.properties
291
- ) {
292
- return [
293
- createOpeningArrayBracket(),
294
- createAnyOneOf(mergedSchemas),
295
- createProperties(mergedSchemas),
296
- createClosingArrayBracket(),
297
- ].flat();
298
- }
299
-
300
- // Handles only anyOf/oneOf
301
- if (
302
- mergedSchemas.oneOf !== undefined ||
303
- mergedSchemas.anyOf !== undefined
304
- ) {
305
- return [
306
- createOpeningArrayBracket(),
307
- createAnyOneOf(mergedSchemas),
308
- createClosingArrayBracket(),
309
- ].flat();
310
- }
311
-
312
- // Handles properties
313
- if (mergedSchemas.properties !== undefined) {
314
- return [
315
- createOpeningArrayBracket(),
316
- createProperties(mergedSchemas),
317
- createClosingArrayBracket(),
318
- ].flat();
319
- }
320
- }
321
-
322
- if (
323
- schema.items?.type === "string" ||
324
- schema.items?.type === "number" ||
325
- schema.items?.type === "integer" ||
326
- schema.items?.type === "boolean" ||
327
- schema.items?.type === "object"
328
- ) {
329
- return [
330
- createOpeningArrayBracket(),
331
- createNodes(schema.items),
332
- createClosingArrayBracket(),
333
- ].flat();
334
- }
335
-
336
- // TODO: clean this up or eliminate it?
337
- return [
338
- createOpeningArrayBracket(),
339
- Object.entries(schema.items!).map(([key, val]) =>
340
- createEdges({
341
- name: key,
342
- schema: val,
343
- required: Array.isArray(schema.required)
344
- ? schema.required.includes(key)
345
- : false,
346
- })
347
- ),
348
- createClosingArrayBracket(),
349
- ].flat();
350
- }
351
-
352
- /**
353
- * For handling discriminators that do not map to a same-level property
354
- */
355
- // function createDiscriminator(schema: SchemaObject) {
356
- // const discriminator = schema.discriminator;
357
- // const propertyName = discriminator?.propertyName;
358
- // const propertyType = "string"; // should always be string
359
- // const mapping: any = discriminator?.mapping;
360
-
361
- // // Explicit mapping is required since we can't support implicit
362
- // if (mapping === undefined) {
363
- // return undefined;
364
- // }
365
-
366
- // // Attempt to get the property description we want to display
367
- // // TODO: how to make it predictable when handling allOf
368
- // let propertyDescription;
369
- // const firstMappingSchema = mapping[Object.keys(mapping)[0]];
370
- // if (firstMappingSchema.properties !== undefined) {
371
- // propertyDescription =
372
- // firstMappingSchema.properties![propertyName!].description;
373
- // }
374
- // if (firstMappingSchema.allOf !== undefined) {
375
- // const { mergedSchemas }: { mergedSchemas: SchemaObject } = mergeAllOf(
376
- // firstMappingSchema.allOf
377
- // );
378
- // if (mergedSchemas.properties !== undefined) {
379
- // propertyDescription =
380
- // mergedSchemas.properties[propertyName!]?.description;
381
- // }
382
- // }
383
-
384
- // if (propertyDescription === undefined) {
385
- // if (
386
- // schema.properties !== undefined &&
387
- // schema.properties![propertyName!] !== undefined
388
- // ) {
389
- // propertyDescription = schema.properties![propertyName!].description;
390
- // }
391
- // }
392
-
393
- // return create("div", {
394
- // className: "discriminatorItem",
395
- // children: create("div", {
396
- // children: [
397
- // create("strong", {
398
- // style: { paddingLeft: "1rem" },
399
- // children: propertyName,
400
- // }),
401
- // guard(propertyType, (name) =>
402
- // create("span", {
403
- // style: { opacity: "0.6" },
404
- // children: ` ${propertyType}`,
405
- // })
406
- // ),
407
- // guard(getQualifierMessage(schema.discriminator as any), (message) =>
408
- // create("div", {
409
- // style: {
410
- // paddingLeft: "1rem",
411
- // },
412
- // children: createDescription(message),
413
- // })
414
- // ),
415
- // guard(propertyDescription, (description) =>
416
- // create("div", {
417
- // style: {
418
- // paddingLeft: "1rem",
419
- // },
420
- // children: createDescription(description),
421
- // })
422
- // ),
423
- // create("DiscriminatorTabs", {
424
- // children: Object.keys(mapping!).map((key, index) => {
425
- // if (mapping[key].allOf !== undefined) {
426
- // const { mergedSchemas }: { mergedSchemas: SchemaObject } =
427
- // mergeAllOf(mapping[key].allOf);
428
- // // Cleanup duplicate property from mapping schema
429
- // delete mergedSchemas.properties![propertyName!];
430
- // mapping[key] = mergedSchemas;
431
- // }
432
-
433
- // if (mapping[key].properties !== undefined) {
434
- // // Cleanup duplicate property from mapping schema
435
- // delete mapping[key].properties![propertyName!];
436
- // }
437
-
438
- // const label = key;
439
- // return create("TabItem", {
440
- // label: label,
441
- // value: `${index}-item-discriminator`,
442
- // children: [
443
- // create("div", {
444
- // style: { marginLeft: "-4px" },
445
- // children: createNodes(mapping[key]),
446
- // }),
447
- // ],
448
- // });
449
- // }),
450
- // }),
451
- // ],
452
- // }),
453
- // });
454
- // }
455
-
456
- function createDetailsNode(
457
- name: string,
458
- schemaName: string,
459
- schema: SchemaObject,
460
- required: string[] | boolean,
461
- nullable: boolean | unknown
462
- ): any {
463
- return create("SchemaItem", {
464
- collapsible: true,
465
- className: "schemaItem",
466
- children: [
467
- createDetails({
468
- children: [
469
- createDetailsSummary({
470
- children: [
471
- create("strong", { children: name }),
472
- create("span", {
473
- style: { opacity: "0.6" },
474
- children: ` ${schemaName}`,
475
- }),
476
- guard(
477
- (schema.nullable && schema.nullable === true) ||
478
- (nullable && nullable === true),
479
- () => [
480
- create("strong", {
481
- style: {
482
- fontSize: "var(--ifm-code-font-size)",
483
- color: "var(--openapi-nullable)",
484
- },
485
- children: " nullable",
486
- }),
487
- ]
488
- ),
489
- guard(
490
- Array.isArray(required)
491
- ? required.includes(name)
492
- : required === true,
493
- () => [
494
- create("strong", {
495
- style: {
496
- fontSize: "var(--ifm-code-font-size)",
497
- color: "var(--openapi-required)",
498
- },
499
- children: " required",
500
- }),
501
- ]
502
- ),
503
- ],
504
- }),
505
- create("div", {
506
- style: { marginLeft: "1rem" },
507
- children: [
508
- guard(getQualifierMessage(schema), (message) =>
509
- create("div", {
510
- style: { marginTop: ".5rem", marginBottom: ".5rem" },
511
- children: createDescription(message),
512
- })
513
- ),
514
- guard(schema.description, (description) =>
515
- create("div", {
516
- style: { marginTop: ".5rem", marginBottom: ".5rem" },
517
- children: createDescription(description),
518
- })
519
- ),
520
- createNodes(schema),
521
- ],
522
- }),
523
- ],
524
- }),
525
- ],
526
- });
527
- }
528
-
529
- function createOneOfProperty(
530
- name: string,
531
- schemaName: string,
532
- schema: SchemaObject,
533
- required: string[] | boolean,
534
- nullable: boolean | unknown
535
- ): any {
536
- return create("SchemaItem", {
537
- collapsible: true,
538
- className: "schemaItem",
539
- children: [
540
- createDetails({
541
- children: [
542
- createDetailsSummary({
543
- children: [
544
- create("strong", { children: name }),
545
- create("span", {
546
- style: { opacity: "0.6" },
547
- children: ` ${schemaName}`,
548
- }),
549
- guard(
550
- (schema.nullable && schema.nullable === true) ||
551
- (nullable && nullable === true),
552
- () => [
553
- create("strong", {
554
- style: {
555
- fontSize: "var(--ifm-code-font-size)",
556
- color: "var(--openapi-nullable)",
557
- },
558
- children: " nullable",
559
- }),
560
- ]
561
- ),
562
- guard(
563
- Array.isArray(required)
564
- ? required.includes(name)
565
- : required === true,
566
- () => [
567
- create("strong", {
568
- style: {
569
- fontSize: "var(--ifm-code-font-size)",
570
- color: "var(--openapi-required)",
571
- },
572
- children: " required",
573
- }),
574
- ]
575
- ),
576
- ],
577
- }),
578
- create("div", {
579
- style: { marginLeft: "1rem" },
580
- children: [
581
- guard(getQualifierMessage(schema), (message) =>
582
- create("div", {
583
- style: { marginTop: ".5rem", marginBottom: ".5rem" },
584
- children: createDescription(message),
585
- })
586
- ),
587
- guard(schema.description, (description) =>
588
- create("div", {
589
- style: { marginTop: ".5rem", marginBottom: ".5rem" },
590
- children: createDescription(description),
591
- })
592
- ),
593
- ],
594
- }),
595
- create("div", {
596
- children: [
597
- create("span", {
598
- className: "badge badge--info",
599
- children: "oneOf",
600
- }),
601
- create("SchemaTabs", {
602
- children: schema["oneOf"]!.map((property, index) => {
603
- const label = property.type ?? `MOD${index + 1}`;
604
- return create("TabItem", {
605
- label: label,
606
- value: `${index}-property`,
607
- children: [
608
- create("p", { children: label }),
609
- guard(schema.description, (description) =>
610
- create("div", {
611
- style: { marginTop: ".5rem", marginBottom: ".5rem" },
612
- children: createDescription(description),
613
- })
614
- ),
615
- ],
616
- });
617
- }),
618
- }),
619
- ],
620
- }),
621
- ],
622
- }),
623
- ],
624
- });
625
- }
626
-
627
- /**
628
- * For handling discriminators that map to a same-level property (like 'petType').
629
- * Note: These should only be encountered while iterating through properties.
630
- */
631
- function createPropertyDiscriminator(
632
- name: string,
633
- schemaName: string,
634
- schema: SchemaObject,
635
- discriminator: any,
636
- required: string[] | boolean
637
- ) {
638
- if (schema === undefined) {
639
- return undefined;
640
- }
641
-
642
- if (discriminator.mapping === undefined) {
643
- return undefined;
644
- }
645
-
646
- return create("SchemaItem", {
647
- name,
648
- required: Array.isArray(required) ? required.includes(name) : required,
649
- schemaName: schemaName,
650
- qualifierMessage: getQualifierMessage(schema),
651
- schema: schema,
652
- collapsible: false,
653
- discriminator: true,
654
- children: [
655
- create("DiscriminatorTabs", {
656
- children: Object.keys(discriminator?.mapping!).map((key, index) => {
657
- const label = key;
658
- return create("TabItem", {
659
- label: label,
660
- value: `${index}-item-discriminator`,
661
- children: createNodes(discriminator?.mapping[key]),
662
- });
663
- }),
664
- }),
665
- ],
666
- });
667
- }
668
-
669
- interface EdgeProps {
670
- name: string;
671
- schema: SchemaObject;
672
- required: string[] | boolean;
673
- discriminator?: any | unknown;
674
- }
675
-
676
- /**
677
- * Creates the edges or "leaves" of a schema tree. Edges can branch into sub-nodes with createDetails().
678
- */
679
- function createEdges({
680
- name,
681
- schema,
682
- required,
683
- discriminator,
684
- }: EdgeProps): any {
685
- const schemaName = getSchemaName(schema);
686
-
687
- if (discriminator !== undefined && discriminator.propertyName === name) {
688
- return createPropertyDiscriminator(
689
- name,
690
- "string",
691
- schema,
692
- discriminator,
693
- required
694
- );
695
- }
696
-
697
- if (schema.oneOf !== undefined || schema.anyOf !== undefined) {
698
- return createOneOfProperty(
699
- name,
700
- schemaName,
701
- schema,
702
- required,
703
- schema.nullable
704
- );
705
- }
706
-
707
- if (schema.allOf !== undefined) {
708
- const {
709
- mergedSchemas,
710
- required,
711
- }: { mergedSchemas: SchemaObject; required: string[] | boolean } =
712
- mergeAllOf(schema.allOf);
713
- const mergedSchemaName = getSchemaName(mergedSchemas);
714
-
715
- if (
716
- mergedSchemas.oneOf !== undefined ||
717
- mergedSchemas.anyOf !== undefined
718
- ) {
719
- return createDetailsNode(
720
- name,
721
- mergedSchemaName,
722
- mergedSchemas,
723
- required,
724
- schema.nullable
725
- );
726
- }
727
-
728
- if (mergedSchemas.properties !== undefined) {
729
- return createDetailsNode(
730
- name,
731
- mergedSchemaName,
732
- mergedSchemas,
733
- required,
734
- schema.nullable
735
- );
736
- }
737
-
738
- if (mergedSchemas.additionalProperties !== undefined) {
739
- return createDetailsNode(
740
- name,
741
- mergedSchemaName,
742
- mergedSchemas,
743
- required,
744
- schema.nullable
745
- );
746
- }
747
-
748
- // array of objects
749
- if (mergedSchemas.items?.properties !== undefined) {
750
- return createDetailsNode(
751
- name,
752
- mergedSchemaName,
753
- mergedSchemas,
754
- required,
755
- schema.nullable
756
- );
757
- }
758
-
759
- if (mergedSchemas.writeOnly && mergedSchemas.writeOnly === true) {
760
- return undefined;
761
- }
762
-
763
- return create("SchemaItem", {
764
- collapsible: false,
765
- name,
766
- required: Array.isArray(required) ? required.includes(name) : required,
767
- schemaName: schemaName,
768
- qualifierMessage: getQualifierMessage(schema),
769
- schema: mergedSchemas,
770
- });
771
- }
772
-
773
- if (schema.properties !== undefined) {
774
- return createDetailsNode(
775
- name,
776
- schemaName,
777
- schema,
778
- required,
779
- schema.nullable
780
- );
781
- }
782
-
783
- if (schema.additionalProperties !== undefined) {
784
- return createDetailsNode(
785
- name,
786
- schemaName,
787
- schema,
788
- required,
789
- schema.nullable
790
- );
791
- }
792
-
793
- // array of objects
794
- if (schema.items?.properties !== undefined) {
795
- return createDetailsNode(
796
- name,
797
- schemaName,
798
- schema,
799
- required,
800
- schema.nullable
801
- );
802
- }
803
-
804
- if (schema.items?.anyOf !== undefined || schema.items?.oneOf !== undefined) {
805
- return createDetailsNode(
806
- name,
807
- schemaName,
808
- schema,
809
- required,
810
- schema.nullable
811
- );
812
- }
813
-
814
- if (schema.writeOnly && schema.writeOnly === true) {
815
- return undefined;
816
- }
817
-
818
- // primitives and array of non-objects
819
- return create("SchemaItem", {
820
- collapsible: false,
821
- name,
822
- required: Array.isArray(required) ? required.includes(name) : required,
823
- schemaName: schemaName,
824
- qualifierMessage: getQualifierMessage(schema),
825
- schema: schema,
826
- });
827
- }
828
-
829
- /**
830
- * Creates a hierarchical level of a schema tree. Nodes produce edges that can branch into sub-nodes with edges, recursively.
831
- */
832
- function createNodes(schema: SchemaObject): any {
833
- const nodes = [];
834
- // if (schema.discriminator !== undefined) {
835
- // return createDiscriminator(schema);
836
- // }
837
-
838
- if (schema.oneOf !== undefined || schema.anyOf !== undefined) {
839
- nodes.push(createAnyOneOf(schema));
840
- }
841
-
842
- if (schema.allOf !== undefined) {
843
- const { mergedSchemas } = mergeAllOf(schema.allOf);
844
- if (mergedSchemas.properties !== undefined) {
845
- nodes.push(createProperties(mergedSchemas));
846
- }
847
-
848
- if (mergedSchemas.items !== undefined) {
849
- nodes.push(createItems(mergedSchemas));
850
- }
851
- }
852
-
853
- if (schema.properties !== undefined) {
854
- nodes.push(createProperties(schema));
855
- }
856
-
857
- if (schema.additionalProperties !== undefined) {
858
- nodes.push(createAdditionalProperties(schema));
859
- }
860
-
861
- // TODO: figure out how to handle array of objects
862
- if (schema.items !== undefined) {
863
- nodes.push(createItems(schema));
864
- }
865
-
866
- if (nodes.length && nodes.length > 0) {
867
- return nodes.filter(Boolean).flat();
868
- }
869
-
870
- // primitive
871
- if (schema.type !== undefined) {
872
- if (schema.allOf) {
873
- //handle circular result in allOf
874
- if (schema.allOf.length && typeof schema.allOf[0] === "string") {
875
- return create("div", {
876
- style: {
877
- marginTop: ".5rem",
878
- marginBottom: ".5rem",
879
- marginLeft: "1rem",
880
- },
881
- children: createDescription(schema.allOf[0]),
882
- });
883
- }
884
- }
885
- return create("div", {
886
- style: {
887
- marginTop: ".5rem",
888
- marginBottom: ".5rem",
889
- marginLeft: "1rem",
890
- },
891
- children: createDescription(schema.type),
892
- });
893
- }
894
-
895
- // handle circular references
896
- if (typeof schema === "string") {
897
- return create("div", {
898
- style: {
899
- marginTop: ".5rem",
900
- marginBottom: ".5rem",
901
- marginLeft: "1rem",
902
- },
903
- children: [createDescription(schema)],
904
- });
905
- }
906
-
907
- // Unknown node/schema type should return undefined
908
- // So far, haven't seen this hit in testing
909
- return "any";
910
- }
911
-
912
20
  interface Props {
913
21
  style?: any;
914
22
  title: string;