Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,29 @@ export const operationClasses = ({
}
}

// Map path to PascalCase class names
const transformedPath = path.map((value) =>
operationClassName({
context,
value,
}),
);

// Remove duplicate class names to prevent circular references
// Keep only the first occurrence of each class name
const uniquePath: Array<string> = [];
const seen = new Set<string>();
for (const name of transformedPath) {
if (!seen.has(name)) {
seen.add(name);
uniquePath.push(name);
}
}

classNames.set(rootClass, {
className: finalClassName,
methodName: methodName || getOperationMethodName({ operation, plugin }),
path: path.map((value) =>
operationClassName({
context,
value,
}),
),
path: uniquePath,
});
}

Expand Down
25 changes: 23 additions & 2 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,29 @@ const generateClassSdk = ({
symbolParentClass.placeholder !== symbolCurrentClass.placeholder
) {
const parentClass = sdkClasses.get(symbolParentClass.id)!;
parentClass.classes.add(symbolCurrentClass.id);
sdkClasses.set(symbolParentClass.id, parentClass);
// Check if adding this child would create a cycle
// A cycle exists if the current class is an ancestor of the parent class
const wouldCreateCycle = (
childId: number,
ancestorId: number,
): boolean => {
const child = sdkClasses.get(childId);
if (!child) return false;
if (child.id === ancestorId) return true;
for (const grandchildId of child.classes) {
if (wouldCreateCycle(grandchildId, ancestorId)) {
return true;
}
}
return false;
};

if (
!wouldCreateCycle(symbolCurrentClass.id, symbolParentClass.id)
) {
parentClass.classes.add(symbolCurrentClass.id);
sdkClasses.set(symbolParentClass.id, parentClass);
}
}
}

Expand Down
Loading