diff --git a/lib/dereference.js b/lib/dereference.js index 3fdcafab..5f9ba4e3 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,7 +42,15 @@ 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, @@ -45,7 +61,15 @@ function crawl (obj, path, pathFromRoot, parents, dereferencedCache, $refs, opti 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; } @@ -57,7 +81,15 @@ 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) { @@ -66,7 +98,15 @@ function crawl (obj, path, pathFromRoot, parents, dereferencedCache, $refs, opti } 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) { @@ -101,7 +141,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 +195,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; } 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)); }