Skip to content

Commit a21e6a7

Browse files
authored
[Bug] Support multiple same-level node types and improve items/array support (#397)
1 parent 2842e76 commit a21e6a7

File tree

11 files changed

+412
-216
lines changed

11 files changed

+412
-216
lines changed

demo/docusaurus.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@ const config = {
239239
specPath: "examples/food/yogurtstore/openapi.yaml",
240240
outputDir: "docs/food/yogurtstore",
241241
},
242+
config: {
243+
specPath:
244+
"https://raw.githubusercontent.com/PaloAltoNetworks/pan.dev/master/openapi-specs/access/prisma-access-config/AddressGroups.yaml",
245+
outputDir: "docs/config",
246+
sidebarOptions: {
247+
groupPathsBy: "tag",
248+
},
249+
},
242250
},
243251
},
244252
],

demo/sidebars.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ const sidebars = {
7070
},
7171
items: require("./docs/cos/sidebar.js"),
7272
},
73+
{
74+
type: "category",
75+
label: "Address Groups",
76+
link: {
77+
type: "generated-index",
78+
title: "Address Groups",
79+
slug: "/category/config-api",
80+
},
81+
items: require("./docs/config/sidebar.js"),
82+
},
7383
{
7484
type: "category",
7585
label: "Burgers",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 { create } from "./utils";
9+
10+
export function createOpeningArrayBracket() {
11+
return create("li", {
12+
children: create("div", {
13+
style: {
14+
fontSize: "var(--ifm-code-font-size)",
15+
opacity: "0.6",
16+
marginLeft: "-.5rem",
17+
paddingBottom: ".5rem",
18+
},
19+
children: ["Array ["],
20+
}),
21+
});
22+
}
23+
24+
export function createClosingArrayBracket() {
25+
return create("li", {
26+
children: create("div", {
27+
style: {
28+
fontSize: "var(--ifm-code-font-size)",
29+
opacity: "0.6",
30+
marginLeft: "-.5rem",
31+
},
32+
children: ["]"],
33+
}),
34+
});
35+
}

packages/docusaurus-plugin-openapi-docs/src/markdown/createRequestSchema.ts

Lines changed: 125 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
* ========================================================================== */
77

88
import { MediaTypeObject, SchemaObject } from "../openapi/types";
9+
import {
10+
createClosingArrayBracket,
11+
createOpeningArrayBracket,
12+
} from "./createArrayBracket";
913
import { createDescription } from "./createDescription";
1014
import { createDetails } from "./createDetails";
1115
import { createDetailsSummary } from "./createDetailsSummary";
@@ -49,54 +53,62 @@ export function mergeAllOf(allOf: SchemaObject[]) {
4953
*/
5054
function createAnyOneOf(schema: SchemaObject): any {
5155
const type = schema.oneOf ? "oneOf" : "anyOf";
52-
return create("div", {
56+
return create("li", {
5357
children: [
54-
create("div", {
55-
children: [
56-
create("span", {
57-
className: "badge badge--info",
58-
children: type,
59-
}),
60-
create("SchemaTabs", {
61-
children: schema[type]!.map((anyOneSchema, index) => {
62-
const label = anyOneSchema.title
63-
? anyOneSchema.title
64-
: `MOD${index + 1}`;
65-
const anyOneChildren = [];
66-
67-
if (anyOneSchema.properties !== undefined) {
68-
anyOneChildren.push(createProperties(anyOneSchema));
69-
}
70-
71-
if (anyOneSchema.allOf !== undefined) {
72-
anyOneChildren.push(createNodes(anyOneSchema));
73-
}
74-
75-
if (anyOneSchema.items !== undefined) {
76-
anyOneChildren.push(createItems(anyOneSchema));
77-
}
78-
79-
if (
80-
anyOneSchema.type === "string" ||
81-
anyOneSchema.type === "number" ||
82-
anyOneSchema.type === "integer" ||
83-
anyOneSchema.type === "boolean"
84-
) {
85-
anyOneChildren.push(createNodes(anyOneSchema));
86-
}
87-
88-
if (anyOneChildren.length) {
89-
return create("TabItem", {
90-
label: label,
91-
value: `${index}-item-properties`,
92-
children: anyOneChildren,
93-
});
94-
}
95-
96-
return undefined;
97-
}),
98-
}),
99-
],
58+
create("span", {
59+
className: "badge badge--info",
60+
children: type,
61+
}),
62+
create("SchemaTabs", {
63+
children: schema[type]!.map((anyOneSchema, index) => {
64+
const label = anyOneSchema.title
65+
? anyOneSchema.title
66+
: `MOD${index + 1}`;
67+
const anyOneChildren = [];
68+
69+
if (anyOneSchema.properties !== undefined) {
70+
anyOneChildren.push(createProperties(anyOneSchema));
71+
}
72+
73+
if (anyOneSchema.allOf !== undefined) {
74+
anyOneChildren.push(createNodes(anyOneSchema));
75+
}
76+
77+
if (anyOneSchema.items !== undefined) {
78+
anyOneChildren.push(createItems(anyOneSchema));
79+
}
80+
81+
if (
82+
anyOneSchema.type === "string" ||
83+
anyOneSchema.type === "number" ||
84+
anyOneSchema.type === "integer" ||
85+
anyOneSchema.type === "boolean"
86+
) {
87+
anyOneChildren.push(createNodes(anyOneSchema));
88+
}
89+
if (anyOneChildren.length) {
90+
if (schema.type === "array") {
91+
return create("TabItem", {
92+
label: label,
93+
value: `${index}-item-properties`,
94+
children: [
95+
createOpeningArrayBracket(),
96+
anyOneChildren,
97+
createClosingArrayBracket(),
98+
]
99+
.filter(Boolean)
100+
.flat(),
101+
});
102+
}
103+
return create("TabItem", {
104+
label: label,
105+
value: `${index}-item-properties`,
106+
children: anyOneChildren.filter(Boolean).flat(),
107+
});
108+
}
109+
110+
return undefined;
111+
}),
100112
}),
101113
],
102114
});
@@ -147,11 +159,11 @@ function createAdditionalProperties(schema: SchemaObject) {
147159
// }
148160

149161
if (
150-
schema.additionalProperties?.type === "string" ||
151-
schema.additionalProperties?.type === "object" ||
152-
schema.additionalProperties?.type === "boolean" ||
153-
schema.additionalProperties?.type === "integer" ||
154-
schema.additionalProperties?.type === "number"
162+
(schema.additionalProperties?.type as string) === "string" ||
163+
(schema.additionalProperties?.type as string) === "object" ||
164+
(schema.additionalProperties?.type as string) === "boolean" ||
165+
(schema.additionalProperties?.type as string) === "integer" ||
166+
(schema.additionalProperties?.type as string) === "number"
155167
) {
156168
const type = schema.additionalProperties?.type;
157169
const additionalProperties =
@@ -219,15 +231,27 @@ function createAdditionalProperties(schema: SchemaObject) {
219231
// TODO: figure out how to handle array of objects
220232
function createItems(schema: SchemaObject) {
221233
if (schema.items?.properties !== undefined) {
222-
return createProperties(schema.items);
234+
return [
235+
createOpeningArrayBracket(),
236+
createProperties(schema.items),
237+
createClosingArrayBracket(),
238+
].flat();
223239
}
224240

225241
if (schema.items?.additionalProperties !== undefined) {
226-
return createAdditionalProperties(schema.items);
242+
return [
243+
createOpeningArrayBracket(),
244+
createAdditionalProperties(schema.items),
245+
createClosingArrayBracket(),
246+
].flat();
227247
}
228248

229249
if (schema.items?.oneOf !== undefined || schema.items?.anyOf !== undefined) {
230-
return createAnyOneOf(schema.items!);
250+
return [
251+
createOpeningArrayBracket(),
252+
createAnyOneOf(schema.items!),
253+
createClosingArrayBracket(),
254+
].flat();
231255
}
232256

233257
if (schema.items?.allOf !== undefined) {
@@ -244,51 +268,64 @@ function createItems(schema: SchemaObject) {
244268
mergedSchemas.anyOf !== undefined) &&
245269
mergedSchemas.properties
246270
) {
247-
return create("div", {
248-
children: [
249-
createAnyOneOf(mergedSchemas),
250-
createProperties(mergedSchemas),
251-
],
252-
});
271+
return [
272+
createOpeningArrayBracket(),
273+
createAnyOneOf(mergedSchemas),
274+
createProperties(mergedSchemas),
275+
createClosingArrayBracket(),
276+
].flat();
253277
}
254278

255279
// Handles only anyOf/oneOf
256280
if (
257281
mergedSchemas.oneOf !== undefined ||
258282
mergedSchemas.anyOf !== undefined
259283
) {
260-
return create("div", {
261-
children: [createAnyOneOf(mergedSchemas)],
262-
});
284+
return [
285+
createOpeningArrayBracket(),
286+
createAnyOneOf(mergedSchemas),
287+
createClosingArrayBracket(),
288+
].flat();
263289
}
264290

265291
// Handles properties
266292
if (mergedSchemas.properties !== undefined) {
267-
return create("div", {
268-
children: [createProperties(mergedSchemas)],
269-
});
293+
return [
294+
createOpeningArrayBracket(),
295+
createProperties(mergedSchemas),
296+
createClosingArrayBracket(),
297+
].flat();
270298
}
271299
}
272300

273301
if (
274302
schema.items?.type === "string" ||
275303
schema.items?.type === "number" ||
276304
schema.items?.type === "integer" ||
277-
schema.items?.type === "boolean"
305+
schema.items?.type === "boolean" ||
306+
schema.items?.type === "object"
278307
) {
279-
return createNodes(schema.items);
308+
return [
309+
createOpeningArrayBracket(),
310+
createNodes(schema.items),
311+
createClosingArrayBracket(),
312+
].flat();
280313
}
281314

282315
// TODO: clean this up or eliminate it?
283-
return Object.entries(schema.items!).map(([key, val]) =>
284-
createEdges({
285-
name: key,
286-
schema: val,
287-
required: Array.isArray(schema.required)
288-
? schema.required.includes(key)
289-
: false,
290-
})
291-
);
316+
return [
317+
createOpeningArrayBracket(),
318+
Object.entries(schema.items!).map(([key, val]) =>
319+
createEdges({
320+
name: key,
321+
schema: val,
322+
required: Array.isArray(schema.required)
323+
? schema.required.includes(key)
324+
: false,
325+
})
326+
),
327+
createClosingArrayBracket(),
328+
].flat();
292329
}
293330

294331
/**
@@ -648,34 +685,39 @@ function createEdges({
648685
* Creates a hierarchical level of a schema tree. Nodes produce edges that can branch into sub-nodes with edges, recursively.
649686
*/
650687
function createNodes(schema: SchemaObject): any {
688+
const nodes = [];
651689
// if (schema.discriminator !== undefined) {
652690
// return createDiscriminator(schema);
653691
// }
654692

655693
if (schema.oneOf !== undefined || schema.anyOf !== undefined) {
656-
return createAnyOneOf(schema);
694+
nodes.push(createAnyOneOf(schema));
657695
}
658696

659697
if (schema.allOf !== undefined) {
660698
const { mergedSchemas } = mergeAllOf(schema.allOf);
661699

662700
// allOf seems to always result in properties
663701
if (mergedSchemas.properties !== undefined) {
664-
return createProperties(mergedSchemas);
702+
nodes.push(createProperties(mergedSchemas));
665703
}
666704
}
667705

668706
if (schema.properties !== undefined) {
669-
return createProperties(schema);
707+
nodes.push(createProperties(schema));
670708
}
671709

672710
if (schema.additionalProperties !== undefined) {
673-
return createAdditionalProperties(schema);
711+
nodes.push(createAdditionalProperties(schema));
674712
}
675713

676714
// TODO: figure out how to handle array of objects
677715
if (schema.items !== undefined) {
678-
return createItems(schema);
716+
nodes.push(createItems(schema));
717+
}
718+
719+
if (nodes.length && nodes.length > 0) {
720+
return nodes.filter(Boolean).flat();
679721
}
680722

681723
// primitive
@@ -763,12 +805,6 @@ export function createRequestSchema({ title, body, ...rest }: Props) {
763805
style: { textAlign: "left" },
764806
children: [
765807
create("strong", { children: `${title}` }),
766-
guard(firstBody.type === "array", (format) =>
767-
create("span", {
768-
style: { opacity: "0.6" },
769-
children: ` array`,
770-
})
771-
),
772808
guard(body.required && body.required === true, () => [
773809
create("strong", {
774810
style: {

0 commit comments

Comments
 (0)