Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: application crash by clipping the recursion using WeakSet #1101

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Changes from 5 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
14 changes: 11 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,20 @@ export class SchemaHelpers {
[this.extRawValue]: true,
};
}

if (visited.has(value as object)) {
return {};
}
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 +562,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
Loading