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