Skip to content

Commit

Permalink
code optimisation for circular ref issue
Browse files Browse the repository at this point in the history
  • Loading branch information
SB-rohitdesai committed Jun 3, 2024
1 parent 2299bcc commit c1af82b
Showing 1 changed file with 75 additions and 18 deletions.
93 changes: 75 additions & 18 deletions lib/dereference.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ module.exports = dereference;
* @param {$RefParser} parser
* @param {$RefParserOptions} options
*/
function dereference (parser, options) {
async function dereference(parser, options) {

Check failure on line 17 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Missing space before function parentheses

Check failure on line 17 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Missing space before function parentheses

Check failure on line 17 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Missing space before function parentheses

Check failure on line 17 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Missing space before function parentheses

Check failure on line 17 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Missing space before function parentheses

Check failure on line 17 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Missing space before function parentheses
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;
}
Expand All @@ -34,46 +42,75 @@ function dereference (parser, options) {
* @param {$RefParserOptions} options
* @returns {{value: object, circular: boolean}}
*/
function crawl (obj, path, pathFromRoot, parents, dereferencedCache, $refs, options) {
async function crawl(

Check failure on line 45 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Missing space before function parentheses

Check failure on line 45 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Missing space before function parentheses

Check failure on line 45 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Missing space before function parentheses

Check failure on line 45 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Missing space before function parentheses

Check failure on line 45 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Missing space before function parentheses

Check failure on line 45 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Missing space before function parentheses
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 {

Check failure on line 75 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 75 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 75 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 75 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 75 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 75 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Closing curly brace appears on the same line as the subsequent block
for (let key of Object.keys(obj)) {
let keyPath = Pointer.join(path, key);
let keyPathFromRoot = Pointer.join(pathFromRoot, key);
let value = obj[key];
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 {

Check failure on line 97 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 97 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 97 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 97 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 97 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 97 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Closing curly brace appears on the same line as the subsequent block
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 {

Check failure on line 113 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 113 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 113 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 113 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 113 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Closing curly brace appears on the same line as the subsequent block

Check failure on line 113 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Closing curly brace appears on the same line as the subsequent block
circular = foundCircularReference(keyPath, $refs, options);
}
}
Expand Down Expand Up @@ -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(

Check failure on line 141 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Missing space before function parentheses

Check failure on line 141 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Missing space before function parentheses

Check failure on line 141 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Missing space before function parentheses

Check failure on line 141 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Missing space before function parentheses

Check failure on line 141 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Missing space before function parentheses

Check failure on line 141 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Missing space before function parentheses
$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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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) {

Check failure on line 245 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Missing space before function parentheses

Check failure on line 245 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Missing space before function parentheses

Check failure on line 245 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Missing space before function parentheses

Check failure on line 245 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 20 on ubuntu-latest

Missing space before function parentheses

Check failure on line 245 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Missing space before function parentheses

Check failure on line 245 in lib/dereference.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Missing space before function parentheses
$refs.circular = true;
if (!options.dereference.circular) {
throw ono.reference(`Circular $ref pointer found at ${keyPath}`);
Expand Down

0 comments on commit c1af82b

Please sign in to comment.