@speclynx/apidom-reference 3.2.1 → 4.0.0

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