Skip to content

Commit ec2e285

Browse files
committed
feat: support circular labels from x-circular-ref
1 parent 251e377 commit ec2e285

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -784,24 +784,42 @@ export function createNodes(
784784

785785
if (schema.allOf !== undefined) {
786786
const circularItems = schema.allOf.filter((item: any) => {
787-
return typeof item === "string" && item.includes("circular");
787+
if (typeof item === "string" && item.includes("circular")) {
788+
return true;
789+
}
790+
if (typeof item === "object" && item["x-circular-ref"]) {
791+
return true;
792+
}
793+
return false;
788794
});
789795

790796
for (const label of circularItems) {
797+
const schemaName =
798+
typeof label === "string"
799+
? label
800+
: label.title
801+
? `circular(${label.title})`
802+
: "circular()";
791803
nodes.push(
792804
create("SchemaItem", {
793805
collapsible: false,
794806
name: "",
795807
required: false,
796-
schemaName: label,
808+
schemaName,
797809
qualifierMessage: undefined,
798810
schema: {},
799811
})
800812
);
801813
}
802814

803815
const rest = schema.allOf.filter((item: any) => {
804-
return !(typeof item === "string" && item.includes("circular"));
816+
if (typeof item === "string" && item.includes("circular")) {
817+
return false;
818+
}
819+
if (typeof item === "object" && item["x-circular-ref"]) {
820+
return false;
821+
}
822+
return true;
805823
});
806824

807825
if (rest.length) {
@@ -828,14 +846,25 @@ export function createNodes(
828846
if (schema.type !== undefined) {
829847
if (schema.allOf) {
830848
//handle circular result in allOf
831-
if (schema.allOf.length && typeof schema.allOf[0] === "string") {
849+
const first: any = schema.allOf[0];
850+
if (
851+
schema.allOf.length &&
852+
((typeof first === "string" && first.includes("circular")) ||
853+
(typeof first === "object" && first["x-circular-ref"]))
854+
) {
855+
const label =
856+
typeof first === "string"
857+
? first
858+
: first.title
859+
? `circular(${first.title})`
860+
: "circular()";
832861
return create("div", {
833862
style: {
834863
marginTop: ".5rem",
835864
marginBottom: ".5rem",
836865
marginLeft: "1rem",
837866
},
838-
children: createDescription(schema.allOf[0]),
867+
children: createDescription(label),
839868
});
840869
}
841870
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ function prettyName(schema: SchemaObject | string, circular?: boolean) {
1111
if (typeof schema === "string") {
1212
return schema.startsWith("circular(") ? schema : "";
1313
}
14+
if (schema["x-circular-ref"]) {
15+
return schema.title ? `circular(${schema.title})` : "circular()";
16+
}
1417
if (schema.format) {
1518
if (schema.type) {
1619
return `${schema.type}<${schema.format}>`;

packages/docusaurus-theme-openapi-docs/src/theme/SchemaItem/index.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,16 @@ export default function SchemaItem(props: Props) {
9797
? schemaName.match(/^circular\\(([^)]*)\\)/)
9898
: null;
9999

100-
const renderCircular = guard(circularMatch, () => (
101-
<span className="openapi-schema__circular">
102-
{circularMatch ? `circular(${circularMatch[1]})` : "circular"}
103-
</span>
100+
const circularTitle = schema?.["x-circular-ref"]
101+
? schema.title
102+
? `circular(${schema.title})`
103+
: "circular"
104+
: circularMatch
105+
? `circular(${circularMatch[1]})`
106+
: null;
107+
108+
const renderCircular = guard(circularTitle, () => (
109+
<span className="openapi-schema__circular">{circularTitle}</span>
104110
));
105111

106112
const renderEnumDescriptions = guard(

0 commit comments

Comments
 (0)