dereference.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. "use strict";
  2. const $Ref = require("./ref");
  3. const Pointer = require("./pointer");
  4. const { ono } = require("@jsdevtools/ono");
  5. const url = require("./util/url");
  6. module.exports = dereference;
  7. /**
  8. * Crawls the JSON schema, finds all JSON references, and dereferences them.
  9. * This method mutates the JSON schema object, replacing JSON references with their resolved value.
  10. *
  11. * @param {$RefParser} parser
  12. * @param {$RefParserOptions} options
  13. */
  14. function dereference (parser, options) {
  15. // console.log('Dereferencing $ref pointers in %s', parser.$refs._root$Ref.path);
  16. let dereferenced = crawl(parser.schema, parser.$refs._root$Ref.path, "#", new Set(), new Set(), new Map(), parser.$refs, options);
  17. parser.$refs.circular = dereferenced.circular;
  18. parser.schema = dereferenced.value;
  19. }
  20. /**
  21. * Recursively crawls the given value, and dereferences any JSON references.
  22. *
  23. * @param {*} obj - The value to crawl. If it's not an object or array, it will be ignored.
  24. * @param {string} path - The full path of `obj`, possibly with a JSON Pointer in the hash
  25. * @param {string} pathFromRoot - The path of `obj` from the schema root
  26. * @param {Set<object>} parents - An array of the parent objects that have already been dereferenced
  27. * @param {Set<object>} processedObjects - An array of all the objects that have already been processed
  28. * @param {Map<string,object>} dereferencedCache - An map of all the dereferenced objects
  29. * @param {$Refs} $refs
  30. * @param {$RefParserOptions} options
  31. * @returns {{value: object, circular: boolean}}
  32. */
  33. function crawl (obj, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options) {
  34. let dereferenced;
  35. let result = {
  36. value: obj,
  37. circular: false
  38. };
  39. let isExcludedPath = options.dereference.excludedPathMatcher;
  40. if (options.dereference.circular === "ignore" || !processedObjects.has(obj)) {
  41. if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !isExcludedPath(pathFromRoot)) {
  42. parents.add(obj);
  43. processedObjects.add(obj);
  44. if ($Ref.isAllowed$Ref(obj, options)) {
  45. dereferenced = dereference$Ref(obj, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options);
  46. result.circular = dereferenced.circular;
  47. result.value = dereferenced.value;
  48. }
  49. else {
  50. for (const key of Object.keys(obj)) {
  51. let keyPath = Pointer.join(path, key);
  52. let keyPathFromRoot = Pointer.join(pathFromRoot, key);
  53. if (isExcludedPath(keyPathFromRoot)) {
  54. continue;
  55. }
  56. let value = obj[key];
  57. let circular = false;
  58. if ($Ref.isAllowed$Ref(value, options)) {
  59. dereferenced = dereference$Ref(value, keyPath, keyPathFromRoot, parents, processedObjects, dereferencedCache, $refs, options);
  60. circular = dereferenced.circular;
  61. // Avoid pointless mutations; breaks frozen objects to no profit
  62. if (obj[key] !== dereferenced.value) {
  63. obj[key] = dereferenced.value;
  64. }
  65. }
  66. else {
  67. if (!parents.has(value)) {
  68. dereferenced = crawl(value, keyPath, keyPathFromRoot, parents, processedObjects, dereferencedCache, $refs, options);
  69. circular = dereferenced.circular;
  70. // Avoid pointless mutations; breaks frozen objects to no profit
  71. if (obj[key] !== dereferenced.value) {
  72. obj[key] = dereferenced.value;
  73. }
  74. }
  75. else {
  76. circular = foundCircularReference(keyPath, $refs, options);
  77. }
  78. }
  79. // Set the "isCircular" flag if this or any other property is circular
  80. result.circular = result.circular || circular;
  81. }
  82. }
  83. parents.delete(obj);
  84. }
  85. }
  86. return result;
  87. }
  88. /**
  89. * Dereferences the given JSON Reference, and then crawls the resulting value.
  90. *
  91. * @param {{$ref: string}} $ref - The JSON Reference to resolve
  92. * @param {string} path - The full path of `$ref`, possibly with a JSON Pointer in the hash
  93. * @param {string} pathFromRoot - The path of `$ref` from the schema root
  94. * @param {Set<object>} parents - An array of the parent objects that have already been dereferenced
  95. * @param {Set<object>} processedObjects - An array of all the objects that have already been dereferenced
  96. * @param {Map<string,object>} dereferencedCache - An map of all the dereferenced objects
  97. * @param {$Refs} $refs
  98. * @param {$RefParserOptions} options
  99. * @returns {{value: object, circular: boolean}}
  100. */
  101. function dereference$Ref ($ref, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options) {
  102. // console.log('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path);
  103. let $refPath = url.resolve(path, $ref.$ref);
  104. const cache = dereferencedCache.get($refPath);
  105. if (cache) {
  106. const refKeys = Object.keys($ref);
  107. if (refKeys.length > 1) {
  108. const extraKeys = {};
  109. for (let key of refKeys) {
  110. if (key !== "$ref" && !(key in cache.value)) {
  111. extraKeys[key] = $ref[key];
  112. }
  113. }
  114. return {
  115. circular: cache.circular,
  116. value: Object.assign({}, cache.value, extraKeys),
  117. };
  118. }
  119. return cache;
  120. }
  121. let pointer = $refs._resolve($refPath, path, options);
  122. if (pointer === null) {
  123. return {
  124. circular: false,
  125. value: null,
  126. };
  127. }
  128. // Check for circular references
  129. let directCircular = pointer.circular;
  130. let circular = directCircular || parents.has(pointer.value);
  131. circular && foundCircularReference(path, $refs, options);
  132. // Dereference the JSON reference
  133. let dereferencedValue = $Ref.dereference($ref, pointer.value);
  134. // Crawl the dereferenced value (unless it's circular)
  135. if (!circular) {
  136. // Determine if the dereferenced value is circular
  137. let dereferenced = crawl(dereferencedValue, pointer.path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options);
  138. circular = dereferenced.circular;
  139. dereferencedValue = dereferenced.value;
  140. }
  141. if (circular && !directCircular && options.dereference.circular === "ignore") {
  142. // The user has chosen to "ignore" circular references, so don't change the value
  143. dereferencedValue = $ref;
  144. }
  145. if (directCircular) {
  146. // The pointer is a DIRECT circular reference (i.e. it references itself).
  147. // So replace the $ref path with the absolute path from the JSON Schema root
  148. dereferencedValue.$ref = pathFromRoot;
  149. }
  150. const dereferencedObject = {
  151. circular,
  152. value: dereferencedValue
  153. };
  154. // only cache if no extra properties than $ref
  155. if (Object.keys($ref).length === 1) {
  156. dereferencedCache.set($refPath, dereferencedObject);
  157. }
  158. return dereferencedObject;
  159. }
  160. /**
  161. * Called when a circular reference is found.
  162. * It sets the {@link $Refs#circular} flag, and throws an error if options.dereference.circular is false.
  163. *
  164. * @param {string} keyPath - The JSON Reference path of the circular reference
  165. * @param {$Refs} $refs
  166. * @param {$RefParserOptions} options
  167. * @returns {boolean} - always returns true, to indicate that a circular reference was found
  168. */
  169. function foundCircularReference (keyPath, $refs, options) {
  170. $refs.circular = true;
  171. if (!options.dereference.circular) {
  172. throw ono.reference(`Circular $ref pointer found at ${keyPath}`);
  173. }
  174. return true;
  175. }