docusaurus-plugin-openapi-docs 1.5.1 → 1.5.2
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/createArrayBracket.d.ts +2 -0
- package/lib/markdown/createArrayBracket.js +37 -0
- package/lib/markdown/createRequestSchema.js +119 -79
- package/lib/markdown/createResponseSchema.js +123 -82
- package/lib/markdown/createStatusCodes.js +4 -6
- package/lib/openapi/createRequestExample.js +26 -7
- package/lib/openapi/createResponseExample.js +26 -7
- package/package.json +2 -2
- package/src/markdown/createArrayBracket.ts +35 -0
- package/src/markdown/createRequestSchema.ts +136 -95
- package/src/markdown/createResponseSchema.ts +143 -98
- package/src/markdown/createStatusCodes.ts +4 -8
- package/src/openapi/createRequestExample.ts +35 -12
- package/src/openapi/createResponseExample.ts +35 -12
|
@@ -6,6 +6,10 @@
|
|
|
6
6
|
* ========================================================================== */
|
|
7
7
|
|
|
8
8
|
import { MediaTypeObject, SchemaObject } from "../openapi/types";
|
|
9
|
+
import {
|
|
10
|
+
createClosingArrayBracket,
|
|
11
|
+
createOpeningArrayBracket,
|
|
12
|
+
} from "./createArrayBracket";
|
|
9
13
|
import { createDescription } from "./createDescription";
|
|
10
14
|
import { createDetails } from "./createDetails";
|
|
11
15
|
import { createDetailsSummary } from "./createDetailsSummary";
|
|
@@ -54,54 +58,63 @@ export function mergeAllOf(allOf: SchemaObject[]) {
|
|
|
54
58
|
*/
|
|
55
59
|
function createAnyOneOf(schema: SchemaObject): any {
|
|
56
60
|
const type = schema.oneOf ? "oneOf" : "anyOf";
|
|
57
|
-
return create("
|
|
61
|
+
return create("li", {
|
|
58
62
|
children: [
|
|
59
|
-
create("
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
+
}
|
|
77
|
+
|
|
78
|
+
if (anyOneSchema.allOf !== undefined) {
|
|
79
|
+
anyOneChildren.push(createNodes(anyOneSchema));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (anyOneSchema.items !== undefined) {
|
|
83
|
+
anyOneChildren.push(createItems(anyOneSchema));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (
|
|
87
|
+
anyOneSchema.type === "string" ||
|
|
88
|
+
anyOneSchema.type === "number" ||
|
|
89
|
+
anyOneSchema.type === "integer" ||
|
|
90
|
+
anyOneSchema.type === "boolean"
|
|
91
|
+
) {
|
|
92
|
+
anyOneChildren.push(createNodes(anyOneSchema));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (anyOneChildren.length) {
|
|
96
|
+
if (schema.type === "array") {
|
|
97
|
+
return create("TabItem", {
|
|
98
|
+
label: label,
|
|
99
|
+
value: `${index}-item-properties`,
|
|
100
|
+
children: [
|
|
101
|
+
createOpeningArrayBracket(),
|
|
102
|
+
anyOneChildren,
|
|
103
|
+
createClosingArrayBracket(),
|
|
104
|
+
]
|
|
105
|
+
.filter(Boolean)
|
|
106
|
+
.flat(),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return create("TabItem", {
|
|
110
|
+
label: label,
|
|
111
|
+
value: `${index}-item-properties`,
|
|
112
|
+
children: anyOneChildren,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return undefined;
|
|
117
|
+
}),
|
|
105
118
|
}),
|
|
106
119
|
],
|
|
107
120
|
});
|
|
@@ -152,11 +165,11 @@ function createAdditionalProperties(schema: SchemaObject) {
|
|
|
152
165
|
// }
|
|
153
166
|
|
|
154
167
|
if (
|
|
155
|
-
schema.additionalProperties?.type === "string" ||
|
|
156
|
-
schema.additionalProperties?.type === "object" ||
|
|
157
|
-
schema.additionalProperties?.type === "boolean" ||
|
|
158
|
-
schema.additionalProperties?.type === "integer" ||
|
|
159
|
-
schema.additionalProperties?.type === "number"
|
|
168
|
+
(schema.additionalProperties?.type as string) === "string" ||
|
|
169
|
+
(schema.additionalProperties?.type as string) === "object" ||
|
|
170
|
+
(schema.additionalProperties?.type as string) === "boolean" ||
|
|
171
|
+
(schema.additionalProperties?.type as string) === "integer" ||
|
|
172
|
+
(schema.additionalProperties?.type as string) === "number"
|
|
160
173
|
) {
|
|
161
174
|
const type = schema.additionalProperties?.type;
|
|
162
175
|
const additionalProperties =
|
|
@@ -224,15 +237,27 @@ function createAdditionalProperties(schema: SchemaObject) {
|
|
|
224
237
|
// TODO: figure out how to handle array of objects
|
|
225
238
|
function createItems(schema: SchemaObject) {
|
|
226
239
|
if (schema.items?.properties !== undefined) {
|
|
227
|
-
return
|
|
240
|
+
return [
|
|
241
|
+
createOpeningArrayBracket(),
|
|
242
|
+
createProperties(schema.items),
|
|
243
|
+
createClosingArrayBracket(),
|
|
244
|
+
].flat();
|
|
228
245
|
}
|
|
229
246
|
|
|
230
247
|
if (schema.items?.additionalProperties !== undefined) {
|
|
231
|
-
return
|
|
248
|
+
return [
|
|
249
|
+
createOpeningArrayBracket(),
|
|
250
|
+
createAdditionalProperties(schema.items),
|
|
251
|
+
createClosingArrayBracket(),
|
|
252
|
+
].flat();
|
|
232
253
|
}
|
|
233
254
|
|
|
234
255
|
if (schema.items?.oneOf !== undefined || schema.items?.anyOf !== undefined) {
|
|
235
|
-
return
|
|
256
|
+
return [
|
|
257
|
+
createOpeningArrayBracket(),
|
|
258
|
+
createAnyOneOf(schema.items!),
|
|
259
|
+
createClosingArrayBracket(),
|
|
260
|
+
].flat();
|
|
236
261
|
}
|
|
237
262
|
|
|
238
263
|
if (schema.items?.allOf !== undefined) {
|
|
@@ -249,12 +274,12 @@ function createItems(schema: SchemaObject) {
|
|
|
249
274
|
mergedSchemas.anyOf !== undefined) &&
|
|
250
275
|
mergedSchemas.properties
|
|
251
276
|
) {
|
|
252
|
-
return
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
277
|
+
return [
|
|
278
|
+
createOpeningArrayBracket(),
|
|
279
|
+
createAnyOneOf(mergedSchemas),
|
|
280
|
+
createProperties(mergedSchemas),
|
|
281
|
+
createClosingArrayBracket(),
|
|
282
|
+
].flat();
|
|
258
283
|
}
|
|
259
284
|
|
|
260
285
|
// Handles only anyOf/oneOf
|
|
@@ -262,16 +287,20 @@ function createItems(schema: SchemaObject) {
|
|
|
262
287
|
mergedSchemas.oneOf !== undefined ||
|
|
263
288
|
mergedSchemas.anyOf !== undefined
|
|
264
289
|
) {
|
|
265
|
-
return
|
|
266
|
-
|
|
267
|
-
|
|
290
|
+
return [
|
|
291
|
+
createOpeningArrayBracket(),
|
|
292
|
+
createAnyOneOf(mergedSchemas),
|
|
293
|
+
createClosingArrayBracket(),
|
|
294
|
+
].flat();
|
|
268
295
|
}
|
|
269
296
|
|
|
270
297
|
// Handles properties
|
|
271
298
|
if (mergedSchemas.properties !== undefined) {
|
|
272
|
-
return
|
|
273
|
-
|
|
274
|
-
|
|
299
|
+
return [
|
|
300
|
+
createOpeningArrayBracket(),
|
|
301
|
+
createProperties(mergedSchemas),
|
|
302
|
+
createClosingArrayBracket(),
|
|
303
|
+
].flat();
|
|
275
304
|
}
|
|
276
305
|
}
|
|
277
306
|
|
|
@@ -279,21 +308,30 @@ function createItems(schema: SchemaObject) {
|
|
|
279
308
|
schema.items?.type === "string" ||
|
|
280
309
|
schema.items?.type === "number" ||
|
|
281
310
|
schema.items?.type === "integer" ||
|
|
282
|
-
schema.items?.type === "boolean"
|
|
311
|
+
schema.items?.type === "boolean" ||
|
|
312
|
+
schema.items?.type === "object"
|
|
283
313
|
) {
|
|
284
|
-
return
|
|
314
|
+
return [
|
|
315
|
+
createOpeningArrayBracket(),
|
|
316
|
+
createNodes(schema.items),
|
|
317
|
+
createClosingArrayBracket(),
|
|
318
|
+
].flat();
|
|
285
319
|
}
|
|
286
320
|
|
|
287
321
|
// TODO: clean this up or eliminate it?
|
|
288
|
-
return
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
:
|
|
295
|
-
|
|
296
|
-
|
|
322
|
+
return [
|
|
323
|
+
createOpeningArrayBracket(),
|
|
324
|
+
Object.entries(schema.items!).map(([key, val]) =>
|
|
325
|
+
createEdges({
|
|
326
|
+
name: key,
|
|
327
|
+
schema: val,
|
|
328
|
+
required: Array.isArray(schema.required)
|
|
329
|
+
? schema.required.includes(key)
|
|
330
|
+
: false,
|
|
331
|
+
})
|
|
332
|
+
),
|
|
333
|
+
createClosingArrayBracket(),
|
|
334
|
+
].flat();
|
|
297
335
|
}
|
|
298
336
|
|
|
299
337
|
/**
|
|
@@ -419,6 +457,15 @@ function createDetailsNode(
|
|
|
419
457
|
style: { opacity: "0.6" },
|
|
420
458
|
children: ` ${schemaName}`,
|
|
421
459
|
}),
|
|
460
|
+
guard(schema.nullable && schema.nullable === true, () => [
|
|
461
|
+
create("strong", {
|
|
462
|
+
style: {
|
|
463
|
+
fontSize: "var(--ifm-code-font-size)",
|
|
464
|
+
color: "var(--openapi-nullable)",
|
|
465
|
+
},
|
|
466
|
+
children: " nullable",
|
|
467
|
+
}),
|
|
468
|
+
]),
|
|
422
469
|
guard(schema.required && schema.required === true, () => [
|
|
423
470
|
create("strong", {
|
|
424
471
|
style: {
|
|
@@ -597,11 +644,9 @@ function createEdges({
|
|
|
597
644
|
collapsible: false,
|
|
598
645
|
name,
|
|
599
646
|
required: false,
|
|
600
|
-
deprecated: mergedSchemas.deprecated,
|
|
601
|
-
schemaDescription: mergedSchemas.description,
|
|
602
647
|
schemaName: schemaName,
|
|
603
648
|
qualifierMessage: getQualifierMessage(schema),
|
|
604
|
-
|
|
649
|
+
schema: mergedSchemas,
|
|
605
650
|
});
|
|
606
651
|
}
|
|
607
652
|
|
|
@@ -631,11 +676,9 @@ function createEdges({
|
|
|
631
676
|
collapsible: false,
|
|
632
677
|
name,
|
|
633
678
|
required: false,
|
|
634
|
-
deprecated: schema.deprecated,
|
|
635
|
-
schemaDescription: schema.description,
|
|
636
679
|
schemaName: schemaName,
|
|
637
680
|
qualifierMessage: getQualifierMessage(schema),
|
|
638
|
-
|
|
681
|
+
schema: schema,
|
|
639
682
|
});
|
|
640
683
|
}
|
|
641
684
|
|
|
@@ -643,34 +686,42 @@ function createEdges({
|
|
|
643
686
|
* Creates a hierarchical level of a schema tree. Nodes produce edges that can branch into sub-nodes with edges, recursively.
|
|
644
687
|
*/
|
|
645
688
|
function createNodes(schema: SchemaObject): any {
|
|
689
|
+
const nodes = [];
|
|
646
690
|
// if (schema.discriminator !== undefined) {
|
|
647
691
|
// return createDiscriminator(schema);
|
|
648
692
|
// }
|
|
649
693
|
|
|
650
694
|
if (schema.oneOf !== undefined || schema.anyOf !== undefined) {
|
|
651
|
-
|
|
695
|
+
nodes.push(createAnyOneOf(schema));
|
|
652
696
|
}
|
|
653
697
|
|
|
654
698
|
if (schema.allOf !== undefined) {
|
|
655
699
|
const { mergedSchemas } = mergeAllOf(schema.allOf);
|
|
656
|
-
|
|
700
|
+
|
|
657
701
|
if (mergedSchemas.properties !== undefined) {
|
|
658
|
-
|
|
702
|
+
nodes.push(createProperties(mergedSchemas));
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
if (mergedSchemas.items !== undefined) {
|
|
706
|
+
nodes.push(createItems(mergedSchemas));
|
|
659
707
|
}
|
|
660
708
|
}
|
|
661
709
|
|
|
662
710
|
if (schema.properties !== undefined) {
|
|
663
|
-
|
|
711
|
+
nodes.push(createProperties(schema));
|
|
664
712
|
}
|
|
665
713
|
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
return createAdditionalProperties(schema);
|
|
714
|
+
if (schema.additionalProperties !== undefined) {
|
|
715
|
+
nodes.push(createAdditionalProperties(schema));
|
|
669
716
|
}
|
|
670
717
|
|
|
671
718
|
// TODO: figure out how to handle array of objects
|
|
672
719
|
if (schema.items !== undefined) {
|
|
673
|
-
|
|
720
|
+
nodes.push(createItems(schema));
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
if (nodes.length && nodes.length > 0) {
|
|
724
|
+
return nodes.filter(Boolean).flat();
|
|
674
725
|
}
|
|
675
726
|
|
|
676
727
|
// primitive
|
|
@@ -776,12 +827,6 @@ export function createResponseSchema({ title, body, ...rest }: Props) {
|
|
|
776
827
|
style: { textAlign: "left" },
|
|
777
828
|
children: [
|
|
778
829
|
create("strong", { children: `${title}` }),
|
|
779
|
-
guard(firstBody!.type === "array", (format) =>
|
|
780
|
-
create("span", {
|
|
781
|
-
style: { opacity: "0.6" },
|
|
782
|
-
children: ` array`,
|
|
783
|
-
})
|
|
784
|
-
),
|
|
785
830
|
guard(
|
|
786
831
|
body.required && body.required === true,
|
|
787
832
|
() => [
|
|
@@ -120,14 +120,10 @@ export function createResponseExamples(
|
|
|
120
120
|
}
|
|
121
121
|
return Object.entries(responseExamples).map(
|
|
122
122
|
([exampleName, exampleValue]: any) => {
|
|
123
|
-
const camelToSpaceName = exampleName.replace(/([A-Z])/g, " $1");
|
|
124
|
-
let finalFormattedName =
|
|
125
|
-
camelToSpaceName.charAt(0).toUpperCase() + camelToSpaceName.slice(1);
|
|
126
|
-
|
|
127
123
|
if (typeof exampleValue.value === "object") {
|
|
128
124
|
return create("TabItem", {
|
|
129
|
-
label: `${
|
|
130
|
-
value: `${
|
|
125
|
+
label: `${exampleName}`,
|
|
126
|
+
value: `${exampleName}`,
|
|
131
127
|
children: [
|
|
132
128
|
guard(exampleValue.summary, (summary) => [
|
|
133
129
|
create("p", {
|
|
@@ -142,8 +138,8 @@ export function createResponseExamples(
|
|
|
142
138
|
});
|
|
143
139
|
}
|
|
144
140
|
return create("TabItem", {
|
|
145
|
-
label: `${
|
|
146
|
-
value: `${
|
|
141
|
+
label: `${exampleName}`,
|
|
142
|
+
value: `${exampleName}`,
|
|
147
143
|
children: [
|
|
148
144
|
guard(exampleValue.summary, (summary) => [
|
|
149
145
|
create("p", {
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* ========================================================================== */
|
|
7
7
|
|
|
8
8
|
import chalk from "chalk";
|
|
9
|
+
import merge from "lodash/merge";
|
|
9
10
|
|
|
10
11
|
import { mergeAllOf } from "../markdown/createRequestSchema";
|
|
11
12
|
import { SchemaObject } from "./types";
|
|
@@ -77,18 +78,30 @@ function sampleRequestFromProp(name: string, prop: any, obj: any): any {
|
|
|
77
78
|
|
|
78
79
|
export const sampleRequestFromSchema = (schema: SchemaObject = {}): any => {
|
|
79
80
|
try {
|
|
80
|
-
|
|
81
|
+
// deep copy schema before processing
|
|
82
|
+
let schemaCopy = JSON.parse(JSON.stringify(schema));
|
|
83
|
+
let { type, example, allOf, properties, items, oneOf, anyOf } = schemaCopy;
|
|
81
84
|
|
|
82
85
|
if (example !== undefined) {
|
|
83
86
|
return example;
|
|
84
87
|
}
|
|
85
88
|
|
|
86
89
|
if (oneOf) {
|
|
90
|
+
if (properties) {
|
|
91
|
+
const combinedSchemas = merge(schemaCopy, oneOf[0]);
|
|
92
|
+
delete combinedSchemas.oneOf;
|
|
93
|
+
return sampleRequestFromSchema(combinedSchemas);
|
|
94
|
+
}
|
|
87
95
|
// Just go with first schema
|
|
88
96
|
return sampleRequestFromSchema(oneOf[0]);
|
|
89
97
|
}
|
|
90
98
|
|
|
91
99
|
if (anyOf) {
|
|
100
|
+
if (properties) {
|
|
101
|
+
const combinedSchemas = merge(schemaCopy, anyOf[0]);
|
|
102
|
+
delete combinedSchemas.anyOf;
|
|
103
|
+
return sampleRequestFromSchema(combinedSchemas);
|
|
104
|
+
}
|
|
92
105
|
// Just go with first schema
|
|
93
106
|
return sampleRequestFromSchema(anyOf[0]);
|
|
94
107
|
}
|
|
@@ -103,6 +116,11 @@ export const sampleRequestFromSchema = (schema: SchemaObject = {}): any => {
|
|
|
103
116
|
}
|
|
104
117
|
}
|
|
105
118
|
}
|
|
119
|
+
if (properties) {
|
|
120
|
+
const combinedSchemas = merge(schemaCopy, mergedSchemas);
|
|
121
|
+
delete combinedSchemas.allOf;
|
|
122
|
+
return sampleRequestFromSchema(combinedSchemas);
|
|
123
|
+
}
|
|
106
124
|
return sampleRequestFromSchema(mergedSchemas);
|
|
107
125
|
}
|
|
108
126
|
|
|
@@ -118,9 +136,9 @@ export const sampleRequestFromSchema = (schema: SchemaObject = {}): any => {
|
|
|
118
136
|
|
|
119
137
|
if (type === "object") {
|
|
120
138
|
let obj: any = {};
|
|
121
|
-
for (let [name, prop] of Object.entries(properties ?? {})) {
|
|
139
|
+
for (let [name, prop] of Object.entries(properties ?? {}) as any) {
|
|
122
140
|
if (prop.properties) {
|
|
123
|
-
for (const [key, value] of Object.entries(prop.properties)) {
|
|
141
|
+
for (const [key, value] of Object.entries(prop.properties) as any) {
|
|
124
142
|
if (
|
|
125
143
|
(value.readOnly && value.readOnly === true) ||
|
|
126
144
|
value.deprecated
|
|
@@ -131,7 +149,9 @@ export const sampleRequestFromSchema = (schema: SchemaObject = {}): any => {
|
|
|
131
149
|
}
|
|
132
150
|
|
|
133
151
|
if (prop.items && prop.items.properties) {
|
|
134
|
-
for (const [key, value] of Object.entries(
|
|
152
|
+
for (const [key, value] of Object.entries(
|
|
153
|
+
prop.items.properties
|
|
154
|
+
) as any) {
|
|
135
155
|
if (
|
|
136
156
|
(value.readOnly && value.readOnly === true) ||
|
|
137
157
|
value.deprecated
|
|
@@ -157,28 +177,31 @@ export const sampleRequestFromSchema = (schema: SchemaObject = {}): any => {
|
|
|
157
177
|
|
|
158
178
|
if (type === "array") {
|
|
159
179
|
if (Array.isArray(items?.anyOf)) {
|
|
160
|
-
return items?.anyOf.map((item) => sampleRequestFromSchema(item));
|
|
180
|
+
return items?.anyOf.map((item: any) => sampleRequestFromSchema(item));
|
|
161
181
|
}
|
|
162
182
|
|
|
163
183
|
if (Array.isArray(items?.oneOf)) {
|
|
164
|
-
return items?.oneOf.map((item) => sampleRequestFromSchema(item));
|
|
184
|
+
return items?.oneOf.map((item: any) => sampleRequestFromSchema(item));
|
|
165
185
|
}
|
|
166
186
|
|
|
167
187
|
return [sampleRequestFromSchema(items)];
|
|
168
188
|
}
|
|
169
189
|
|
|
170
|
-
if (
|
|
171
|
-
if (
|
|
172
|
-
return
|
|
190
|
+
if (schemaCopy.enum) {
|
|
191
|
+
if (schemaCopy.default) {
|
|
192
|
+
return schemaCopy.default;
|
|
173
193
|
}
|
|
174
|
-
return normalizeArray(
|
|
194
|
+
return normalizeArray(schemaCopy.enum)[0];
|
|
175
195
|
}
|
|
176
196
|
|
|
177
|
-
if (
|
|
197
|
+
if (
|
|
198
|
+
(schema.readOnly && schema.readOnly === true) ||
|
|
199
|
+
schemaCopy.deprecated
|
|
200
|
+
) {
|
|
178
201
|
return undefined;
|
|
179
202
|
}
|
|
180
203
|
|
|
181
|
-
return primitive(
|
|
204
|
+
return primitive(schemaCopy);
|
|
182
205
|
} catch (err) {
|
|
183
206
|
console.error(
|
|
184
207
|
chalk.yellow("WARNING: failed to create example from schema object:", err)
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* ========================================================================== */
|
|
7
7
|
|
|
8
8
|
import chalk from "chalk";
|
|
9
|
+
import merge from "lodash/merge";
|
|
9
10
|
|
|
10
11
|
import { mergeAllOf } from "../markdown/createResponseSchema";
|
|
11
12
|
import { SchemaObject } from "./types";
|
|
@@ -77,7 +78,9 @@ function sampleResponseFromProp(name: string, prop: any, obj: any): any {
|
|
|
77
78
|
|
|
78
79
|
export const sampleResponseFromSchema = (schema: SchemaObject = {}): any => {
|
|
79
80
|
try {
|
|
80
|
-
|
|
81
|
+
// deep copy schema before processing
|
|
82
|
+
let schemaCopy = JSON.parse(JSON.stringify(schema));
|
|
83
|
+
let { type, example, allOf, properties, items, oneOf, anyOf } = schemaCopy;
|
|
81
84
|
|
|
82
85
|
if (example !== undefined) {
|
|
83
86
|
return example;
|
|
@@ -96,15 +99,30 @@ export const sampleResponseFromSchema = (schema: SchemaObject = {}): any => {
|
|
|
96
99
|
}
|
|
97
100
|
}
|
|
98
101
|
}
|
|
102
|
+
if (properties) {
|
|
103
|
+
const combinedSchemas = merge(schemaCopy, mergedSchemas);
|
|
104
|
+
delete combinedSchemas.allOf;
|
|
105
|
+
return sampleResponseFromSchema(combinedSchemas);
|
|
106
|
+
}
|
|
99
107
|
return sampleResponseFromSchema(mergedSchemas);
|
|
100
108
|
}
|
|
101
109
|
|
|
102
110
|
if (oneOf) {
|
|
111
|
+
if (properties) {
|
|
112
|
+
const combinedSchemas = merge(schemaCopy, oneOf[0]);
|
|
113
|
+
delete combinedSchemas.oneOf;
|
|
114
|
+
return sampleResponseFromSchema(combinedSchemas);
|
|
115
|
+
}
|
|
103
116
|
// Just go with first schema
|
|
104
117
|
return sampleResponseFromSchema(oneOf[0]);
|
|
105
118
|
}
|
|
106
119
|
|
|
107
120
|
if (anyOf) {
|
|
121
|
+
if (properties) {
|
|
122
|
+
const combinedSchemas = merge(schemaCopy, anyOf[0]);
|
|
123
|
+
delete combinedSchemas.anyOf;
|
|
124
|
+
return sampleResponseFromSchema(combinedSchemas);
|
|
125
|
+
}
|
|
108
126
|
// Just go with first schema
|
|
109
127
|
return sampleResponseFromSchema(anyOf[0]);
|
|
110
128
|
}
|
|
@@ -121,9 +139,9 @@ export const sampleResponseFromSchema = (schema: SchemaObject = {}): any => {
|
|
|
121
139
|
|
|
122
140
|
if (type === "object") {
|
|
123
141
|
let obj: any = {};
|
|
124
|
-
for (let [name, prop] of Object.entries(properties ?? {})) {
|
|
142
|
+
for (let [name, prop] of Object.entries(properties ?? {}) as any) {
|
|
125
143
|
if (prop.properties) {
|
|
126
|
-
for (const [key, value] of Object.entries(prop.properties)) {
|
|
144
|
+
for (const [key, value] of Object.entries(prop.properties) as any) {
|
|
127
145
|
if (
|
|
128
146
|
(value.writeOnly && value.writeOnly === true) ||
|
|
129
147
|
value.deprecated
|
|
@@ -134,7 +152,9 @@ export const sampleResponseFromSchema = (schema: SchemaObject = {}): any => {
|
|
|
134
152
|
}
|
|
135
153
|
|
|
136
154
|
if (prop.items && prop.items.properties) {
|
|
137
|
-
for (const [key, value] of Object.entries(
|
|
155
|
+
for (const [key, value] of Object.entries(
|
|
156
|
+
prop.items.properties
|
|
157
|
+
) as any) {
|
|
138
158
|
if (
|
|
139
159
|
(value.writeOnly && value.writeOnly === true) ||
|
|
140
160
|
value.deprecated
|
|
@@ -160,28 +180,31 @@ export const sampleResponseFromSchema = (schema: SchemaObject = {}): any => {
|
|
|
160
180
|
|
|
161
181
|
if (type === "array") {
|
|
162
182
|
if (Array.isArray(items?.anyOf)) {
|
|
163
|
-
return items?.anyOf.map((item) => sampleResponseFromSchema(item));
|
|
183
|
+
return items?.anyOf.map((item: any) => sampleResponseFromSchema(item));
|
|
164
184
|
}
|
|
165
185
|
|
|
166
186
|
if (Array.isArray(items?.oneOf)) {
|
|
167
|
-
return items?.oneOf.map((item) => sampleResponseFromSchema(item));
|
|
187
|
+
return items?.oneOf.map((item: any) => sampleResponseFromSchema(item));
|
|
168
188
|
}
|
|
169
189
|
|
|
170
190
|
return [sampleResponseFromSchema(items)];
|
|
171
191
|
}
|
|
172
192
|
|
|
173
|
-
if (
|
|
174
|
-
if (
|
|
175
|
-
return
|
|
193
|
+
if (schemaCopy.enum) {
|
|
194
|
+
if (schemaCopy.default) {
|
|
195
|
+
return schemaCopy.default;
|
|
176
196
|
}
|
|
177
|
-
return normalizeArray(
|
|
197
|
+
return normalizeArray(schemaCopy.enum)[0];
|
|
178
198
|
}
|
|
179
199
|
|
|
180
|
-
if (
|
|
200
|
+
if (
|
|
201
|
+
(schemaCopy.writeOnly && schemaCopy.writeOnly === true) ||
|
|
202
|
+
schemaCopy.deprecated
|
|
203
|
+
) {
|
|
181
204
|
return undefined;
|
|
182
205
|
}
|
|
183
206
|
|
|
184
|
-
return primitive(
|
|
207
|
+
return primitive(schemaCopy);
|
|
185
208
|
} catch (err) {
|
|
186
209
|
console.error(
|
|
187
210
|
chalk.yellow("WARNING: failed to create example from schema object:", err)
|