@speclynx/apidom-reference 4.0.2 → 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 +6 -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,760 @@
1
+ import { propEq } from 'ramda';
2
+ import { isUndefined } from 'ramda-adjunct';
3
+ import {
4
+ isStringElement,
5
+ isElement,
6
+ Element,
7
+ RefElement,
8
+ ParseResultElement,
9
+ cloneShallow,
10
+ cloneDeep,
11
+ } from '@speclynx/apidom-datamodel';
12
+ import { toValue, toYAML } from '@speclynx/apidom-core';
13
+ import { ApiDOMStructuredError } from '@speclynx/apidom-error';
14
+ import { traverse, traverseAsync, find, type Path } from '@speclynx/apidom-traverse';
15
+ import { evaluate, URIFragmentIdentifier } from '@speclynx/apidom-json-pointer';
16
+ import {
17
+ ReferenceElement,
18
+ ExampleElement,
19
+ LinkElement,
20
+ PathItemElement,
21
+ isReferenceElement,
22
+ isOperationElement,
23
+ isPathItemElement,
24
+ isReferenceLikeElement,
25
+ refract,
26
+ refractReference,
27
+ refractPathItem,
28
+ refractOperation,
29
+ } from '@speclynx/apidom-ns-openapi-3-0';
30
+
31
+ import UnresolvableReferenceError from '../../../errors/UnresolvableReferenceError.ts';
32
+ import MaximumDereferenceDepthError from '../../../errors/MaximumDereferenceDepthError.ts';
33
+ import MaximumResolveDepthError from '../../../errors/MaximumResolveDepthError.ts';
34
+ import * as url from '../../../util/url.ts';
35
+ import parse from '../../../parse/index.ts';
36
+ import Reference from '../../../Reference.ts';
37
+ import ReferenceSet from '../../../ReferenceSet.ts';
38
+ import { AncestorLineage } from '../../util.ts';
39
+ import type { ReferenceOptions } from '../../../options/index.ts';
40
+
41
+ /**
42
+ * @public
43
+ */
44
+ export interface OpenAPI3_0DereferenceVisitorOptions {
45
+ readonly reference: Reference;
46
+ readonly options: ReferenceOptions;
47
+ readonly indirections?: Element[];
48
+ readonly refractCache?: WeakMap<Element, Element>;
49
+ readonly ancestors?: AncestorLineage<Element>;
50
+ }
51
+
52
+ /**
53
+ * @public
54
+ */
55
+ class OpenAPI3_0DereferenceVisitor {
56
+ protected readonly indirections: Element[];
57
+
58
+ protected readonly reference: Reference;
59
+
60
+ protected readonly options: ReferenceOptions;
61
+
62
+ protected readonly refractCache: WeakMap<Element, Element>;
63
+
64
+ /**
65
+ * Tracks element ancestors across dive-deep traversal boundaries.
66
+ * Used for cycle detection: if a referenced element is found in
67
+ * the ancestor lineage, a circular reference is detected.
68
+ */
69
+ protected readonly ancestors: AncestorLineage<Element>;
70
+
71
+ constructor({
72
+ reference,
73
+ options,
74
+ indirections = [],
75
+ ancestors = new AncestorLineage(),
76
+ refractCache = new WeakMap(),
77
+ }: OpenAPI3_0DereferenceVisitorOptions) {
78
+ this.indirections = indirections;
79
+ this.reference = reference;
80
+ this.options = options;
81
+ this.ancestors = new AncestorLineage(...ancestors);
82
+ this.refractCache = refractCache;
83
+ }
84
+
85
+ protected toAncestorLineage(path: Path<Element>): [AncestorLineage<Element>, Set<Element>] {
86
+ const ancestorNodes = path.getAncestorNodes();
87
+ const directAncestors = new Set<Element>(ancestorNodes.filter(isElement));
88
+ const ancestorsLineage = new AncestorLineage(...this.ancestors, directAncestors);
89
+
90
+ return [ancestorsLineage, directAncestors];
91
+ }
92
+
93
+ protected toBaseURI(uri: string): string {
94
+ return url.resolve(this.reference.uri, url.sanitize(url.stripHash(uri)));
95
+ }
96
+
97
+ protected async toReference(uri: string): Promise<Reference> {
98
+ // detect maximum depth of resolution
99
+ if (this.reference.depth >= this.options.resolve.maxDepth) {
100
+ throw new MaximumResolveDepthError(
101
+ `Maximum resolution depth of ${this.options.resolve.maxDepth} has been exceeded by file "${this.reference.uri}"`,
102
+ { maxDepth: this.options.resolve.maxDepth, uri: this.reference.uri },
103
+ );
104
+ }
105
+
106
+ const baseURI = this.toBaseURI(uri);
107
+ const { refSet } = this.reference as { refSet: ReferenceSet };
108
+
109
+ // we've already processed this Reference in past
110
+ if (refSet.has(baseURI)) {
111
+ return refSet.find(propEq(baseURI, 'uri'))!;
112
+ }
113
+
114
+ const parseResult = await parse(url.unsanitize(baseURI), {
115
+ ...this.options,
116
+ parse: { ...this.options.parse, mediaType: 'text/plain' },
117
+ });
118
+
119
+ // register new mutable reference with a refSet
120
+ //
121
+ // NOTE(known limitation): the mutable reference is mutated in place during traversal
122
+ // (via `{ mutable: true }`). When an external document evaluates a JSON pointer back
123
+ // into this document, it may receive an already-resolved element instead of the original
124
+ // $ref. That resolved element was produced using the entry document's resolution context
125
+ // (ancestors, indirections), which may differ from the external document's context.
126
+ // This can affect cycle detection in rare cross-document circular reference patterns.
127
+ //
128
+ // Remediation: evaluate JSON pointers against the immutable (original) parse tree
129
+ // instead of the mutable working copy. The `immutable://` reference below preserves
130
+ // the original tree and could be used for pointer evaluation, ensuring every resolution
131
+ // context always sees raw, unresolved elements and processes them with its own
132
+ // ancestors/indirections. The trade-off is that elements referenced by multiple
133
+ // documents would be resolved once per context instead of being reused.
134
+ const mutableReference = new Reference({
135
+ uri: baseURI,
136
+ value: this.options.dereference.immutable ? cloneDeep(parseResult) : parseResult,
137
+ depth: this.reference.depth + 1,
138
+ });
139
+ refSet.add(mutableReference);
140
+
141
+ if (this.options.dereference.immutable) {
142
+ // register new immutable reference with a refSet
143
+ const immutableReference = new Reference({
144
+ uri: `immutable://${baseURI}`,
145
+ value: parseResult,
146
+ depth: this.reference.depth + 1,
147
+ });
148
+ refSet.add(immutableReference);
149
+ }
150
+
151
+ return mutableReference;
152
+ }
153
+
154
+ /**
155
+ * Handles an error according to the continueOnError option.
156
+ *
157
+ * For new errors: wraps in UnresolvableReferenceError with structured context
158
+ * (type, uri, location, codeFrame, refFieldName, refFieldValue, trace).
159
+ * For errors already wrapped by a nested visitor: prepends the current hop to the trace.
160
+ *
161
+ * Inner/intermediate visitors always throw to let the trace accumulate.
162
+ * Only the entry document visitor respects continueOnError (callback/swallow/throw).
163
+ */
164
+ protected handleError(
165
+ message: string,
166
+ error: Error,
167
+ referencingElement: Element,
168
+ refFieldName: string,
169
+ refFieldValue: string,
170
+ visitorPath: Path<Element>,
171
+ ): void {
172
+ const { continueOnError } = this.options.dereference;
173
+ const isEntryDocument =
174
+ url.stripHash(this.reference.refSet?.rootRef?.uri ?? '') === this.reference.uri;
175
+ const uri = this.reference.uri;
176
+ const type = referencingElement.element as string;
177
+ const codeFrame = toYAML(referencingElement);
178
+
179
+ // find element location by identity in the document tree.
180
+ // guarded: this.reference.value may not be a ParseResultElement or may lack a result.
181
+ // falls back to visitorPath which may produce an incomplete path when
182
+ // dereferenceApiDOM is called with a fragment (cloneShallow creates a new root identity).
183
+ let location: string | undefined;
184
+ const root = (this.reference.value as ParseResultElement).result;
185
+ if (isElement(root)) {
186
+ traverse(root, {
187
+ enter: (p: Path<Element>) => {
188
+ if (
189
+ p.node === referencingElement ||
190
+ this.refractCache.get(p.node) === referencingElement
191
+ ) {
192
+ location = p.formatPath();
193
+ p.stop();
194
+ }
195
+ },
196
+ });
197
+ }
198
+ location ??= visitorPath.formatPath();
199
+
200
+ const hop = { uri, type, refFieldName, refFieldValue, location, codeFrame };
201
+
202
+ // enrich existing error from nested visitor or create new one
203
+ let unresolvedError: UnresolvableReferenceError;
204
+ if (error instanceof UnresolvableReferenceError) {
205
+ // prefix relative locations for entries belonging to the referenced document
206
+ const refBaseURI = this.toBaseURI(refFieldValue);
207
+ const fragment = URIFragmentIdentifier.fromURIReference(refFieldValue);
208
+ if (fragment) {
209
+ if (refBaseURI === (error as any).uri && (error as any).location) {
210
+ (error as any).location = fragment + (error as any).location;
211
+ }
212
+ for (const h of (error as any).trace) {
213
+ if (h.uri === refBaseURI && h.location) h.location = fragment + h.location;
214
+ }
215
+ }
216
+ // @ts-ignore
217
+ error.trace = [hop, ...error.trace];
218
+ unresolvedError = error;
219
+ } else {
220
+ unresolvedError = new UnresolvableReferenceError(message, {
221
+ cause: error,
222
+ type,
223
+ uri,
224
+ location,
225
+ codeFrame,
226
+ refFieldName,
227
+ refFieldValue,
228
+ trace: [],
229
+ });
230
+ }
231
+
232
+ if (!isEntryDocument || continueOnError === false) throw unresolvedError;
233
+ if (typeof continueOnError === 'function') continueOnError(unresolvedError);
234
+ }
235
+
236
+ public async ReferenceElement(path: Path<Element>) {
237
+ const referencingElement = path.node as ReferenceElement;
238
+
239
+ // skip current referencing element as it's already been access
240
+ if (this.indirections.includes(referencingElement)) {
241
+ path.skip();
242
+ return;
243
+ }
244
+
245
+ const retrievalURI = this.toBaseURI(toValue(referencingElement.$ref) as string);
246
+ const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
247
+ const isExternalReference = !isInternalReference;
248
+
249
+ // ignore resolving internal Reference Objects
250
+ if (!this.options.resolve.internal && isInternalReference) {
251
+ // skip traversing this reference element and all it's child elements
252
+ path.skip();
253
+ return;
254
+ }
255
+ // ignore resolving external Reference Objects
256
+ if (!this.options.resolve.external && isExternalReference) {
257
+ // skip traversing this reference element and all it's child elements
258
+ path.skip();
259
+ return;
260
+ }
261
+
262
+ const $refBaseURI = url.resolve(retrievalURI, toValue(referencingElement.$ref) as string);
263
+ const indirectionsSize = this.indirections.length;
264
+
265
+ try {
266
+ const reference = await this.toReference(toValue(referencingElement.$ref) as string);
267
+
268
+ this.indirections.push(referencingElement);
269
+
270
+ const jsonPointer = URIFragmentIdentifier.fromURIReference($refBaseURI);
271
+
272
+ // possibly non-semantic fragment
273
+ let referencedElement = evaluate<Element>(
274
+ (reference.value as ParseResultElement).result as Element,
275
+ jsonPointer,
276
+ );
277
+
278
+ // applying semantics to a fragment
279
+ const referencedElementType = referencingElement.meta.get('referenced-element') as string;
280
+ if (
281
+ referencedElement.element !== referencedElementType &&
282
+ !isReferenceElement(referencedElement)
283
+ ) {
284
+ if (this.refractCache.has(referencedElement)) {
285
+ referencedElement = this.refractCache.get(referencedElement)!;
286
+ } else if (isReferenceLikeElement(referencedElement)) {
287
+ // handling generic indirect references
288
+ const sourceElement = referencedElement;
289
+ referencedElement = refractReference(referencedElement);
290
+ referencedElement.meta.set('referenced-element', referencedElementType);
291
+ this.refractCache.set(sourceElement, referencedElement);
292
+ } else {
293
+ // handling direct references
294
+ const sourceElement = referencedElement;
295
+ referencedElement = refract(referencedElement, { element: referencedElementType });
296
+ this.refractCache.set(sourceElement, referencedElement);
297
+ }
298
+ }
299
+
300
+ // detect direct or indirect reference
301
+ if (referencingElement === referencedElement) {
302
+ throw new ApiDOMStructuredError('Recursive Reference Object detected', {
303
+ $ref: toValue(referencingElement.$ref),
304
+ });
305
+ }
306
+
307
+ // detect maximum depth of dereferencing
308
+ if (this.indirections.length > this.options.dereference.maxDepth) {
309
+ throw new MaximumDereferenceDepthError(
310
+ `Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`,
311
+ { maxDepth: this.options.dereference.maxDepth, uri: this.reference.uri },
312
+ );
313
+ }
314
+
315
+ // detect second deep dive into the same fragment and avoid it
316
+ const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
317
+ if (ancestorsLineage.includes(referencedElement)) {
318
+ reference.refSet!.circular = true;
319
+
320
+ if (this.options.dereference.circular === 'error') {
321
+ throw new ApiDOMStructuredError('Circular reference detected', {
322
+ $ref: toValue(referencingElement.$ref),
323
+ });
324
+ } else if (this.options.dereference.circular === 'replace') {
325
+ const refElement = new RefElement($refBaseURI, {
326
+ type: referencingElement.element,
327
+ uri: reference.uri,
328
+ $ref: toValue(referencingElement.$ref),
329
+ });
330
+ const replacer =
331
+ this.options.dereference.strategyOpts['openapi-3-0']?.circularReplacer ??
332
+ this.options.dereference.circularReplacer;
333
+ const replacement = replacer(refElement);
334
+
335
+ path.replaceWith(replacement);
336
+ return;
337
+ }
338
+ }
339
+
340
+ /**
341
+ * Dive deep into the fragment.
342
+ *
343
+ * Cases to consider:
344
+ * 1. We're crossing document boundary
345
+ * 2. Fragment is from non-entry document
346
+ * 3. Fragment is a Reference Object. We need to follow it to get the eventual value
347
+ * 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
348
+ */
349
+ const isNonEntryDocument = url.stripHash(reference.refSet!.rootRef!.uri) !== reference.uri;
350
+ const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
351
+ if (
352
+ (isExternalReference ||
353
+ isNonEntryDocument ||
354
+ isReferenceElement(referencedElement) ||
355
+ shouldDetectCircular) &&
356
+ !ancestorsLineage.includesCycle(referencedElement)
357
+ ) {
358
+ // append referencing reference to ancestors lineage
359
+ directAncestors.add(referencingElement);
360
+
361
+ const visitor = new OpenAPI3_0DereferenceVisitor({
362
+ reference,
363
+ indirections: [...this.indirections],
364
+ options: this.options,
365
+ refractCache: this.refractCache,
366
+ ancestors: ancestorsLineage,
367
+ });
368
+ referencedElement = await traverseAsync(referencedElement, visitor, { mutable: true });
369
+
370
+ // remove referencing reference from ancestors lineage
371
+ directAncestors.delete(referencingElement);
372
+ }
373
+
374
+ /**
375
+ * Creating a new version of referenced element to avoid modifying the original one.
376
+ */
377
+ const mergedElement = cloneShallow(referencedElement);
378
+ // annotate referenced element with info about original referencing element
379
+ mergedElement.meta.set('ref-fields', {
380
+ $ref: toValue(referencingElement.$ref),
381
+ });
382
+ // annotate fragment with info about origin
383
+ mergedElement.meta.set('ref-origin', reference.uri);
384
+ mergedElement.meta.set('ref-type', referencingElement.element);
385
+
386
+ /**
387
+ * Transclude referencing element with merged referenced element.
388
+ */
389
+ path.replaceWith(mergedElement);
390
+ } catch (error: unknown) {
391
+ const $ref = toValue(referencingElement.$ref) as string;
392
+ this.handleError(
393
+ `Error while dereferencing Reference Object. Cannot resolve $ref "${$ref}": ${(error as Error).message}`,
394
+ error as Error,
395
+ referencingElement,
396
+ '$ref',
397
+ $ref,
398
+ path,
399
+ );
400
+ } finally {
401
+ if (this.indirections.length > indirectionsSize) this.indirections.pop();
402
+ }
403
+ }
404
+
405
+ public async PathItemElement(path: Path<Element>) {
406
+ const referencingElement = path.node as PathItemElement;
407
+
408
+ // ignore PathItemElement without $ref field
409
+ if (!isStringElement(referencingElement.$ref)) {
410
+ return;
411
+ }
412
+
413
+ // skip current referencing element as it's already been access
414
+ if (this.indirections.includes(referencingElement)) {
415
+ path.skip();
416
+ return;
417
+ }
418
+
419
+ const retrievalURI = this.toBaseURI(toValue(referencingElement.$ref) as string);
420
+ const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
421
+ const isExternalReference = !isInternalReference;
422
+
423
+ // ignore resolving internal Path Item Objects
424
+ if (!this.options.resolve.internal && isInternalReference) {
425
+ // skip traversing this Path Item element but traverse all it's child elements
426
+ return;
427
+ }
428
+ // ignore resolving external Path Item Objects
429
+ if (!this.options.resolve.external && isExternalReference) {
430
+ // skip traversing this Path Item element but traverse all it's child elements
431
+ return;
432
+ }
433
+
434
+ const $refBaseURI = url.resolve(retrievalURI, toValue(referencingElement.$ref) as string);
435
+ const indirectionsSize = this.indirections.length;
436
+
437
+ try {
438
+ const reference = await this.toReference(toValue(referencingElement.$ref) as string);
439
+
440
+ this.indirections.push(referencingElement);
441
+
442
+ const jsonPointer = URIFragmentIdentifier.fromURIReference($refBaseURI);
443
+
444
+ // possibly non-semantic referenced element
445
+ let referencedElement = evaluate<Element>(
446
+ (reference.value as ParseResultElement).result as Element,
447
+ jsonPointer,
448
+ );
449
+
450
+ // applying semantics to a referenced element
451
+ if (!isPathItemElement(referencedElement)) {
452
+ if (this.refractCache.has(referencedElement)) {
453
+ referencedElement = this.refractCache.get(referencedElement)!;
454
+ } else {
455
+ const sourceElement = referencedElement;
456
+ referencedElement = refractPathItem(referencedElement);
457
+ this.refractCache.set(sourceElement, referencedElement);
458
+ }
459
+ }
460
+
461
+ // detect direct or indirect reference
462
+ if (referencingElement === referencedElement) {
463
+ throw new ApiDOMStructuredError('Recursive Path Item Object reference detected', {
464
+ $ref: toValue(referencingElement.$ref),
465
+ });
466
+ }
467
+
468
+ // detect maximum depth of dereferencing
469
+ if (this.indirections.length > this.options.dereference.maxDepth) {
470
+ throw new MaximumDereferenceDepthError(
471
+ `Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`,
472
+ { maxDepth: this.options.dereference.maxDepth, uri: this.reference.uri },
473
+ );
474
+ }
475
+
476
+ // detect cross-boundary cycle
477
+ const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
478
+ if (ancestorsLineage.includes(referencedElement)) {
479
+ reference.refSet!.circular = true;
480
+
481
+ if (this.options.dereference.circular === 'error') {
482
+ throw new ApiDOMStructuredError('Circular reference detected', {
483
+ $ref: toValue(referencingElement.$ref),
484
+ });
485
+ } else if (this.options.dereference.circular === 'replace') {
486
+ const refElement = new RefElement($refBaseURI, {
487
+ type: referencingElement.element,
488
+ uri: reference.uri,
489
+ $ref: toValue(referencingElement.$ref),
490
+ });
491
+ const replacer =
492
+ this.options.dereference.strategyOpts['openapi-3-0']?.circularReplacer ??
493
+ this.options.dereference.circularReplacer;
494
+ const replacement = replacer(refElement);
495
+
496
+ path.replaceWith(replacement);
497
+ return;
498
+ }
499
+ }
500
+
501
+ /**
502
+ * Dive deep into the fragment.
503
+ *
504
+ * Cases to consider:
505
+ * 1. We're crossing document boundary
506
+ * 2. Fragment is from non-entry document
507
+ * 3. Fragment is a Path Item Object with $ref field. We need to follow it to get the eventual value
508
+ * 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
509
+ */
510
+ const isNonEntryDocument = url.stripHash(reference.refSet!.rootRef!.uri) !== reference.uri;
511
+ const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
512
+ if (
513
+ (isExternalReference ||
514
+ isNonEntryDocument ||
515
+ (isPathItemElement(referencedElement) && isStringElement(referencedElement.$ref)) ||
516
+ shouldDetectCircular) &&
517
+ !ancestorsLineage.includesCycle(referencedElement)
518
+ ) {
519
+ // append referencing reference to ancestors lineage
520
+ directAncestors.add(referencingElement);
521
+
522
+ const visitor = new OpenAPI3_0DereferenceVisitor({
523
+ reference,
524
+ indirections: [...this.indirections],
525
+ options: this.options,
526
+ refractCache: this.refractCache,
527
+ ancestors: ancestorsLineage,
528
+ });
529
+ referencedElement = await traverseAsync(referencedElement, visitor, { mutable: true });
530
+
531
+ // remove referencing reference from ancestors lineage
532
+ directAncestors.delete(referencingElement);
533
+ }
534
+
535
+ /**
536
+ * Creating a new version of Path Item by merging fields from referenced Path Item with referencing one.
537
+ */
538
+ if (isPathItemElement(referencedElement)) {
539
+ const mergedElement = cloneShallow<PathItemElement>(referencedElement);
540
+ // existing keywords from referencing PathItemElement overrides ones from referenced element
541
+ referencingElement.forEach((value: Element, keyElement: Element, item: Element) => {
542
+ mergedElement.remove(toValue(keyElement) as string);
543
+ (mergedElement.content as Element[]).push(item);
544
+ });
545
+ mergedElement.remove('$ref');
546
+
547
+ // annotate referenced element with info about original referencing element
548
+ mergedElement.meta.set('ref-fields', {
549
+ $ref: toValue(referencingElement.$ref),
550
+ });
551
+ // annotate referenced element with info about origin and type
552
+ mergedElement.meta.set('ref-origin', reference.uri);
553
+ mergedElement.meta.set('ref-type', referencingElement.element);
554
+
555
+ referencedElement = mergedElement;
556
+ }
557
+
558
+ /**
559
+ * Transclude referencing element with merged referenced element.
560
+ */
561
+ path.replaceWith(referencedElement);
562
+ } catch (error: unknown) {
563
+ const $ref = toValue(referencingElement.$ref) as string;
564
+ this.handleError(
565
+ `Error while dereferencing Path Item Object. Cannot resolve $ref "${$ref}": ${(error as Error).message}`,
566
+ error as Error,
567
+ referencingElement,
568
+ '$ref',
569
+ $ref,
570
+ path,
571
+ );
572
+ } finally {
573
+ if (this.indirections.length > indirectionsSize) this.indirections.pop();
574
+ }
575
+ }
576
+
577
+ public async LinkElement(path: Path<Element>) {
578
+ const linkElement = path.node as LinkElement;
579
+
580
+ // ignore LinkElement without operationRef or operationId field
581
+ if (!isStringElement(linkElement.operationRef) && !isStringElement(linkElement.operationId)) {
582
+ return;
583
+ }
584
+
585
+ // operationRef and operationId fields are mutually exclusive
586
+ if (isStringElement(linkElement.operationRef) && isStringElement(linkElement.operationId)) {
587
+ throw new ApiDOMStructuredError(
588
+ 'LinkElement operationRef and operationId fields are mutually exclusive',
589
+ {
590
+ operationRef: toValue(linkElement.operationRef),
591
+ operationId: toValue(linkElement.operationId),
592
+ },
593
+ );
594
+ }
595
+
596
+ try {
597
+ let operationElement: Element | undefined;
598
+
599
+ if (isStringElement(linkElement.operationRef)) {
600
+ // possibly non-semantic referenced element
601
+ const jsonPointer = URIFragmentIdentifier.fromURIReference(
602
+ toValue(linkElement.operationRef) as string,
603
+ );
604
+ const retrievalURI = this.toBaseURI(toValue(linkElement.operationRef) as string);
605
+ const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
606
+ const isExternalReference = !isInternalReference;
607
+
608
+ // ignore resolving internal Operation Object reference
609
+ if (!this.options.resolve.internal && isInternalReference) {
610
+ // skip traversing this Link element but traverse all it's child elements
611
+ return;
612
+ }
613
+ // ignore resolving external Operation Object reference
614
+ if (!this.options.resolve.external && isExternalReference) {
615
+ // skip traversing this Link element but traverse all it's child elements
616
+ return;
617
+ }
618
+
619
+ const reference = await this.toReference(toValue(linkElement.operationRef) as string);
620
+
621
+ operationElement = evaluate<Element>(
622
+ (reference.value as ParseResultElement).result as Element,
623
+ jsonPointer,
624
+ );
625
+ // applying semantics to a referenced element
626
+ if (!isOperationElement(operationElement)) {
627
+ if (this.refractCache.has(operationElement)) {
628
+ operationElement = this.refractCache.get(operationElement)!;
629
+ } else {
630
+ const sourceElement = operationElement;
631
+ operationElement = refractOperation(operationElement);
632
+ this.refractCache.set(sourceElement, operationElement);
633
+ }
634
+ }
635
+ // create shallow clone to be able to annotate with metadata
636
+ operationElement = cloneShallow(operationElement);
637
+ // annotate operation element with info about origin
638
+ operationElement.meta.set('ref-origin', reference.uri);
639
+ operationElement.meta.set('ref-type', linkElement.element);
640
+
641
+ const linkElementCopy = cloneShallow(linkElement);
642
+ linkElementCopy.operationRef?.meta.set('operation', operationElement);
643
+
644
+ /**
645
+ * Transclude Link Object containing Operation Object in its meta.
646
+ */
647
+ path.replaceWith(linkElementCopy);
648
+ return;
649
+ }
650
+
651
+ if (isStringElement(linkElement.operationId)) {
652
+ const operationId = toValue(linkElement.operationId) as string;
653
+ const reference = await this.toReference(url.unsanitize(this.reference.uri));
654
+ const operationPath = find(
655
+ (reference.value as ParseResultElement).result as Element,
656
+ (path) =>
657
+ isOperationElement(path.node) &&
658
+ isElement(path.node.operationId) &&
659
+ path.node.operationId.equals(operationId),
660
+ );
661
+ operationElement = operationPath?.node;
662
+ // OperationElement not found by its operationId
663
+ if (isUndefined(operationElement)) {
664
+ throw new ApiDOMStructuredError(
665
+ `OperationElement(operationId=${operationId}) not found`,
666
+ { operationId },
667
+ );
668
+ }
669
+
670
+ const linkElementCopy = cloneShallow(linkElement);
671
+ linkElementCopy.operationId?.meta.set('operation', operationElement);
672
+
673
+ /**
674
+ * Transclude Link Object containing Operation Object in its meta.
675
+ */
676
+ path.replaceWith(linkElementCopy);
677
+ }
678
+ } catch (error: unknown) {
679
+ const refFieldName = isStringElement(linkElement.operationRef)
680
+ ? 'operationRef'
681
+ : 'operationId';
682
+ const refFieldValue = isStringElement(linkElement.operationRef)
683
+ ? (toValue(linkElement.operationRef) as string)
684
+ : (toValue(linkElement.operationId) as string);
685
+ this.handleError(
686
+ `Error while dereferencing Link Object. Cannot resolve ${refFieldName} "${refFieldValue}": ${(error as Error).message}`,
687
+ error as Error,
688
+ linkElement,
689
+ refFieldName,
690
+ refFieldValue,
691
+ path,
692
+ );
693
+ }
694
+ }
695
+
696
+ public async ExampleElement(path: Path<Element>) {
697
+ const exampleElement = path.node as ExampleElement;
698
+
699
+ // ignore ExampleElement without externalValue field
700
+ if (!isStringElement(exampleElement.externalValue)) {
701
+ return;
702
+ }
703
+
704
+ // value and externalValue fields are mutually exclusive
705
+ if (exampleElement.hasKey('value') && isStringElement(exampleElement.externalValue)) {
706
+ throw new ApiDOMStructuredError(
707
+ 'ExampleElement value and externalValue fields are mutually exclusive',
708
+ {
709
+ value: toValue(exampleElement.value),
710
+ externalValue: toValue(exampleElement.externalValue),
711
+ },
712
+ );
713
+ }
714
+
715
+ const retrievalURI = this.toBaseURI(toValue(exampleElement.externalValue) as string);
716
+ const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
717
+ const isExternalReference = !isInternalReference;
718
+
719
+ // ignore resolving external Example Objects
720
+ if (!this.options.resolve.internal && isInternalReference) {
721
+ // skip traversing this Example element but traverse all it's child elements
722
+ return;
723
+ }
724
+ // ignore resolving external Example Objects
725
+ if (!this.options.resolve.external && isExternalReference) {
726
+ // skip traversing this Example element but traverse all it's child elements
727
+ return;
728
+ }
729
+
730
+ try {
731
+ const reference = await this.toReference(toValue(exampleElement.externalValue) as string);
732
+
733
+ // shallow clone of the referenced element
734
+ const valueElement = cloneShallow((reference.value as ParseResultElement).result as Element);
735
+ // annotate operation element with info about origin
736
+ valueElement.meta.set('ref-origin', reference.uri);
737
+ valueElement.meta.set('ref-type', exampleElement.element);
738
+
739
+ const exampleElementCopy = cloneShallow(exampleElement);
740
+ exampleElementCopy.value = valueElement;
741
+
742
+ /**
743
+ * Transclude Example Object containing external value.
744
+ */
745
+ path.replaceWith(exampleElementCopy);
746
+ } catch (error: unknown) {
747
+ const externalValue = toValue(exampleElement.externalValue) as string;
748
+ this.handleError(
749
+ `Error while dereferencing Example Object. Cannot resolve externalValue "${externalValue}": ${(error as Error).message}`,
750
+ error as Error,
751
+ exampleElement,
752
+ 'externalValue',
753
+ externalValue,
754
+ path,
755
+ );
756
+ }
757
+ }
758
+ }
759
+
760
+ export default OpenAPI3_0DereferenceVisitor;