From c1af82bfc3f21753a45d5fc43d1f7d56cd698776 Mon Sep 17 00:00:00 2001 From: sb-rohitdesai Date: Mon, 3 Jun 2024 20:26:22 +0530 Subject: [PATCH 1/6] code optimisation for circular ref issue --- lib/dereference.js | 93 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 18 deletions(-) diff --git a/lib/dereference.js b/lib/dereference.js index 3fdcafab..93c3324f 100644 --- a/lib/dereference.js +++ b/lib/dereference.js @@ -14,10 +14,18 @@ module.exports = dereference; * @param {$RefParser} parser * @param {$RefParserOptions} options */ -function dereference (parser, options) { +async function dereference(parser, options) { parser.$refs.propertyMap = {}; // we assign a new object prior to another dereference process // console.log('Dereferencing $ref pointers in %s', parser.$refs._root$Ref.path); - let dereferenced = crawl(parser.schema, parser.$refs._root$Ref.path, "#", [], {}, parser.$refs, options); + let dereferenced = await crawl( + parser.schema, + parser.$refs._root$Ref.path, + "#", + [], + {}, + parser.$refs, + options + ); parser.$refs.circular = dereferenced.circular; parser.schema = dereferenced.value; } @@ -34,22 +42,37 @@ function dereference (parser, options) { * @param {$RefParserOptions} options * @returns {{value: object, circular: boolean}} */ -function crawl (obj, path, pathFromRoot, parents, dereferencedCache, $refs, options) { +async function crawl( + obj, + path, + pathFromRoot, + parents, + dereferencedCache, + $refs, + options +) { let dereferenced; let result = { value: obj, - circular: false + circular: false, }; if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj)) { parents.push(obj); if ($Ref.isAllowed$Ref(obj, options)) { - dereferenced = dereference$Ref(obj, path, pathFromRoot, parents, dereferencedCache, $refs, options); + dereferenced = await dereference$Ref( + obj, + path, + pathFromRoot, + parents, + dereferencedCache, + $refs, + options + ); result.circular = dereferenced.circular; result.value = dereferenced.value; - } - else { + } else { for (let key of Object.keys(obj)) { let keyPath = Pointer.join(path, key); let keyPathFromRoot = Pointer.join(pathFromRoot, key); @@ -57,23 +80,37 @@ function crawl (obj, path, pathFromRoot, parents, dereferencedCache, $refs, opti let circular = false; if ($Ref.isAllowed$Ref(value, options)) { - dereferenced = dereference$Ref(value, keyPath, keyPathFromRoot, parents, dereferencedCache, $refs, options); + dereferenced = await dereference$Ref( + value, + keyPath, + keyPathFromRoot, + parents, + dereferencedCache, + $refs, + options + ); circular = dereferenced.circular; // Avoid pointless mutations; breaks frozen objects to no profit if (obj[key] !== dereferenced.value) { obj[key] = dereferenced.value; } - } - else { + } else { if (parents.indexOf(value) === -1) { - dereferenced = crawl(value, keyPath, keyPathFromRoot, parents, dereferencedCache, $refs, options); + dereferenced = await crawl( + value, + keyPath, + keyPathFromRoot, + parents, + dereferencedCache, + $refs, + options + ); circular = dereferenced.circular; // Avoid pointless mutations; breaks frozen objects to no profit if (obj[key] !== dereferenced.value) { obj[key] = dereferenced.value; } - } - else { + } else { circular = foundCircularReference(keyPath, $refs, options); } } @@ -101,7 +138,15 @@ function crawl (obj, path, pathFromRoot, parents, dereferencedCache, $refs, opti * @param {$RefParserOptions} options * @returns {{value: object, circular: boolean}} */ -function dereference$Ref ($ref, path, pathFromRoot, parents, dereferencedCache, $refs, options) { +async function dereference$Ref( + $ref, + path, + pathFromRoot, + parents, + dereferencedCache, + $refs, + options +) { // console.log('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path); let $refPath = url.resolve(path, $ref.$ref); @@ -147,12 +192,24 @@ function dereference$Ref ($ref, path, pathFromRoot, parents, dereferencedCache, // Crawl the dereferenced value (unless it's circular) if (!circular) { // Determine if the dereferenced value is circular - let dereferenced = crawl(dereferencedValue, pointer.path, pathFromRoot, parents, dereferencedCache, $refs, options); + let dereferenced = await crawl( + dereferencedValue, + pointer.path, + pathFromRoot, + parents, + dereferencedCache, + $refs, + options + ); circular = dereferenced.circular; dereferencedValue = dereferenced.value; } - if (circular && !directCircular && options.dereference.circular === "ignore") { + if ( + circular && + !directCircular && + options.dereference.circular === "ignore" + ) { // The user has chosen to "ignore" circular references, so don't change the value dereferencedValue = $ref; } @@ -165,7 +222,7 @@ function dereference$Ref ($ref, path, pathFromRoot, parents, dereferencedCache, const dereferencedObject = { circular, - value: dereferencedValue + value: dereferencedValue, }; // only cache if no extra properties than $ref @@ -185,7 +242,7 @@ function dereference$Ref ($ref, path, pathFromRoot, parents, dereferencedCache, * @param {$RefParserOptions} options * @returns {boolean} - always returns true, to indicate that a circular reference was found */ -function foundCircularReference (keyPath, $refs, options) { +function foundCircularReference(keyPath, $refs, options) { $refs.circular = true; if (!options.dereference.circular) { throw ono.reference(`Circular $ref pointer found at ${keyPath}`); From 36d8a2de6c38248c6b1b40f7c24036603ba4d357 Mon Sep 17 00:00:00 2001 From: sb-rohitdesai Date: Tue, 4 Jun 2024 17:55:32 +0530 Subject: [PATCH 2/6] removed lint issues --- lib/dereference.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/dereference.js b/lib/dereference.js index 93c3324f..529029a8 100644 --- a/lib/dereference.js +++ b/lib/dereference.js @@ -14,7 +14,7 @@ module.exports = dereference; * @param {$RefParser} parser * @param {$RefParserOptions} options */ -async function dereference(parser, options) { +async function dereference (parser, options) { parser.$refs.propertyMap = {}; // we assign a new object prior to another dereference process // console.log('Dereferencing $ref pointers in %s', parser.$refs._root$Ref.path); let dereferenced = await crawl( @@ -42,7 +42,7 @@ async function dereference(parser, options) { * @param {$RefParserOptions} options * @returns {{value: object, circular: boolean}} */ -async function crawl( +async function crawl ( obj, path, pathFromRoot, @@ -72,7 +72,8 @@ async function crawl( ); result.circular = dereferenced.circular; result.value = dereferenced.value; - } else { + } + else { for (let key of Object.keys(obj)) { let keyPath = Pointer.join(path, key); let keyPathFromRoot = Pointer.join(pathFromRoot, key); @@ -94,7 +95,8 @@ async function crawl( if (obj[key] !== dereferenced.value) { obj[key] = dereferenced.value; } - } else { + } + else { if (parents.indexOf(value) === -1) { dereferenced = await crawl( value, @@ -110,7 +112,8 @@ async function crawl( if (obj[key] !== dereferenced.value) { obj[key] = dereferenced.value; } - } else { + } + else { circular = foundCircularReference(keyPath, $refs, options); } } @@ -138,7 +141,7 @@ async function crawl( * @param {$RefParserOptions} options * @returns {{value: object, circular: boolean}} */ -async function dereference$Ref( +async function dereference$Ref ( $ref, path, pathFromRoot, @@ -242,7 +245,7 @@ async function dereference$Ref( * @param {$RefParserOptions} options * @returns {boolean} - always returns true, to indicate that a circular reference was found */ -function foundCircularReference(keyPath, $refs, options) { +function foundCircularReference (keyPath, $refs, options) { $refs.circular = true; if (!options.dereference.circular) { throw ono.reference(`Circular $ref pointer found at ${keyPath}`); From b00172c47f82d92cc83004560268c51b41d44ddc Mon Sep 17 00:00:00 2001 From: sb-rohitdesai Date: Tue, 4 Jun 2024 18:07:47 +0530 Subject: [PATCH 3/6] removed lint-space issues --- lib/dereference.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/dereference.js b/lib/dereference.js index 529029a8..db84d7eb 100644 --- a/lib/dereference.js +++ b/lib/dereference.js @@ -72,7 +72,7 @@ async function crawl ( ); result.circular = dereferenced.circular; result.value = dereferenced.value; - } + } else { for (let key of Object.keys(obj)) { let keyPath = Pointer.join(path, key); @@ -95,7 +95,7 @@ async function crawl ( if (obj[key] !== dereferenced.value) { obj[key] = dereferenced.value; } - } + } else { if (parents.indexOf(value) === -1) { dereferenced = await crawl( @@ -112,7 +112,7 @@ async function crawl ( if (obj[key] !== dereferenced.value) { obj[key] = dereferenced.value; } - } + } else { circular = foundCircularReference(keyPath, $refs, options); } From ba3842db50d37602c12b4720bd07cf39b8dfcc86 Mon Sep 17 00:00:00 2001 From: sb-rohitdesai Date: Thu, 13 Jun 2024 12:09:56 +0530 Subject: [PATCH 4/6] fix: added await to complete async await functionality --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index aea52ee1..44c36104 100644 --- a/lib/index.js +++ b/lib/index.js @@ -268,7 +268,7 @@ $RefParser.prototype.dereference = async function dereference (path, schema, opt try { await this.resolve(args.path, args.schema, args.options); - _dereference(me, args.options); + await _dereference(me, args.options); finalize(me); return maybe(args.callback, Promise.resolve(me.schema)); } From b0cc5f190fe6998e992e1016b9aa5864918449dd Mon Sep 17 00:00:00 2001 From: sb-rohitdesai Date: Thu, 13 Jun 2024 16:17:25 +0530 Subject: [PATCH 5/6] fix: remove extra semi colons --- lib/dereference.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/dereference.js b/lib/dereference.js index db84d7eb..ad3a21b0 100644 --- a/lib/dereference.js +++ b/lib/dereference.js @@ -54,7 +54,7 @@ async function crawl ( let dereferenced; let result = { value: obj, - circular: false, + circular: false }; if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj)) { @@ -225,7 +225,7 @@ async function dereference$Ref ( const dereferencedObject = { circular, - value: dereferencedValue, + value: dereferencedValue }; // only cache if no extra properties than $ref @@ -251,4 +251,4 @@ function foundCircularReference (keyPath, $refs, options) { throw ono.reference(`Circular $ref pointer found at ${keyPath}`); } return true; -} +} \ No newline at end of file From 791d0224eb0ac8596733b197d622cf734b4a7e9a Mon Sep 17 00:00:00 2001 From: sb-rohitdesai Date: Thu, 13 Jun 2024 16:23:45 +0530 Subject: [PATCH 6/6] fix: added new line at end of the code --- lib/dereference.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dereference.js b/lib/dereference.js index ad3a21b0..5f9ba4e3 100644 --- a/lib/dereference.js +++ b/lib/dereference.js @@ -251,4 +251,4 @@ function foundCircularReference (keyPath, $refs, options) { throw ono.reference(`Circular $ref pointer found at ${keyPath}`); } return true; -} \ No newline at end of file +}