Skip to content

Commit 7bc3853

Browse files
authored
fix: prevent infinite recursion in mergeAllOfSchemas by tracking visited schemas - hope this works (#132)
1 parent 6559751 commit 7bc3853

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

bin/modules/simplified-openapi.mjs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ function reduceProperties(schema, schemaName) {
279279
}
280280
}
281281

282-
function mergeAllOfSchemas(allOfArray, allSchemas) {
282+
function mergeAllOfSchemas(allOfArray, allSchemas, visited = new Set()) {
283283
const merged = {
284284
type: 'object',
285285
properties: {},
@@ -288,11 +288,29 @@ function mergeAllOfSchemas(allOfArray, allSchemas) {
288288
allOfArray.forEach((item) => {
289289
if (item.$ref) {
290290
const refSchemaName = item.$ref.replace('#/components/schemas/', '');
291+
292+
if (visited.has(refSchemaName)) {
293+
return;
294+
}
295+
visited.add(refSchemaName);
296+
291297
const refSchema = allSchemas[refSchemaName];
292298
if (refSchema) {
293299
console.log(
294-
`Processing ref ${refSchemaName} for ${item.title}, exists: true, has properties: ${!!refSchema.properties}`
300+
`Processing ref ${refSchemaName} for ${item.title}, exists: true, has properties: ${!!refSchema.properties}, has allOf: ${!!refSchema.allOf}`
295301
);
302+
303+
if (refSchema.allOf) {
304+
const nestedMerged = mergeAllOfSchemas(refSchema.allOf, allSchemas, new Set(visited));
305+
Object.assign(merged.properties, nestedMerged.properties);
306+
if (nestedMerged.required) {
307+
merged.required = [...(merged.required || []), ...nestedMerged.required];
308+
}
309+
if (nestedMerged.description && !merged.description) {
310+
merged.description = nestedMerged.description;
311+
}
312+
}
313+
296314
if (refSchema.properties) {
297315
console.log(`Ensuring ${item.title} has all required properties from ${refSchemaName}`);
298316
Object.assign(merged.properties, refSchema.properties);

0 commit comments

Comments
 (0)