From 9b73e41b7a644992851068d6cf327a5775eb7884 Mon Sep 17 00:00:00 2001 From: catosaurusrex2003 Date: Tue, 22 Oct 2024 23:41:28 +0530 Subject: [PATCH 1/6] clipped the recursion with a recursion limit of 10 --- library/src/helpers/schema.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/library/src/helpers/schema.ts b/library/src/helpers/schema.ts index 12846eab9..340296a71 100644 --- a/library/src/helpers/schema.ts +++ b/library/src/helpers/schema.ts @@ -518,7 +518,12 @@ export class SchemaHelpers { } // eslint-disable-next-line @typescript-eslint/no-explicit-any - private static jsonFieldToSchema(value: any): any { + private static jsonFieldToSchema(value: any, MAX_REC: number = 10): any { + // MAX_REC should never be passed as parameter. + // it is meant for internal recursion limit tracking + if (MAX_REC == 0) { + return {}; + } if (value === undefined || value === null) { return { type: 'string', @@ -545,7 +550,7 @@ export class SchemaHelpers { 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, MAX_REC - 1)), [this.extRenderAdditionalInfo]: false, }; } @@ -554,7 +559,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, MAX_REC - 1); return obj; }, {} as Record, From 79d7344368930f8ecbcbd1d1c4ff4eb0dc44bcd9 Mon Sep 17 00:00:00 2001 From: catosaurusrex2003 Date: Wed, 23 Oct 2024 00:00:43 +0530 Subject: [PATCH 2/6] linting --- library/src/helpers/schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/helpers/schema.ts b/library/src/helpers/schema.ts index 340296a71..384129e58 100644 --- a/library/src/helpers/schema.ts +++ b/library/src/helpers/schema.ts @@ -518,7 +518,7 @@ export class SchemaHelpers { } // eslint-disable-next-line @typescript-eslint/no-explicit-any - private static jsonFieldToSchema(value: any, MAX_REC: number = 10): any { + private static jsonFieldToSchema(value: any, MAX_REC = 10): any { // MAX_REC should never be passed as parameter. // it is meant for internal recursion limit tracking if (MAX_REC == 0) { From b6fd1e37cc915d12e6876577460d4b0b5d8ce1b0 Mon Sep 17 00:00:00 2001 From: catosaurusrex2003 Date: Mon, 4 Nov 2024 20:38:18 +0530 Subject: [PATCH 3/6] added-weaksets --- library/src/helpers/schema.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/library/src/helpers/schema.ts b/library/src/helpers/schema.ts index 384129e58..b1fe93bc0 100644 --- a/library/src/helpers/schema.ts +++ b/library/src/helpers/schema.ts @@ -518,12 +518,9 @@ export class SchemaHelpers { } // eslint-disable-next-line @typescript-eslint/no-explicit-any - private static jsonFieldToSchema(value: any, MAX_REC = 10): any { - // MAX_REC should never be passed as parameter. + 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 (MAX_REC == 0) { - return {}; - } if (value === undefined || value === null) { return { type: 'string', @@ -543,6 +540,12 @@ export class SchemaHelpers { [this.extRawValue]: true, }; } + + if (visited.has(value)) { + return {}; + } + visited.add(value); + if (this.isJSONSchema(value)) { return value; } @@ -550,7 +553,7 @@ export class SchemaHelpers { return { type: 'array', // eslint-disable-next-line @typescript-eslint/no-unsafe-return - items: value.map((v) => this.jsonFieldToSchema(v, MAX_REC - 1)), + items: value.map((v) => this.jsonFieldToSchema(v, visited)), [this.extRenderAdditionalInfo]: false, }; } @@ -559,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, MAX_REC - 1); + obj[k] = this.jsonFieldToSchema(v, visited); return obj; }, {} as Record, From a1575a274115f4f9953108693e74e08379a5d7c6 Mon Sep 17 00:00:00 2001 From: catosaurusrex2003 Date: Mon, 4 Nov 2024 20:43:20 +0530 Subject: [PATCH 4/6] lint-fix --- library/src/helpers/schema.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/helpers/schema.ts b/library/src/helpers/schema.ts index b1fe93bc0..b1fec1daa 100644 --- a/library/src/helpers/schema.ts +++ b/library/src/helpers/schema.ts @@ -541,10 +541,10 @@ export class SchemaHelpers { }; } - if (visited.has(value)) { + if (visited.has(value as object)) { return {}; } - visited.add(value); + visited.add(value as object); if (this.isJSONSchema(value)) { return value; From c1d35e125173667524873ad64e20df1d2c8f128d Mon Sep 17 00:00:00 2001 From: catosaurusrex2003 Date: Thu, 14 Nov 2024 16:37:50 +0530 Subject: [PATCH 5/6] added Error Message --- library/src/helpers/schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/helpers/schema.ts b/library/src/helpers/schema.ts index b1fec1daa..af57d61f2 100644 --- a/library/src/helpers/schema.ts +++ b/library/src/helpers/schema.ts @@ -542,7 +542,7 @@ export class SchemaHelpers { } if (visited.has(value as object)) { - return {}; + throw new Error("too much recursion. Please check document for recursion.") } visited.add(value as object); From e61fc94cdefd89e5eadd15c5519cea8c7bb47863 Mon Sep 17 00:00:00 2001 From: catosaurusrex2003 Date: Thu, 14 Nov 2024 16:46:36 +0530 Subject: [PATCH 6/6] linting --- library/src/helpers/schema.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/src/helpers/schema.ts b/library/src/helpers/schema.ts index af57d61f2..e057803a7 100644 --- a/library/src/helpers/schema.ts +++ b/library/src/helpers/schema.ts @@ -542,7 +542,9 @@ export class SchemaHelpers { } if (visited.has(value as object)) { - throw new Error("too much recursion. Please check document for recursion.") + throw new Error( + 'too much recursion. Please check document for recursion.', + ); } visited.add(value as object);