Skip to content

Commit a9ebe39

Browse files
committed
Improve circular ref detection
1 parent 21e4275 commit a9ebe39

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

packages/docusaurus-plugin-openapi-docs/src/openapi/utils/loadAndResolveSpec.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ async function resolveJsonRefs(specUrlOrObject: object | string) {
123123
}
124124
}
125125

126-
function markCircularRefs(obj: any, stack: any[] = []): boolean | "circular" {
126+
function markCircularRefs(
127+
obj: any,
128+
stack: any[] = [],
129+
key: string | undefined = undefined
130+
): boolean | "circular" {
127131
if (!obj || typeof obj !== "object") {
128132
return false;
129133
}
@@ -139,23 +143,29 @@ function markCircularRefs(obj: any, stack: any[] = []): boolean | "circular" {
139143
for (let i = 0; i < obj.length; i++) {
140144
const val = obj[i];
141145
if (typeof val === "object" && val !== null) {
142-
const res = markCircularRefs(val, stack);
146+
const res = markCircularRefs(val, stack, String(i));
143147
if (res) {
144148
if (res === "circular") {
145-
obj[i] = { title: val.title, "x-circular-ref": true };
149+
obj[i] = {
150+
title: val.title ?? String(i),
151+
"x-circular-ref": true,
152+
};
146153
}
147154
foundCircular = true;
148155
}
149156
}
150157
}
151158
} else {
152-
for (const key of Object.keys(obj)) {
153-
const val = obj[key];
159+
for (const k of Object.keys(obj)) {
160+
const val = obj[k];
154161
if (typeof val === "object" && val !== null) {
155-
const res = markCircularRefs(val, stack);
162+
const res = markCircularRefs(val, stack, k);
156163
if (res) {
157164
if (res === "circular") {
158-
obj[key] = { title: val.title, "x-circular-ref": true };
165+
obj[k] = {
166+
title: val.title ?? k,
167+
"x-circular-ref": true,
168+
};
159169
}
160170
foundCircular = true;
161171
}

0 commit comments

Comments
 (0)