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