Skip to content

Commit

Permalink
fix: application crash by clipping the recursion using WeakSet (#1101)
Browse files Browse the repository at this point in the history
* clipped the recursion with a recursion limit of 10

* linting

* added-weaksets

* lint-fix

* added Error Message

* linting

---------

Co-authored-by: asyncapi-bot <[email protected]>
Co-authored-by: Cody's Dad <[email protected]>
  • Loading branch information
3 people authored Jan 28, 2025
1 parent 94fdb21 commit 42b7314
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions library/src/helpers/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,9 @@ export class SchemaHelpers {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static jsonFieldToSchema(value: any): any {
private static jsonFieldToSchema(value: any, visited = new WeakSet()): any {
// visited should never be passed as parameter.
// it is meant for internal recursion limit tracking
if (value === undefined || value === null) {
return {
type: 'string',
Expand All @@ -538,14 +540,22 @@ export class SchemaHelpers {
[this.extRawValue]: true,
};
}

if (visited.has(value as object)) {
throw new Error(
'too much recursion. Please check document for recursion.',
);
}
visited.add(value as object);

if (this.isJSONSchema(value)) {
return value;
}
if (Array.isArray(value)) {
return {
type: 'array',
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
items: value.map((v) => this.jsonFieldToSchema(v)),
items: value.map((v) => this.jsonFieldToSchema(v, visited)),
[this.extRenderAdditionalInfo]: false,
};
}
Expand All @@ -554,7 +564,7 @@ export class SchemaHelpers {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
properties: Object.entries(value).reduce(
(obj, [k, v]) => {
obj[k] = this.jsonFieldToSchema(v);
obj[k] = this.jsonFieldToSchema(v, visited);
return obj;
},
{} as Record<string, unknown>,
Expand Down

0 comments on commit 42b7314

Please sign in to comment.