@speclynx/apidom-reference 4.0.1 → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/package.json +26 -27
  3. package/src/File.cjs +50 -0
  4. package/src/File.mjs +44 -0
  5. package/src/File.ts +63 -0
  6. package/src/Reference.cjs +31 -0
  7. package/src/Reference.mjs +27 -0
  8. package/src/Reference.ts +38 -0
  9. package/src/ReferenceSet.cjs +60 -0
  10. package/src/ReferenceSet.mjs +57 -0
  11. package/src/ReferenceSet.ts +73 -0
  12. package/src/bundle/index.cjs +61 -0
  13. package/src/bundle/index.mjs +55 -0
  14. package/src/bundle/index.ts +57 -0
  15. package/src/bundle/strategies/BundleStrategy.cjs +20 -0
  16. package/src/bundle/strategies/BundleStrategy.mjs +16 -0
  17. package/src/bundle/strategies/BundleStrategy.ts +27 -0
  18. package/src/bundle/strategies/openapi-3-1/index.cjs +35 -0
  19. package/src/bundle/strategies/openapi-3-1/index.mjs +29 -0
  20. package/src/bundle/strategies/openapi-3-1/index.ts +57 -0
  21. package/src/configuration/empty.cjs +9 -0
  22. package/src/configuration/empty.mjs +1 -0
  23. package/src/configuration/empty.ts +1 -0
  24. package/src/configuration/saturated.cjs +88 -0
  25. package/src/configuration/saturated.mjs +80 -0
  26. package/src/configuration/saturated.ts +72 -0
  27. package/src/dereference/index.cjs +90 -0
  28. package/src/dereference/index.mjs +83 -0
  29. package/src/dereference/index.ts +96 -0
  30. package/src/dereference/strategies/DereferenceStrategy.cjs +20 -0
  31. package/src/dereference/strategies/DereferenceStrategy.mjs +16 -0
  32. package/src/dereference/strategies/DereferenceStrategy.ts +27 -0
  33. package/src/dereference/strategies/apidom/index.cjs +89 -0
  34. package/src/dereference/strategies/apidom/index.mjs +83 -0
  35. package/src/dereference/strategies/apidom/index.ts +128 -0
  36. package/src/dereference/strategies/apidom/selectors/element-id.cjs +47 -0
  37. package/src/dereference/strategies/apidom/selectors/element-id.mjs +41 -0
  38. package/src/dereference/strategies/apidom/selectors/element-id.ts +48 -0
  39. package/src/dereference/strategies/apidom/visitor.cjs +266 -0
  40. package/src/dereference/strategies/apidom/visitor.mjs +259 -0
  41. package/src/dereference/strategies/apidom/visitor.ts +316 -0
  42. package/src/dereference/strategies/arazzo-1/index.cjs +109 -0
  43. package/src/dereference/strategies/arazzo-1/index.mjs +100 -0
  44. package/src/dereference/strategies/arazzo-1/index.ts +158 -0
  45. package/src/dereference/strategies/arazzo-1/selectors/$anchor.cjs +12 -0
  46. package/src/dereference/strategies/arazzo-1/selectors/$anchor.mjs +1 -0
  47. package/src/dereference/strategies/arazzo-1/selectors/$anchor.ts +9 -0
  48. package/src/dereference/strategies/arazzo-1/selectors/uri.cjs +8 -0
  49. package/src/dereference/strategies/arazzo-1/selectors/uri.mjs +1 -0
  50. package/src/dereference/strategies/arazzo-1/selectors/uri.ts +5 -0
  51. package/src/dereference/strategies/arazzo-1/source-descriptions.cjs +248 -0
  52. package/src/dereference/strategies/arazzo-1/source-descriptions.mjs +243 -0
  53. package/src/dereference/strategies/arazzo-1/source-descriptions.ts +317 -0
  54. package/src/dereference/strategies/arazzo-1/util.cjs +37 -0
  55. package/src/dereference/strategies/arazzo-1/util.mjs +29 -0
  56. package/src/dereference/strategies/arazzo-1/util.ts +33 -0
  57. package/src/dereference/strategies/arazzo-1/visitor.cjs +507 -0
  58. package/src/dereference/strategies/arazzo-1/visitor.mjs +500 -0
  59. package/src/dereference/strategies/arazzo-1/visitor.ts +574 -0
  60. package/src/dereference/strategies/asyncapi-2/index.cjs +94 -0
  61. package/src/dereference/strategies/asyncapi-2/index.mjs +88 -0
  62. package/src/dereference/strategies/asyncapi-2/index.ts +133 -0
  63. package/src/dereference/strategies/asyncapi-2/visitor.cjs +501 -0
  64. package/src/dereference/strategies/asyncapi-2/visitor.mjs +494 -0
  65. package/src/dereference/strategies/asyncapi-2/visitor.ts +589 -0
  66. package/src/dereference/strategies/openapi-2/index.cjs +96 -0
  67. package/src/dereference/strategies/openapi-2/index.mjs +90 -0
  68. package/src/dereference/strategies/openapi-2/index.ts +136 -0
  69. package/src/dereference/strategies/openapi-2/visitor.cjs +629 -0
  70. package/src/dereference/strategies/openapi-2/visitor.mjs +622 -0
  71. package/src/dereference/strategies/openapi-2/visitor.ts +745 -0
  72. package/src/dereference/strategies/openapi-3-0/index.cjs +96 -0
  73. package/src/dereference/strategies/openapi-3-0/index.mjs +90 -0
  74. package/src/dereference/strategies/openapi-3-0/index.ts +134 -0
  75. package/src/dereference/strategies/openapi-3-0/visitor.cjs +622 -0
  76. package/src/dereference/strategies/openapi-3-0/visitor.mjs +615 -0
  77. package/src/dereference/strategies/openapi-3-0/visitor.ts +760 -0
  78. package/src/dereference/strategies/openapi-3-1/index.cjs +99 -0
  79. package/src/dereference/strategies/openapi-3-1/index.mjs +90 -0
  80. package/src/dereference/strategies/openapi-3-1/index.ts +141 -0
  81. package/src/dereference/strategies/openapi-3-1/selectors/$anchor.cjs +65 -0
  82. package/src/dereference/strategies/openapi-3-1/selectors/$anchor.mjs +54 -0
  83. package/src/dereference/strategies/openapi-3-1/selectors/$anchor.ts +64 -0
  84. package/src/dereference/strategies/openapi-3-1/selectors/uri.cjs +50 -0
  85. package/src/dereference/strategies/openapi-3-1/selectors/uri.mjs +42 -0
  86. package/src/dereference/strategies/openapi-3-1/selectors/uri.ts +54 -0
  87. package/src/dereference/strategies/openapi-3-1/util.cjs +68 -0
  88. package/src/dereference/strategies/openapi-3-1/util.mjs +59 -0
  89. package/src/dereference/strategies/openapi-3-1/util.ts +83 -0
  90. package/src/dereference/strategies/openapi-3-1/visitor.cjs +874 -0
  91. package/src/dereference/strategies/openapi-3-1/visitor.mjs +867 -0
  92. package/src/dereference/strategies/openapi-3-1/visitor.ts +1053 -0
  93. package/src/dereference/util.cjs +31 -0
  94. package/src/dereference/util.mjs +27 -0
  95. package/src/dereference/util.ts +29 -0
  96. package/src/errors/BundleError.cjs +10 -0
  97. package/src/errors/BundleError.mjs +7 -0
  98. package/src/errors/BundleError.ts +8 -0
  99. package/src/errors/DereferenceError.cjs +10 -0
  100. package/src/errors/DereferenceError.mjs +7 -0
  101. package/src/errors/DereferenceError.ts +8 -0
  102. package/src/errors/EvaluationElementIdError.cjs +10 -0
  103. package/src/errors/EvaluationElementIdError.mjs +7 -0
  104. package/src/errors/EvaluationElementIdError.ts +8 -0
  105. package/src/errors/EvaluationJsonSchema$anchorError.cjs +11 -0
  106. package/src/errors/EvaluationJsonSchema$anchorError.mjs +6 -0
  107. package/src/errors/EvaluationJsonSchema$anchorError.ts +8 -0
  108. package/src/errors/EvaluationJsonSchemaUriError.cjs +11 -0
  109. package/src/errors/EvaluationJsonSchemaUriError.mjs +6 -0
  110. package/src/errors/EvaluationJsonSchemaUriError.ts +8 -0
  111. package/src/errors/InvalidJsonSchema$anchorError.cjs +15 -0
  112. package/src/errors/InvalidJsonSchema$anchorError.mjs +10 -0
  113. package/src/errors/InvalidJsonSchema$anchorError.ts +12 -0
  114. package/src/errors/JsonSchema$anchorError.cjs +10 -0
  115. package/src/errors/JsonSchema$anchorError.mjs +7 -0
  116. package/src/errors/JsonSchema$anchorError.ts +8 -0
  117. package/src/errors/JsonSchemaUriError.cjs +10 -0
  118. package/src/errors/JsonSchemaUriError.mjs +7 -0
  119. package/src/errors/JsonSchemaUriError.ts +8 -0
  120. package/src/errors/MaximumBundleDepthError.cjs +11 -0
  121. package/src/errors/MaximumBundleDepthError.mjs +6 -0
  122. package/src/errors/MaximumBundleDepthError.ts +8 -0
  123. package/src/errors/MaximumDereferenceDepthError.cjs +11 -0
  124. package/src/errors/MaximumDereferenceDepthError.mjs +6 -0
  125. package/src/errors/MaximumDereferenceDepthError.ts +8 -0
  126. package/src/errors/MaximumResolveDepthError.cjs +11 -0
  127. package/src/errors/MaximumResolveDepthError.mjs +6 -0
  128. package/src/errors/MaximumResolveDepthError.ts +8 -0
  129. package/src/errors/ParseError.cjs +10 -0
  130. package/src/errors/ParseError.mjs +7 -0
  131. package/src/errors/ParseError.ts +8 -0
  132. package/src/errors/ParserError.cjs +11 -0
  133. package/src/errors/ParserError.mjs +6 -0
  134. package/src/errors/ParserError.ts +8 -0
  135. package/src/errors/PluginError.cjs +18 -0
  136. package/src/errors/PluginError.mjs +15 -0
  137. package/src/errors/PluginError.ts +15 -0
  138. package/src/errors/ResolveError.cjs +10 -0
  139. package/src/errors/ResolveError.mjs +7 -0
  140. package/src/errors/ResolveError.ts +8 -0
  141. package/src/errors/ResolverError.cjs +11 -0
  142. package/src/errors/ResolverError.mjs +6 -0
  143. package/src/errors/ResolverError.ts +8 -0
  144. package/src/errors/UnmatchedBundleStrategyError.cjs +11 -0
  145. package/src/errors/UnmatchedBundleStrategyError.mjs +6 -0
  146. package/src/errors/UnmatchedBundleStrategyError.ts +8 -0
  147. package/src/errors/UnmatchedDereferenceStrategyError.cjs +11 -0
  148. package/src/errors/UnmatchedDereferenceStrategyError.mjs +6 -0
  149. package/src/errors/UnmatchedDereferenceStrategyError.ts +8 -0
  150. package/src/errors/UnmatchedParserError.cjs +11 -0
  151. package/src/errors/UnmatchedParserError.mjs +6 -0
  152. package/src/errors/UnmatchedParserError.ts +8 -0
  153. package/src/errors/UnmatchedResolveStrategyError.cjs +11 -0
  154. package/src/errors/UnmatchedResolveStrategyError.mjs +6 -0
  155. package/src/errors/UnmatchedResolveStrategyError.ts +8 -0
  156. package/src/errors/UnmatchedResolverError.cjs +11 -0
  157. package/src/errors/UnmatchedResolverError.mjs +6 -0
  158. package/src/errors/UnmatchedResolverError.ts +8 -0
  159. package/src/errors/UnresolvableReferenceError.cjs +11 -0
  160. package/src/errors/UnresolvableReferenceError.mjs +6 -0
  161. package/src/errors/UnresolvableReferenceError.ts +8 -0
  162. package/src/index.cjs +146 -0
  163. package/src/index.mjs +103 -0
  164. package/src/index.ts +135 -0
  165. package/src/options/index.cjs +194 -0
  166. package/src/options/index.mjs +191 -0
  167. package/src/options/index.ts +239 -0
  168. package/src/options/util.cjs +24 -0
  169. package/src/options/util.mjs +19 -0
  170. package/src/options/util.ts +22 -0
  171. package/src/parse/index.cjs +69 -0
  172. package/src/parse/index.mjs +63 -0
  173. package/src/parse/index.ts +67 -0
  174. package/src/parse/parsers/Parser.cjs +62 -0
  175. package/src/parse/parsers/Parser.mjs +58 -0
  176. package/src/parse/parsers/Parser.ts +80 -0
  177. package/src/parse/parsers/apidom-json/index.cjs +70 -0
  178. package/src/parse/parsers/apidom-json/index.mjs +64 -0
  179. package/src/parse/parsers/apidom-json/index.ts +78 -0
  180. package/src/parse/parsers/arazzo-json-1/index.cjs +62 -0
  181. package/src/parse/parsers/arazzo-json-1/index.mjs +56 -0
  182. package/src/parse/parsers/arazzo-json-1/index.ts +76 -0
  183. package/src/parse/parsers/arazzo-json-1/source-descriptions.cjs +221 -0
  184. package/src/parse/parsers/arazzo-json-1/source-descriptions.mjs +214 -0
  185. package/src/parse/parsers/arazzo-json-1/source-descriptions.ts +280 -0
  186. package/src/parse/parsers/arazzo-yaml-1/index.cjs +62 -0
  187. package/src/parse/parsers/arazzo-yaml-1/index.mjs +56 -0
  188. package/src/parse/parsers/arazzo-yaml-1/index.ts +77 -0
  189. package/src/parse/parsers/arazzo-yaml-1/source-descriptions.cjs +12 -0
  190. package/src/parse/parsers/arazzo-yaml-1/source-descriptions.mjs +7 -0
  191. package/src/parse/parsers/arazzo-yaml-1/source-descriptions.ts +16 -0
  192. package/src/parse/parsers/asyncapi-json-2/index.cjs +54 -0
  193. package/src/parse/parsers/asyncapi-json-2/index.mjs +48 -0
  194. package/src/parse/parsers/asyncapi-json-2/index.ts +58 -0
  195. package/src/parse/parsers/asyncapi-yaml-2/index.cjs +54 -0
  196. package/src/parse/parsers/asyncapi-yaml-2/index.mjs +48 -0
  197. package/src/parse/parsers/asyncapi-yaml-2/index.ts +58 -0
  198. package/src/parse/parsers/binary/index-browser.cjs +56 -0
  199. package/src/parse/parsers/binary/index-browser.mjs +50 -0
  200. package/src/parse/parsers/binary/index-browser.ts +60 -0
  201. package/src/parse/parsers/binary/index-node.cjs +51 -0
  202. package/src/parse/parsers/binary/index-node.mjs +45 -0
  203. package/src/parse/parsers/binary/index-node.ts +57 -0
  204. package/src/parse/parsers/json/index.cjs +53 -0
  205. package/src/parse/parsers/json/index.mjs +47 -0
  206. package/src/parse/parsers/json/index.ts +52 -0
  207. package/src/parse/parsers/openapi-json-2/index.cjs +54 -0
  208. package/src/parse/parsers/openapi-json-2/index.mjs +48 -0
  209. package/src/parse/parsers/openapi-json-2/index.ts +58 -0
  210. package/src/parse/parsers/openapi-json-3-0/index.cjs +54 -0
  211. package/src/parse/parsers/openapi-json-3-0/index.mjs +48 -0
  212. package/src/parse/parsers/openapi-json-3-0/index.ts +59 -0
  213. package/src/parse/parsers/openapi-json-3-1/index.cjs +54 -0
  214. package/src/parse/parsers/openapi-json-3-1/index.mjs +48 -0
  215. package/src/parse/parsers/openapi-json-3-1/index.ts +59 -0
  216. package/src/parse/parsers/openapi-yaml-2/index.cjs +54 -0
  217. package/src/parse/parsers/openapi-yaml-2/index.mjs +48 -0
  218. package/src/parse/parsers/openapi-yaml-2/index.ts +58 -0
  219. package/src/parse/parsers/openapi-yaml-3-0/index.cjs +54 -0
  220. package/src/parse/parsers/openapi-yaml-3-0/index.mjs +48 -0
  221. package/src/parse/parsers/openapi-yaml-3-0/index.ts +59 -0
  222. package/src/parse/parsers/openapi-yaml-3-1/index.cjs +54 -0
  223. package/src/parse/parsers/openapi-yaml-3-1/index.mjs +48 -0
  224. package/src/parse/parsers/openapi-yaml-3-1/index.ts +59 -0
  225. package/src/parse/parsers/yaml-1-2/index.cjs +56 -0
  226. package/src/parse/parsers/yaml-1-2/index.mjs +50 -0
  227. package/src/parse/parsers/yaml-1-2/index.ts +60 -0
  228. package/src/resolve/index.cjs +67 -0
  229. package/src/resolve/index.mjs +60 -0
  230. package/src/resolve/index.ts +75 -0
  231. package/src/resolve/resolvers/HTTPResolver.cjs +45 -0
  232. package/src/resolve/resolvers/HTTPResolver.mjs +37 -0
  233. package/src/resolve/resolvers/HTTPResolver.ts +58 -0
  234. package/src/resolve/resolvers/Resolver.cjs +20 -0
  235. package/src/resolve/resolvers/Resolver.mjs +16 -0
  236. package/src/resolve/resolvers/Resolver.ts +25 -0
  237. package/src/resolve/resolvers/file/index-browser.cjs +24 -0
  238. package/src/resolve/resolvers/file/index-browser.mjs +19 -0
  239. package/src/resolve/resolvers/file/index-browser.ts +24 -0
  240. package/src/resolve/resolvers/file/index-node.cjs +49 -0
  241. package/src/resolve/resolvers/file/index-node.mjs +42 -0
  242. package/src/resolve/resolvers/file/index-node.ts +55 -0
  243. package/src/resolve/resolvers/http-axios/cache/MemoryCache.cjs +41 -0
  244. package/src/resolve/resolvers/http-axios/cache/MemoryCache.mjs +37 -0
  245. package/src/resolve/resolvers/http-axios/cache/MemoryCache.ts +46 -0
  246. package/src/resolve/resolvers/http-axios/index.cjs +113 -0
  247. package/src/resolve/resolvers/http-axios/index.mjs +105 -0
  248. package/src/resolve/resolvers/http-axios/index.ts +130 -0
  249. package/src/resolve/strategies/ResolveStrategy.cjs +20 -0
  250. package/src/resolve/strategies/ResolveStrategy.mjs +16 -0
  251. package/src/resolve/strategies/ResolveStrategy.ts +26 -0
  252. package/src/resolve/strategies/apidom/index.cjs +49 -0
  253. package/src/resolve/strategies/apidom/index.mjs +43 -0
  254. package/src/resolve/strategies/apidom/index.ts +78 -0
  255. package/src/resolve/strategies/asyncapi-2/index.cjs +49 -0
  256. package/src/resolve/strategies/asyncapi-2/index.mjs +43 -0
  257. package/src/resolve/strategies/asyncapi-2/index.ts +78 -0
  258. package/src/resolve/strategies/openapi-2/index.cjs +49 -0
  259. package/src/resolve/strategies/openapi-2/index.mjs +43 -0
  260. package/src/resolve/strategies/openapi-2/index.ts +78 -0
  261. package/src/resolve/strategies/openapi-3-0/index.cjs +49 -0
  262. package/src/resolve/strategies/openapi-3-0/index.mjs +43 -0
  263. package/src/resolve/strategies/openapi-3-0/index.ts +78 -0
  264. package/src/resolve/strategies/openapi-3-1/index.cjs +49 -0
  265. package/src/resolve/strategies/openapi-3-1/index.mjs +43 -0
  266. package/src/resolve/strategies/openapi-3-1/index.ts +78 -0
  267. package/src/resolve/util.cjs +37 -0
  268. package/src/resolve/util.mjs +30 -0
  269. package/src/resolve/util.ts +39 -0
  270. package/src/util/plugins.cjs +39 -0
  271. package/src/util/plugins.mjs +34 -0
  272. package/src/util/plugins.ts +37 -0
  273. package/src/util/url.cjs +288 -0
  274. package/src/util/url.mjs +274 -0
  275. package/src/util/url.ts +285 -0
@@ -0,0 +1,500 @@
1
+ import { propEq, none } from 'ramda';
2
+ import { isElement, isStringElement, RefElement, cloneShallow, cloneDeep } from '@speclynx/apidom-datamodel';
3
+ import { toValue, toYAML } from '@speclynx/apidom-core';
4
+ import { ApiDOMStructuredError } from '@speclynx/apidom-error';
5
+ import { traverse, traverseAsync } from '@speclynx/apidom-traverse';
6
+ import { evaluate as jsonPointerEvaluate, compile as jsonPointerCompile, URIFragmentIdentifier } from '@speclynx/apidom-json-pointer';
7
+ import { isParameterElement, isJSONSchemaElement, isBooleanJSONSchemaElement } from '@speclynx/apidom-ns-arazzo-1';
8
+ import { parse as parseRuntimeExpression } from '@swaggerexpert/arazzo-runtime-expression';
9
+ import { isAnchor, uriToAnchor, evaluate as $anchorEvaluate } from "./selectors/$anchor.mjs";
10
+ import { evaluate as uriEvaluate } from "./selectors/uri.mjs";
11
+ import { resolveSchema$refField } from "../openapi-3-1/util.mjs";
12
+ import { maybeRefractToJSONSchemaElement } from "./util.mjs";
13
+ import UnresolvableReferenceError from "../../../errors/UnresolvableReferenceError.mjs";
14
+ import MaximumDereferenceDepthError from "../../../errors/MaximumDereferenceDepthError.mjs";
15
+ import MaximumResolveDepthError from "../../../errors/MaximumResolveDepthError.mjs";
16
+ import * as url from "../../../util/url.mjs";
17
+ import parse from "../../../parse/index.mjs";
18
+ import Reference from "../../../Reference.mjs";
19
+ import File from "../../../File.mjs";
20
+ import { AncestorLineage } from "../../util.mjs";
21
+ import EvaluationJsonSchemaUriError from "../../../errors/EvaluationJsonSchemaUriError.mjs";
22
+ /**
23
+ * @public
24
+ */
25
+ /**
26
+ * @public
27
+ */
28
+ class Arazzo1DereferenceVisitor {
29
+ indirections;
30
+ reference;
31
+ options;
32
+
33
+ /**
34
+ * Tracks element ancestors across dive-deep traversal boundaries.
35
+ * Used for cycle detection: if a referenced element is found in
36
+ * the ancestor lineage, a circular reference is detected.
37
+ */
38
+ ancestors;
39
+ constructor({
40
+ reference,
41
+ options,
42
+ indirections = [],
43
+ ancestors = new AncestorLineage()
44
+ }) {
45
+ this.indirections = indirections;
46
+ this.reference = reference;
47
+ this.options = options;
48
+ this.ancestors = new AncestorLineage(...ancestors);
49
+ }
50
+ toAncestorLineage(path) {
51
+ const ancestorNodes = path.getAncestorNodes();
52
+ const directAncestors = new Set(ancestorNodes.filter(isElement));
53
+ const ancestorsLineage = new AncestorLineage(...this.ancestors, directAncestors);
54
+ return [ancestorsLineage, directAncestors];
55
+ }
56
+ toBaseURI(uri) {
57
+ return url.resolve(this.reference.uri, url.sanitize(url.stripHash(uri)));
58
+ }
59
+ async toReference(uri) {
60
+ // detect maximum depth of resolution
61
+ if (this.reference.depth >= this.options.resolve.maxDepth) {
62
+ throw new MaximumResolveDepthError(`Maximum resolution depth of ${this.options.resolve.maxDepth} has been exceeded by file "${this.reference.uri}"`, {
63
+ maxDepth: this.options.resolve.maxDepth,
64
+ uri: this.reference.uri
65
+ });
66
+ }
67
+ const baseURI = this.toBaseURI(uri);
68
+ const {
69
+ refSet
70
+ } = this.reference;
71
+
72
+ // we've already processed this Reference in past
73
+ if (refSet.has(baseURI)) {
74
+ return refSet.find(propEq(baseURI, 'uri'));
75
+ }
76
+ const parseResult = await parse(url.unsanitize(baseURI), {
77
+ ...this.options,
78
+ parse: {
79
+ ...this.options.parse,
80
+ mediaType: 'text/plain'
81
+ }
82
+ });
83
+
84
+ // register new mutable reference with a refSet
85
+ //
86
+ // NOTE(known limitation): the mutable reference is mutated in place during traversal
87
+ // (via `{ mutable: true }`). When an external document evaluates a JSON pointer back
88
+ // into this document, it may receive an already-resolved element instead of the original
89
+ // $ref. That resolved element was produced using the entry document's resolution context
90
+ // (ancestors, indirections), which may differ from the external document's context.
91
+ // This can affect cycle detection in rare cross-document circular reference patterns.
92
+ //
93
+ // Remediation: evaluate JSON pointers against the immutable (original) parse tree
94
+ // instead of the mutable working copy. The `immutable://` reference below preserves
95
+ // the original tree and could be used for pointer evaluation, ensuring every resolution
96
+ // context always sees raw, unresolved elements and processes them with its own
97
+ // ancestors/indirections. The trade-off is that elements referenced by multiple
98
+ // documents would be resolved once per context instead of being reused.
99
+ const mutableReference = new Reference({
100
+ uri: baseURI,
101
+ value: this.options.dereference.immutable ? cloneDeep(parseResult) : parseResult,
102
+ depth: this.reference.depth + 1
103
+ });
104
+ refSet.add(mutableReference);
105
+ if (this.options.dereference.immutable) {
106
+ // register new immutable reference with a refSet
107
+ const immutableReference = new Reference({
108
+ uri: `immutable://${baseURI}`,
109
+ value: parseResult,
110
+ depth: this.reference.depth + 1
111
+ });
112
+ refSet.add(immutableReference);
113
+ }
114
+ return mutableReference;
115
+ }
116
+
117
+ /**
118
+ * Handles an error according to the continueOnError option.
119
+ *
120
+ * For new errors: wraps in UnresolvableReferenceError with structured context
121
+ * (type, uri, location, codeFrame, refFieldName, refFieldValue, trace).
122
+ * For errors already wrapped by a nested visitor: prepends the current hop to the trace.
123
+ *
124
+ * Inner/intermediate visitors always throw to let the trace accumulate.
125
+ * Only the entry document visitor respects continueOnError (callback/swallow/throw).
126
+ */
127
+ handleError(message, error, referencingElement, refFieldName, refFieldValue, visitorPath) {
128
+ const {
129
+ continueOnError
130
+ } = this.options.dereference;
131
+ const isEntryDocument = url.stripHash(this.reference.refSet?.rootRef?.uri ?? '') === this.reference.uri;
132
+ const uri = this.reference.uri;
133
+ const type = referencingElement.element;
134
+ const codeFrame = toYAML(referencingElement);
135
+
136
+ // find element location by identity in the document tree.
137
+ // guarded: this.reference.value may not be a ParseResultElement or may lack a result.
138
+ // falls back to visitorPath which may produce an incomplete path when
139
+ // dereferenceApiDOM is called with a fragment (cloneShallow creates a new root identity).
140
+ let location;
141
+ const root = this.reference.value.result;
142
+ if (isElement(root)) {
143
+ traverse(root, {
144
+ enter(p) {
145
+ if (p.node === referencingElement) {
146
+ location = p.formatPath();
147
+ p.stop();
148
+ }
149
+ }
150
+ });
151
+ }
152
+ location ??= visitorPath.formatPath();
153
+ const hop = {
154
+ uri,
155
+ type,
156
+ refFieldName,
157
+ refFieldValue,
158
+ location,
159
+ codeFrame
160
+ };
161
+
162
+ // enrich existing error from nested visitor or create new one
163
+ let unresolvedError;
164
+ if (error instanceof UnresolvableReferenceError) {
165
+ // prefix relative locations for entries belonging to the referenced document
166
+ const refBaseURI = this.toBaseURI(refFieldValue);
167
+ const fragment = URIFragmentIdentifier.fromURIReference(refFieldValue);
168
+ if (fragment) {
169
+ if (refBaseURI === error.uri && error.location) {
170
+ error.location = fragment + error.location;
171
+ }
172
+ for (const h of error.trace) {
173
+ if (h.uri === refBaseURI && h.location) h.location = fragment + h.location;
174
+ }
175
+ }
176
+ // @ts-ignore
177
+ error.trace = [hop, ...error.trace];
178
+ unresolvedError = error;
179
+ } else {
180
+ unresolvedError = new UnresolvableReferenceError(message, {
181
+ cause: error,
182
+ type,
183
+ uri,
184
+ location,
185
+ codeFrame,
186
+ refFieldName,
187
+ refFieldValue,
188
+ trace: []
189
+ });
190
+ }
191
+ if (!isEntryDocument || continueOnError === false) throw unresolvedError;
192
+ if (typeof continueOnError === 'function') continueOnError(unresolvedError);
193
+ }
194
+ ReusableElement(path) {
195
+ const referencingElement = path.node;
196
+
197
+ // skip current Reusable Object if reference field is not defined as a string
198
+ if (!isStringElement(referencingElement.reference)) {
199
+ path.skip();
200
+ return;
201
+ }
202
+
203
+ // ignore if resolve.internal is false (Reusable Objects are internal references only)
204
+ if (!this.options.resolve.internal) {
205
+ path.skip();
206
+ return;
207
+ }
208
+ const runtimeExpression = toValue(referencingElement.reference);
209
+
210
+ // parse the runtime expression
211
+ const {
212
+ result,
213
+ tree
214
+ } = parseRuntimeExpression(runtimeExpression);
215
+ if (!result.success) {
216
+ throw new ApiDOMStructuredError(`Invalid Reusable Object reference format: "${runtimeExpression}"`, {
217
+ runtimeExpression
218
+ });
219
+ }
220
+
221
+ // ReusableElement can only reference components
222
+ if (tree.type !== 'ComponentsExpression') {
223
+ throw new ApiDOMStructuredError(`Reusable Object reference "${runtimeExpression}" must be a components expression`, {
224
+ runtimeExpression
225
+ });
226
+ }
227
+
228
+ // evaluate runtime expression as JSON Pointer to get the referenced element
229
+ const jsonPointer = jsonPointerCompile(['components', tree.field, tree.subField]);
230
+ let referencedElement;
231
+ try {
232
+ referencedElement = jsonPointerEvaluate(this.reference.value.result, jsonPointer);
233
+ } catch {
234
+ throw new ApiDOMStructuredError(`Reusable Object reference "${runtimeExpression}" cannot be resolved`, {
235
+ runtimeExpression
236
+ });
237
+ }
238
+
239
+ /**
240
+ * Create a shallow clone of the referenced element to avoid modifying the original.
241
+ */
242
+ const mergedElement = cloneShallow(referencedElement);
243
+ // annotate with info about original referencing element
244
+ mergedElement.meta.set('ref-fields', {
245
+ reference: runtimeExpression,
246
+ value: toValue(referencingElement.value)
247
+ });
248
+ // annotate with info about origin
249
+ mergedElement.meta.set('ref-origin', this.reference.uri);
250
+ mergedElement.meta.set('ref-type', referencingElement.element);
251
+
252
+ // override value field if present for Parameter Objects
253
+ if (isParameterElement(mergedElement) && referencingElement.hasKey('value')) {
254
+ mergedElement.remove('value');
255
+ mergedElement.set('value', referencingElement.get('value'));
256
+ }
257
+
258
+ /**
259
+ * Transclude referencing element with merged referenced element.
260
+ */
261
+ path.replaceWith(mergedElement);
262
+ }
263
+ async JSONSchemaElement(path) {
264
+ const referencingElement = path.node;
265
+
266
+ // skip current referencing schema as $ref keyword was not defined
267
+ if (!isStringElement(referencingElement.$ref)) {
268
+ return;
269
+ }
270
+
271
+ // skip current referencing element as it's already been accessed
272
+ if (this.indirections.includes(referencingElement)) {
273
+ path.skip();
274
+ return;
275
+ }
276
+ const indirectionsSize = this.indirections.length;
277
+ try {
278
+ // compute baseURI using rules around $id and $ref keywords
279
+ let reference = await this.toReference(url.unsanitize(this.reference.uri));
280
+ let {
281
+ uri: retrievalURI
282
+ } = reference;
283
+ const $refBaseURI = resolveSchema$refField(retrievalURI, referencingElement);
284
+ const $refBaseURIStrippedHash = url.stripHash($refBaseURI);
285
+ const file = new File({
286
+ uri: $refBaseURIStrippedHash
287
+ });
288
+ const isUnknownURI = none(r => r.canRead(file), this.options.resolve.resolvers);
289
+ const isURL = !isUnknownURI;
290
+ let isInternalReference = url.stripHash(this.reference.uri) === $refBaseURI;
291
+ let isExternalReference = !isInternalReference;
292
+
293
+ // determining reference, proper evaluation and selection mechanism
294
+ let referencedElement;
295
+ try {
296
+ if (isUnknownURI || isURL) {
297
+ // we're dealing with canonical URI or URL with possible fragment
298
+ retrievalURI = this.toBaseURI($refBaseURI);
299
+ const selector = $refBaseURI;
300
+ const referenceAsSchema = maybeRefractToJSONSchemaElement(reference.value.result);
301
+ referencedElement = uriEvaluate(selector, referenceAsSchema);
302
+ referencedElement = maybeRefractToJSONSchemaElement(referencedElement);
303
+
304
+ // ignore resolving internal Schema Objects
305
+ if (!this.options.resolve.internal && isInternalReference) {
306
+ // skip traversing this schema element but traverse all its child elements
307
+ return;
308
+ }
309
+ // ignore resolving external Schema Objects
310
+ if (!this.options.resolve.external && isExternalReference) {
311
+ // skip traversing this schema element but traverse all its child elements
312
+ return;
313
+ }
314
+ } else {
315
+ // we're assuming here that we're dealing with JSON Pointer here
316
+ retrievalURI = this.toBaseURI($refBaseURI);
317
+ isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
318
+ isExternalReference = !isInternalReference;
319
+
320
+ // ignore resolving internal Schema Objects
321
+ if (!this.options.resolve.internal && isInternalReference) {
322
+ // skip traversing this schema element but traverse all its child elements
323
+ return;
324
+ }
325
+ // ignore resolving external Schema Objects
326
+ if (!this.options.resolve.external && isExternalReference) {
327
+ // skip traversing this schema element but traverse all its child elements
328
+ return;
329
+ }
330
+ reference = await this.toReference(url.unsanitize($refBaseURI));
331
+ const selector = URIFragmentIdentifier.fromURIReference($refBaseURI);
332
+ const referenceAsSchema = maybeRefractToJSONSchemaElement(reference.value.result);
333
+ referencedElement = jsonPointerEvaluate(referenceAsSchema, selector);
334
+ referencedElement = maybeRefractToJSONSchemaElement(referencedElement);
335
+ }
336
+ } catch (error) {
337
+ /**
338
+ * JSONSchemaElement($id=URL) was not found, so we're going to try to resolve
339
+ * the URL and assume the returned response is a JSON Schema.
340
+ */
341
+ if (isURL && error instanceof EvaluationJsonSchemaUriError) {
342
+ if (isAnchor(uriToAnchor($refBaseURI))) {
343
+ // we're dealing with JSON Schema $anchor here
344
+ isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
345
+ isExternalReference = !isInternalReference;
346
+
347
+ // ignore resolving internal Schema Objects
348
+ if (!this.options.resolve.internal && isInternalReference) {
349
+ // skip traversing this schema element but traverse all its child elements
350
+ return;
351
+ }
352
+ // ignore resolving external Schema Objects
353
+ if (!this.options.resolve.external && isExternalReference) {
354
+ // skip traversing this schema element but traverse all its child elements
355
+ return;
356
+ }
357
+ reference = await this.toReference(url.unsanitize($refBaseURI));
358
+ const selector = uriToAnchor($refBaseURI);
359
+ const referenceAsSchema = maybeRefractToJSONSchemaElement(reference.value.result);
360
+ referencedElement = $anchorEvaluate(selector, referenceAsSchema);
361
+ referencedElement = maybeRefractToJSONSchemaElement(referencedElement);
362
+ } else {
363
+ // we're assuming here that we're dealing with JSON Pointer here
364
+ retrievalURI = this.toBaseURI($refBaseURI);
365
+ isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
366
+ isExternalReference = !isInternalReference;
367
+
368
+ // ignore resolving internal Schema Objects
369
+ if (!this.options.resolve.internal && isInternalReference) {
370
+ // skip traversing this schema element but traverse all its child elements
371
+ return;
372
+ }
373
+ // ignore resolving external Schema Objects
374
+ if (!this.options.resolve.external && isExternalReference) {
375
+ // skip traversing this schema element but traverse all its child elements
376
+ return;
377
+ }
378
+ reference = await this.toReference(url.unsanitize($refBaseURI));
379
+ const selector = URIFragmentIdentifier.fromURIReference($refBaseURI);
380
+ const referenceAsSchema = maybeRefractToJSONSchemaElement(reference.value.result);
381
+ referencedElement = jsonPointerEvaluate(referenceAsSchema, selector);
382
+ referencedElement = maybeRefractToJSONSchemaElement(referencedElement);
383
+ }
384
+ } else {
385
+ throw error;
386
+ }
387
+ }
388
+ this.indirections.push(referencingElement);
389
+
390
+ // detect direct or indirect reference
391
+ if (referencingElement === referencedElement) {
392
+ throw new ApiDOMStructuredError('Recursive JSON Schema reference detected', {
393
+ $ref: toValue(referencingElement.$ref)
394
+ });
395
+ }
396
+
397
+ // detect maximum depth of dereferencing
398
+ if (this.indirections.length > this.options.dereference.maxDepth) {
399
+ throw new MaximumDereferenceDepthError(`Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`, {
400
+ maxDepth: this.options.dereference.maxDepth,
401
+ uri: this.reference.uri
402
+ });
403
+ }
404
+
405
+ // detect cross-boundary cycle
406
+ const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
407
+ if (ancestorsLineage.includes(referencedElement)) {
408
+ reference.refSet.circular = true;
409
+ if (this.options.dereference.circular === 'error') {
410
+ throw new ApiDOMStructuredError('Circular reference detected', {
411
+ $ref: toValue(referencingElement.$ref)
412
+ });
413
+ } else if (this.options.dereference.circular === 'replace') {
414
+ const refElement = new RefElement($refBaseURI, {
415
+ type: referencingElement.element,
416
+ uri: reference.uri,
417
+ $ref: toValue(referencingElement.$ref)
418
+ });
419
+ const replacer = this.options.dereference.strategyOpts['arazzo-1']?.circularReplacer ?? this.options.dereference.circularReplacer;
420
+ const replacement = replacer(refElement);
421
+ path.replaceWith(replacement);
422
+ return;
423
+ }
424
+ }
425
+
426
+ /**
427
+ * Dive deep into the fragment.
428
+ *
429
+ * Cases to consider:
430
+ * 1. We're crossing document boundary
431
+ * 2. Fragment is from non-entry document
432
+ * 3. Fragment is a JSON Schema with $ref field. We need to follow it to get the eventual value
433
+ * 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
434
+ */
435
+ const isNonEntryDocument = url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
436
+ const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
437
+ if ((isExternalReference || isNonEntryDocument || isJSONSchemaElement(referencedElement) && isStringElement(referencedElement.$ref) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
438
+ // append referencing reference to ancestors lineage
439
+ directAncestors.add(referencingElement);
440
+ const visitor = new Arazzo1DereferenceVisitor({
441
+ reference,
442
+ indirections: [...this.indirections],
443
+ options: this.options,
444
+ ancestors: ancestorsLineage
445
+ });
446
+ referencedElement = await traverseAsync(referencedElement, visitor, {
447
+ mutable: true
448
+ });
449
+
450
+ // remove referencing reference from ancestors lineage
451
+ directAncestors.delete(referencingElement);
452
+ }
453
+
454
+ // Boolean JSON Schemas
455
+ if (isBooleanJSONSchemaElement(referencedElement)) {
456
+ const booleanJsonSchemaElement = cloneDeep(referencedElement);
457
+ // annotate referenced element with info about original referencing element
458
+ booleanJsonSchemaElement.meta.set('ref-fields', {
459
+ $ref: toValue(referencingElement.$ref)
460
+ });
461
+ // annotate referenced element with info about origin
462
+ booleanJsonSchemaElement.meta.set('ref-origin', reference.uri);
463
+ booleanJsonSchemaElement.meta.set('ref-type', referencingElement.element);
464
+ path.replaceWith(booleanJsonSchemaElement);
465
+ return;
466
+ }
467
+
468
+ /**
469
+ * Creating a new version of JSON Schema by merging fields from referenced Schema with referencing one.
470
+ */
471
+ if (isJSONSchemaElement(referencedElement)) {
472
+ const mergedElement = cloneShallow(referencedElement);
473
+ // existing keywords from referencing schema overrides ones from referenced schema
474
+ referencingElement.forEach((value, keyElement, item) => {
475
+ mergedElement.remove(toValue(keyElement));
476
+ mergedElement.content.push(item);
477
+ });
478
+ mergedElement.remove('$ref');
479
+ // annotate referenced element with info about original referencing element
480
+ mergedElement.meta.set('ref-fields', {
481
+ $ref: toValue(referencingElement.$ref)
482
+ });
483
+ // annotate fragment with info about origin
484
+ mergedElement.meta.set('ref-origin', reference.uri);
485
+ mergedElement.meta.set('ref-type', referencingElement.element);
486
+ referencedElement = mergedElement;
487
+ }
488
+ /**
489
+ * Transclude referencing element with merged referenced element.
490
+ */
491
+ path.replaceWith(referencedElement);
492
+ } catch (error) {
493
+ const $ref = toValue(referencingElement.$ref);
494
+ this.handleError(`Error while dereferencing Schema Object. Cannot resolve $ref "${$ref}": ${error.message}`, error, referencingElement, '$ref', $ref, path);
495
+ } finally {
496
+ if (this.indirections.length > indirectionsSize) this.indirections.pop();
497
+ }
498
+ }
499
+ }
500
+ export default Arazzo1DereferenceVisitor;