@speclynx/apidom-reference 3.0.0 → 3.2.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.
- package/CHANGELOG.md +16 -0
- package/README.md +97 -0
- package/dist/apidom-reference.browser.js +3862 -3241
- package/dist/apidom-reference.browser.min.js +1 -1
- package/package.json +48 -25
- package/src/dereference/index.cjs +4 -0
- package/src/dereference/index.mjs +4 -0
- package/src/dereference/strategies/apidom/visitor.cjs +139 -59
- package/src/dereference/strategies/apidom/visitor.mjs +142 -62
- package/src/dereference/strategies/arazzo-1/index.cjs +1 -4
- package/src/dereference/strategies/arazzo-1/index.mjs +2 -4
- package/src/dereference/strategies/arazzo-1/visitor.cjs +289 -199
- package/src/dereference/strategies/arazzo-1/visitor.mjs +292 -203
- package/src/dereference/strategies/asyncapi-2/index.cjs +1 -4
- package/src/dereference/strategies/asyncapi-2/index.mjs +2 -4
- package/src/dereference/strategies/asyncapi-2/visitor.cjs +325 -229
- package/src/dereference/strategies/asyncapi-2/visitor.mjs +328 -233
- package/src/dereference/strategies/openapi-2/index.cjs +1 -4
- package/src/dereference/strategies/openapi-2/index.mjs +2 -4
- package/src/dereference/strategies/openapi-2/visitor.cjs +420 -318
- package/src/dereference/strategies/openapi-2/visitor.mjs +425 -324
- package/src/dereference/strategies/openapi-3-0/index.cjs +1 -4
- package/src/dereference/strategies/openapi-3-0/index.mjs +2 -4
- package/src/dereference/strategies/openapi-3-0/visitor.cjs +405 -286
- package/src/dereference/strategies/openapi-3-0/visitor.mjs +409 -291
- package/src/dereference/strategies/openapi-3-1/index.cjs +1 -4
- package/src/dereference/strategies/openapi-3-1/index.mjs +2 -4
- package/src/dereference/strategies/openapi-3-1/visitor.cjs +598 -484
- package/src/dereference/strategies/openapi-3-1/visitor.mjs +602 -489
- package/src/errors/DereferenceError.cjs +1 -1
- package/src/errors/DereferenceError.mjs +2 -2
- package/src/errors/ResolveError.cjs +1 -1
- package/src/errors/ResolveError.mjs +2 -2
- package/src/errors/UnresolvableReferenceError.cjs +11 -0
- package/src/errors/UnresolvableReferenceError.mjs +6 -0
- package/src/index.cjs +3 -1
- package/src/index.mjs +1 -0
- package/src/options/index.cjs +10 -1
- package/src/options/index.mjs +10 -1
- package/src/util/plugins.cjs +1 -6
- package/src/util/plugins.mjs +2 -5
- package/types/apidom-reference.d.ts +10 -2
- package/types/dereference/strategies/apidom/visitor.d.ts +10 -0
- package/types/dereference/strategies/arazzo-1/visitor.d.ts +19 -5
- package/types/dereference/strategies/asyncapi-2/visitor.d.ts +21 -7
- package/types/dereference/strategies/openapi-2/visitor.d.ts +21 -8
- package/types/dereference/strategies/openapi-3-0/visitor.d.ts +21 -7
- package/types/dereference/strategies/openapi-3-1/visitor.d.ts +21 -7
- package/types/errors/DereferenceError.d.ts +2 -2
- package/types/errors/ResolveError.d.ts +2 -2
- package/types/errors/UnresolvableReferenceError.d.ts +7 -0
- package/types/index.d.ts +1 -0
- package/types/options/index.d.ts +2 -0
|
@@ -11,15 +11,13 @@ var _apidomError = require("@speclynx/apidom-error");
|
|
|
11
11
|
var _apidomTraverse = require("@speclynx/apidom-traverse");
|
|
12
12
|
var _apidomJsonPointer = require("@speclynx/apidom-json-pointer");
|
|
13
13
|
var _apidomNsAsyncapi = require("@speclynx/apidom-ns-asyncapi-2");
|
|
14
|
+
var _UnresolvableReferenceError = _interopRequireDefault(require("../../../errors/UnresolvableReferenceError.cjs"));
|
|
14
15
|
var _MaximumDereferenceDepthError = _interopRequireDefault(require("../../../errors/MaximumDereferenceDepthError.cjs"));
|
|
15
16
|
var _MaximumResolveDepthError = _interopRequireDefault(require("../../../errors/MaximumResolveDepthError.cjs"));
|
|
16
17
|
var _util = require("../../util.cjs");
|
|
17
18
|
var url = _interopRequireWildcard(require("../../../util/url.cjs"));
|
|
18
19
|
var _index = _interopRequireDefault(require("../../../parse/index.cjs"));
|
|
19
20
|
var _Reference = _interopRequireDefault(require("../../../Reference.cjs"));
|
|
20
|
-
// initialize element identity manager
|
|
21
|
-
const identityManager = new _apidomCore.IdentityManager();
|
|
22
|
-
|
|
23
21
|
/**
|
|
24
22
|
* @public
|
|
25
23
|
*/
|
|
@@ -29,33 +27,45 @@ const identityManager = new _apidomCore.IdentityManager();
|
|
|
29
27
|
*/
|
|
30
28
|
class AsyncAPI2DereferenceVisitor {
|
|
31
29
|
indirections;
|
|
32
|
-
namespace;
|
|
33
30
|
reference;
|
|
34
31
|
options;
|
|
35
|
-
ancestors;
|
|
36
32
|
refractCache;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Tracks element ancestors across dive-deep traversal boundaries.
|
|
36
|
+
* Used for cycle detection: if a referenced element is found in
|
|
37
|
+
* the ancestor lineage, a circular reference is detected.
|
|
38
|
+
*/
|
|
39
|
+
ancestors;
|
|
37
40
|
constructor({
|
|
38
41
|
reference,
|
|
39
|
-
namespace,
|
|
40
42
|
options,
|
|
41
43
|
indirections = [],
|
|
42
44
|
ancestors = new _util.AncestorLineage(),
|
|
43
|
-
refractCache = new
|
|
45
|
+
refractCache = new WeakMap()
|
|
44
46
|
}) {
|
|
45
47
|
this.indirections = indirections;
|
|
46
|
-
this.namespace = namespace;
|
|
47
48
|
this.reference = reference;
|
|
48
49
|
this.options = options;
|
|
49
50
|
this.ancestors = new _util.AncestorLineage(...ancestors);
|
|
50
51
|
this.refractCache = refractCache;
|
|
51
52
|
}
|
|
53
|
+
toAncestorLineage(path) {
|
|
54
|
+
const ancestorNodes = path.getAncestorNodes();
|
|
55
|
+
const directAncestors = new Set(ancestorNodes.filter(_apidomDatamodel.isElement));
|
|
56
|
+
const ancestorsLineage = new _util.AncestorLineage(...this.ancestors, directAncestors);
|
|
57
|
+
return [ancestorsLineage, directAncestors];
|
|
58
|
+
}
|
|
52
59
|
toBaseURI(uri) {
|
|
53
60
|
return url.resolve(this.reference.uri, url.sanitize(url.stripHash(uri)));
|
|
54
61
|
}
|
|
55
62
|
async toReference(uri) {
|
|
56
63
|
// detect maximum depth of resolution
|
|
57
64
|
if (this.reference.depth >= this.options.resolve.maxDepth) {
|
|
58
|
-
throw new _MaximumResolveDepthError.default(`Maximum resolution depth of ${this.options.resolve.maxDepth} has been exceeded by file "${this.reference.uri}"
|
|
65
|
+
throw new _MaximumResolveDepthError.default(`Maximum resolution depth of ${this.options.resolve.maxDepth} has been exceeded by file "${this.reference.uri}"`, {
|
|
66
|
+
maxDepth: this.options.resolve.maxDepth,
|
|
67
|
+
uri: this.reference.uri
|
|
68
|
+
});
|
|
59
69
|
}
|
|
60
70
|
const baseURI = this.toBaseURI(uri);
|
|
61
71
|
const {
|
|
@@ -75,9 +85,23 @@ class AsyncAPI2DereferenceVisitor {
|
|
|
75
85
|
});
|
|
76
86
|
|
|
77
87
|
// register new mutable reference with a refSet
|
|
88
|
+
//
|
|
89
|
+
// NOTE(known limitation): the mutable reference is mutated in place during traversal
|
|
90
|
+
// (via `{ mutable: true }`). When an external document evaluates a JSON pointer back
|
|
91
|
+
// into this document, it may receive an already-resolved element instead of the original
|
|
92
|
+
// $ref. That resolved element was produced using the entry document's resolution context
|
|
93
|
+
// (ancestors, indirections), which may differ from the external document's context.
|
|
94
|
+
// This can affect cycle detection in rare cross-document circular reference patterns.
|
|
95
|
+
//
|
|
96
|
+
// Remediation: evaluate JSON pointers against the immutable (original) parse tree
|
|
97
|
+
// instead of the mutable working copy. The `immutable://` reference below preserves
|
|
98
|
+
// the original tree and could be used for pointer evaluation, ensuring every resolution
|
|
99
|
+
// context always sees raw, unresolved elements and processes them with its own
|
|
100
|
+
// ancestors/indirections. The trade-off is that elements referenced by multiple
|
|
101
|
+
// documents would be resolved once per context instead of being reused.
|
|
78
102
|
const mutableReference = new _Reference.default({
|
|
79
103
|
uri: baseURI,
|
|
80
|
-
value: (0, _apidomDatamodel.cloneDeep)(parseResult),
|
|
104
|
+
value: this.options.dereference.immutable ? (0, _apidomDatamodel.cloneDeep)(parseResult) : parseResult,
|
|
81
105
|
depth: this.reference.depth + 1
|
|
82
106
|
});
|
|
83
107
|
refSet.add(mutableReference);
|
|
@@ -92,15 +116,77 @@ class AsyncAPI2DereferenceVisitor {
|
|
|
92
116
|
}
|
|
93
117
|
return mutableReference;
|
|
94
118
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Handles an error according to the continueOnError option.
|
|
122
|
+
*
|
|
123
|
+
* For new errors: wraps in UnresolvableReferenceError with structured context
|
|
124
|
+
* (type, uri, location, codeFrame, refFieldName, refFieldValue, trace).
|
|
125
|
+
* For errors already wrapped by a nested visitor: prepends the current hop to the trace.
|
|
126
|
+
*
|
|
127
|
+
* Inner/intermediate visitors always throw to let the trace accumulate.
|
|
128
|
+
* Only the entry document visitor respects continueOnError (callback/swallow/throw).
|
|
129
|
+
*/
|
|
130
|
+
handleError(message, error, referencingElement, refFieldName, refFieldValue, visitorPath) {
|
|
131
|
+
const {
|
|
132
|
+
continueOnError
|
|
133
|
+
} = this.options.dereference;
|
|
134
|
+
const isEntryDocument = url.stripHash(this.reference.refSet?.rootRef?.uri ?? '') === this.reference.uri;
|
|
135
|
+
const uri = this.reference.uri;
|
|
136
|
+
const type = referencingElement.element;
|
|
137
|
+
const codeFrame = (0, _apidomCore.toYAML)(referencingElement);
|
|
138
|
+
|
|
139
|
+
// find element location: tree search for entry documents, visitor path for external
|
|
140
|
+
let location;
|
|
141
|
+
(0, _apidomTraverse.traverse)(this.reference.value.result, {
|
|
142
|
+
enter: p => {
|
|
143
|
+
if (p.node === referencingElement || this.refractCache.get(p.node) === referencingElement) {
|
|
144
|
+
location = p.formatPath();
|
|
145
|
+
p.stop();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
location ??= visitorPath.formatPath();
|
|
150
|
+
const hop = {
|
|
151
|
+
uri,
|
|
152
|
+
type,
|
|
153
|
+
refFieldName,
|
|
154
|
+
refFieldValue,
|
|
155
|
+
location,
|
|
156
|
+
codeFrame
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// enrich existing error from nested visitor or create new one
|
|
160
|
+
let unresolvedError;
|
|
161
|
+
if (error instanceof _UnresolvableReferenceError.default) {
|
|
162
|
+
// prefix relative locations for entries belonging to the referenced document
|
|
163
|
+
const refBaseURI = this.toBaseURI(refFieldValue);
|
|
164
|
+
const fragment = _apidomJsonPointer.URIFragmentIdentifier.fromURIReference(refFieldValue);
|
|
165
|
+
if (fragment) {
|
|
166
|
+
if (refBaseURI === error.uri && error.location) {
|
|
167
|
+
error.location = fragment + error.location;
|
|
168
|
+
}
|
|
169
|
+
for (const h of error.trace) {
|
|
170
|
+
if (h.uri === refBaseURI && h.location) h.location = fragment + h.location;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// @ts-ignore
|
|
174
|
+
error.trace = [hop, ...error.trace];
|
|
175
|
+
unresolvedError = error;
|
|
176
|
+
} else {
|
|
177
|
+
unresolvedError = new _UnresolvableReferenceError.default(message, {
|
|
178
|
+
cause: error,
|
|
179
|
+
type,
|
|
180
|
+
uri,
|
|
181
|
+
location,
|
|
182
|
+
codeFrame,
|
|
183
|
+
refFieldName,
|
|
184
|
+
refFieldValue,
|
|
185
|
+
trace: []
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
if (!isEntryDocument || continueOnError === false) throw unresolvedError;
|
|
189
|
+
if (typeof continueOnError === 'function') continueOnError(unresolvedError);
|
|
104
190
|
}
|
|
105
191
|
async ReferenceElement(path) {
|
|
106
192
|
const referencingElement = path.node;
|
|
@@ -110,7 +196,6 @@ class AsyncAPI2DereferenceVisitor {
|
|
|
110
196
|
path.skip();
|
|
111
197
|
return;
|
|
112
198
|
}
|
|
113
|
-
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
|
|
114
199
|
const retrievalURI = this.toBaseURI((0, _apidomCore.toValue)(referencingElement.$ref));
|
|
115
200
|
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
|
|
116
201
|
const isExternalReference = !isInternalReference;
|
|
@@ -127,133 +212,138 @@ class AsyncAPI2DereferenceVisitor {
|
|
|
127
212
|
path.skip();
|
|
128
213
|
return;
|
|
129
214
|
}
|
|
130
|
-
const reference = await this.toReference((0, _apidomCore.toValue)(referencingElement.$ref));
|
|
131
215
|
const $refBaseURI = url.resolve(retrievalURI, (0, _apidomCore.toValue)(referencingElement.$ref));
|
|
132
|
-
this.indirections.
|
|
133
|
-
|
|
216
|
+
const indirectionsSize = this.indirections.length;
|
|
217
|
+
try {
|
|
218
|
+
const reference = await this.toReference((0, _apidomCore.toValue)(referencingElement.$ref));
|
|
219
|
+
this.indirections.push(referencingElement);
|
|
220
|
+
const jsonPointer = _apidomJsonPointer.URIFragmentIdentifier.fromURIReference($refBaseURI);
|
|
134
221
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
referencedElement.id = identityManager.identify(referencedElement);
|
|
222
|
+
// possibly non-semantic fragment
|
|
223
|
+
let referencedElement = (0, _apidomJsonPointer.evaluate)(reference.value.result, jsonPointer);
|
|
138
224
|
|
|
139
|
-
|
|
140
|
-
* Applying semantics to a referenced element if semantics are missing.
|
|
141
|
-
*/
|
|
142
|
-
if ((0, _apidomDatamodel.isPrimitiveElement)(referencedElement)) {
|
|
225
|
+
// applying semantics to a fragment
|
|
143
226
|
const referencedElementType = referencingElement.meta.get('referenced-element');
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
227
|
+
if (referencedElement.element !== referencedElementType && !(0, _apidomNsAsyncapi.isReferenceElement)(referencedElement)) {
|
|
228
|
+
if (this.refractCache.has(referencedElement)) {
|
|
229
|
+
referencedElement = this.refractCache.get(referencedElement);
|
|
230
|
+
} else if ((0, _apidomNsAsyncapi.isReferenceLikeElement)(referencedElement)) {
|
|
231
|
+
// handling generic indirect references
|
|
232
|
+
const sourceElement = referencedElement;
|
|
233
|
+
referencedElement = (0, _apidomNsAsyncapi.refractReference)(referencedElement);
|
|
234
|
+
referencedElement.meta.set('referenced-element', referencedElementType);
|
|
235
|
+
this.refractCache.set(sourceElement, referencedElement);
|
|
236
|
+
} else {
|
|
237
|
+
// handling direct references
|
|
238
|
+
const sourceElement = referencedElement;
|
|
239
|
+
referencedElement = (0, _apidomNsAsyncapi.refract)(referencedElement, {
|
|
240
|
+
element: referencedElementType
|
|
241
|
+
});
|
|
242
|
+
this.refractCache.set(sourceElement, referencedElement);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// detect direct or circular reference
|
|
247
|
+
if (referencingElement === referencedElement) {
|
|
248
|
+
throw new _apidomError.ApiDOMStructuredError('Recursive Reference Object detected', {
|
|
249
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
156
250
|
});
|
|
157
|
-
this.refractCache.set(cacheKey, referencedElement);
|
|
158
251
|
}
|
|
159
|
-
}
|
|
160
252
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
253
|
+
// detect maximum depth of dereferencing
|
|
254
|
+
if (this.indirections.length > this.options.dereference.maxDepth) {
|
|
255
|
+
throw new _MaximumDereferenceDepthError.default(`Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`, {
|
|
256
|
+
maxDepth: this.options.dereference.maxDepth,
|
|
257
|
+
uri: this.reference.uri
|
|
258
|
+
});
|
|
259
|
+
}
|
|
165
260
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
261
|
+
// detect second deep dive into the same fragment and avoid it
|
|
262
|
+
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
|
|
263
|
+
if (ancestorsLineage.includes(referencedElement)) {
|
|
264
|
+
reference.refSet.circular = true;
|
|
265
|
+
if (this.options.dereference.circular === 'error') {
|
|
266
|
+
throw new _apidomError.ApiDOMStructuredError('Circular reference detected', {
|
|
267
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
268
|
+
});
|
|
269
|
+
} else if (this.options.dereference.circular === 'replace') {
|
|
270
|
+
const refElement = new _apidomDatamodel.RefElement($refBaseURI, {
|
|
271
|
+
type: referencingElement.element,
|
|
272
|
+
uri: reference.uri,
|
|
273
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
274
|
+
});
|
|
275
|
+
const replacer = this.options.dereference.strategyOpts['asyncapi-2']?.circularReplacer ?? this.options.dereference.circularReplacer;
|
|
276
|
+
const replacement = replacer(refElement);
|
|
277
|
+
path.replaceWith(replacement);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Dive deep into the fragment.
|
|
284
|
+
*
|
|
285
|
+
* Cases to consider:
|
|
286
|
+
* 1. We're crossing document boundary
|
|
287
|
+
* 2. Fragment is from non-entry document
|
|
288
|
+
* 3. Fragment is a Reference Object. We need to follow it to get the eventual value
|
|
289
|
+
* 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
|
|
290
|
+
*/
|
|
291
|
+
const isNonEntryDocument = url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
|
|
292
|
+
const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
|
|
293
|
+
if ((isExternalReference || isNonEntryDocument || (0, _apidomNsAsyncapi.isReferenceElement)(referencedElement) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
|
|
294
|
+
// append referencing reference to ancestors lineage
|
|
295
|
+
directAncestors.add(referencingElement);
|
|
296
|
+
const visitor = new AsyncAPI2DereferenceVisitor({
|
|
297
|
+
reference,
|
|
298
|
+
indirections: [...this.indirections],
|
|
299
|
+
options: this.options,
|
|
300
|
+
refractCache: this.refractCache,
|
|
301
|
+
ancestors: ancestorsLineage
|
|
302
|
+
});
|
|
303
|
+
referencedElement = await (0, _apidomTraverse.traverseAsync)(referencedElement, visitor, {
|
|
304
|
+
mutable: true
|
|
305
|
+
});
|
|
170
306
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
307
|
+
// remove referencing reference from ancestors lineage
|
|
308
|
+
directAncestors.delete(referencingElement);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Boolean JSON Schemas
|
|
312
|
+
if ((0, _apidomNsAsyncapi.isBooleanJSONSchemaElement)(referencedElement)) {
|
|
313
|
+
const booleanJsonSchemaElement = (0, _apidomDatamodel.cloneDeep)(referencedElement);
|
|
314
|
+
// annotate referenced element with info about original referencing element
|
|
315
|
+
booleanJsonSchemaElement.meta.set('ref-fields', {
|
|
180
316
|
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
181
317
|
});
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
path.replaceWith(
|
|
318
|
+
// annotate referenced element with info about origin
|
|
319
|
+
booleanJsonSchemaElement.meta.set('ref-origin', reference.uri);
|
|
320
|
+
booleanJsonSchemaElement.meta.set('ref-type', referencingElement.element);
|
|
321
|
+
path.replaceWith(booleanJsonSchemaElement);
|
|
186
322
|
return;
|
|
187
323
|
}
|
|
188
|
-
}
|
|
189
324
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
* 1. We're crossing document boundary
|
|
195
|
-
* 2. Fragment is from non-root document
|
|
196
|
-
* 3. Fragment is a Reference Object. We need to follow it to get the eventual value
|
|
197
|
-
* 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
|
|
198
|
-
*/
|
|
199
|
-
const isNonRootDocument = url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
|
|
200
|
-
const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
|
|
201
|
-
if ((isExternalReference || isNonRootDocument || (0, _apidomNsAsyncapi.isReferenceElement)(referencedElement) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
|
|
202
|
-
// append referencing reference to ancestors lineage
|
|
203
|
-
directAncestors.add(referencingElement);
|
|
204
|
-
const visitor = new AsyncAPI2DereferenceVisitor({
|
|
205
|
-
reference,
|
|
206
|
-
namespace: this.namespace,
|
|
207
|
-
indirections: [...this.indirections],
|
|
208
|
-
options: this.options,
|
|
209
|
-
refractCache: this.refractCache,
|
|
210
|
-
ancestors: ancestorsLineage
|
|
211
|
-
});
|
|
212
|
-
referencedElement = await (0, _apidomTraverse.traverseAsync)(referencedElement, visitor, {
|
|
213
|
-
mutable: true
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
// remove referencing reference from ancestors lineage
|
|
217
|
-
directAncestors.delete(referencingElement);
|
|
218
|
-
}
|
|
219
|
-
this.indirections.pop();
|
|
220
|
-
|
|
221
|
-
// Boolean JSON Schemas
|
|
222
|
-
if ((0, _apidomNsAsyncapi.isBooleanJSONSchemaElement)(referencedElement)) {
|
|
223
|
-
const booleanJsonSchemaElement = (0, _apidomDatamodel.cloneDeep)(referencedElement);
|
|
224
|
-
// assign unique id to merged element
|
|
225
|
-
booleanJsonSchemaElement.meta.set('id', identityManager.generateId());
|
|
325
|
+
/**
|
|
326
|
+
* Creating a new version of referenced element to avoid modifying the original one.
|
|
327
|
+
*/
|
|
328
|
+
const mergedElement = (0, _apidomDatamodel.cloneShallow)(referencedElement);
|
|
226
329
|
// annotate referenced element with info about original referencing element
|
|
227
|
-
|
|
330
|
+
mergedElement.meta.set('ref-fields', {
|
|
228
331
|
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
229
332
|
});
|
|
230
|
-
// annotate
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
booleanJsonSchemaElement.meta.set('ref-referencing-element-id', identityManager.identify(referencingElement));
|
|
234
|
-
path.replaceWith(booleanJsonSchemaElement);
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
333
|
+
// annotate fragment with info about origin
|
|
334
|
+
mergedElement.meta.set('ref-origin', reference.uri);
|
|
335
|
+
mergedElement.meta.set('ref-type', referencingElement.element);
|
|
237
336
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
// annotate fragment with info about origin
|
|
249
|
-
mergedElement.meta.set('ref-origin', reference.uri);
|
|
250
|
-
// annotate fragment with info about referencing element
|
|
251
|
-
mergedElement.meta.set('ref-referencing-element-id', identityManager.identify(referencingElement));
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Transclude referencing element with merged referenced element.
|
|
255
|
-
*/
|
|
256
|
-
path.replaceWith(mergedElement);
|
|
337
|
+
/**
|
|
338
|
+
* Transclude referencing element with merged referenced element.
|
|
339
|
+
*/
|
|
340
|
+
path.replaceWith(mergedElement);
|
|
341
|
+
} catch (error) {
|
|
342
|
+
const $ref = (0, _apidomCore.toValue)(referencingElement.$ref);
|
|
343
|
+
this.handleError(`Error while dereferencing Reference Object. Cannot resolve $ref "${$ref}": ${error.message}`, error, referencingElement, '$ref', $ref, path);
|
|
344
|
+
} finally {
|
|
345
|
+
if (this.indirections.length > indirectionsSize) this.indirections.pop();
|
|
346
|
+
}
|
|
257
347
|
}
|
|
258
348
|
async ChannelItemElement(path) {
|
|
259
349
|
const referencingElement = path.node;
|
|
@@ -268,7 +358,6 @@ class AsyncAPI2DereferenceVisitor {
|
|
|
268
358
|
path.skip();
|
|
269
359
|
return;
|
|
270
360
|
}
|
|
271
|
-
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
|
|
272
361
|
const retrievalURI = this.toBaseURI((0, _apidomCore.toValue)(referencingElement.$ref));
|
|
273
362
|
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
|
|
274
363
|
const isExternalReference = !isInternalReference;
|
|
@@ -283,117 +372,124 @@ class AsyncAPI2DereferenceVisitor {
|
|
|
283
372
|
// skip traversing this channel item but traverse all it's child elements
|
|
284
373
|
return;
|
|
285
374
|
}
|
|
286
|
-
const reference = await this.toReference((0, _apidomCore.toValue)(referencingElement.$ref));
|
|
287
375
|
const $refBaseURI = url.resolve(retrievalURI, (0, _apidomCore.toValue)(referencingElement.$ref));
|
|
288
|
-
this.indirections.
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
referencedElement.id = identityManager.identify(referencedElement);
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* Applying semantics to a referenced element if semantics are missing.
|
|
297
|
-
*/
|
|
298
|
-
if ((0, _apidomDatamodel.isPrimitiveElement)(referencedElement)) {
|
|
299
|
-
const cacheKey = `channel-item-${identityManager.identify(referencedElement)}`;
|
|
300
|
-
if (this.refractCache.has(cacheKey)) {
|
|
301
|
-
referencedElement = this.refractCache.get(cacheKey);
|
|
302
|
-
} else {
|
|
303
|
-
referencedElement = (0, _apidomNsAsyncapi.refractChannelItem)(referencedElement);
|
|
304
|
-
this.refractCache.set(cacheKey, referencedElement);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
376
|
+
const indirectionsSize = this.indirections.length;
|
|
377
|
+
try {
|
|
378
|
+
const reference = await this.toReference((0, _apidomCore.toValue)(referencingElement.$ref));
|
|
379
|
+
this.indirections.push(referencingElement);
|
|
380
|
+
const jsonPointer = _apidomJsonPointer.URIFragmentIdentifier.fromURIReference($refBaseURI);
|
|
307
381
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
throw new _apidomError.ApiDOMError('Recursive Channel Item Object reference detected');
|
|
311
|
-
}
|
|
382
|
+
// possibly non-semantic referenced element
|
|
383
|
+
let referencedElement = (0, _apidomJsonPointer.evaluate)(reference.value.result, jsonPointer);
|
|
312
384
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
385
|
+
// applying semantics to a referenced element
|
|
386
|
+
if (!(0, _apidomNsAsyncapi.isChannelItemElement)(referencedElement)) {
|
|
387
|
+
if (this.refractCache.has(referencedElement)) {
|
|
388
|
+
referencedElement = this.refractCache.get(referencedElement);
|
|
389
|
+
} else {
|
|
390
|
+
const sourceElement = referencedElement;
|
|
391
|
+
referencedElement = (0, _apidomNsAsyncapi.refractChannelItem)(referencedElement);
|
|
392
|
+
this.refractCache.set(sourceElement, referencedElement);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
317
395
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
if (this.options.dereference.circular === 'error') {
|
|
322
|
-
throw new _apidomError.ApiDOMError('Circular reference detected');
|
|
323
|
-
} else if (this.options.dereference.circular === 'replace') {
|
|
324
|
-
const refElement = new _apidomDatamodel.RefElement(referencedElement.id, {
|
|
325
|
-
type: 'channel-item',
|
|
326
|
-
uri: reference.uri,
|
|
396
|
+
// detect direct or indirect reference
|
|
397
|
+
if (referencingElement === referencedElement) {
|
|
398
|
+
throw new _apidomError.ApiDOMStructuredError('Recursive Channel Item Object reference detected', {
|
|
327
399
|
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
328
400
|
});
|
|
329
|
-
const replacer = this.options.dereference.strategyOpts['asyncapi-2']?.circularReplacer ?? this.options.dereference.circularReplacer;
|
|
330
|
-
const replacement = replacer(refElement);
|
|
331
|
-
this.indirections.pop();
|
|
332
|
-
path.replaceWith(replacement);
|
|
333
|
-
return;
|
|
334
401
|
}
|
|
335
|
-
}
|
|
336
402
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
* 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
|
|
345
|
-
*/
|
|
346
|
-
const isNonRootDocument = url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
|
|
347
|
-
const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
|
|
348
|
-
if ((isExternalReference || isNonRootDocument || (0, _apidomNsAsyncapi.isChannelItemElement)(referencedElement) && (0, _apidomDatamodel.isStringElement)(referencedElement.$ref) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
|
|
349
|
-
// append referencing reference to ancestors lineage
|
|
350
|
-
directAncestors.add(referencingElement);
|
|
351
|
-
const visitor = new AsyncAPI2DereferenceVisitor({
|
|
352
|
-
reference,
|
|
353
|
-
namespace: this.namespace,
|
|
354
|
-
indirections: [...this.indirections],
|
|
355
|
-
options: this.options,
|
|
356
|
-
refractCache: this.refractCache,
|
|
357
|
-
ancestors: ancestorsLineage
|
|
358
|
-
});
|
|
359
|
-
referencedElement = await (0, _apidomTraverse.traverseAsync)(referencedElement, visitor, {
|
|
360
|
-
mutable: true
|
|
361
|
-
});
|
|
403
|
+
// detect maximum depth of dereferencing
|
|
404
|
+
if (this.indirections.length > this.options.dereference.maxDepth) {
|
|
405
|
+
throw new _MaximumDereferenceDepthError.default(`Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`, {
|
|
406
|
+
maxDepth: this.options.dereference.maxDepth,
|
|
407
|
+
uri: this.reference.uri
|
|
408
|
+
});
|
|
409
|
+
}
|
|
362
410
|
|
|
363
|
-
//
|
|
364
|
-
directAncestors.
|
|
365
|
-
|
|
366
|
-
|
|
411
|
+
// detect second deep dive into the same fragment and avoid it
|
|
412
|
+
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
|
|
413
|
+
if (ancestorsLineage.includes(referencedElement)) {
|
|
414
|
+
reference.refSet.circular = true;
|
|
415
|
+
if (this.options.dereference.circular === 'error') {
|
|
416
|
+
throw new _apidomError.ApiDOMStructuredError('Circular reference detected', {
|
|
417
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
418
|
+
});
|
|
419
|
+
} else if (this.options.dereference.circular === 'replace') {
|
|
420
|
+
const refElement = new _apidomDatamodel.RefElement($refBaseURI, {
|
|
421
|
+
type: referencingElement.element,
|
|
422
|
+
uri: reference.uri,
|
|
423
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
424
|
+
});
|
|
425
|
+
const replacer = this.options.dereference.strategyOpts['asyncapi-2']?.circularReplacer ?? this.options.dereference.circularReplacer;
|
|
426
|
+
const replacement = replacer(refElement);
|
|
427
|
+
path.replaceWith(replacement);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
367
431
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
432
|
+
/**
|
|
433
|
+
* Dive deep into the fragment.
|
|
434
|
+
*
|
|
435
|
+
* Cases to consider:
|
|
436
|
+
* 1. We're crossing document boundary
|
|
437
|
+
* 2. Fragment is from non-entry document
|
|
438
|
+
* 3. Fragment is a Channel Item Object with $ref field. We need to follow it to get the eventual value
|
|
439
|
+
* 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
|
|
440
|
+
*/
|
|
441
|
+
const isNonEntryDocument = url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
|
|
442
|
+
const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
|
|
443
|
+
if ((isExternalReference || isNonEntryDocument || (0, _apidomNsAsyncapi.isChannelItemElement)(referencedElement) && (0, _apidomDatamodel.isStringElement)(referencedElement.$ref) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
|
|
444
|
+
// append referencing reference to ancestors lineage
|
|
445
|
+
directAncestors.add(referencingElement);
|
|
446
|
+
const visitor = new AsyncAPI2DereferenceVisitor({
|
|
447
|
+
reference,
|
|
448
|
+
indirections: [...this.indirections],
|
|
449
|
+
options: this.options,
|
|
450
|
+
refractCache: this.refractCache,
|
|
451
|
+
ancestors: ancestorsLineage
|
|
452
|
+
});
|
|
453
|
+
referencedElement = await (0, _apidomTraverse.traverseAsync)(referencedElement, visitor, {
|
|
454
|
+
mutable: true
|
|
455
|
+
});
|
|
381
456
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
457
|
+
// remove referencing reference from ancestors lineage
|
|
458
|
+
directAncestors.delete(referencingElement);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Creating a new version of Channel Item by merging fields from referenced Channel Item with referencing one.
|
|
463
|
+
*/
|
|
464
|
+
if ((0, _apidomNsAsyncapi.isChannelItemElement)(referencedElement)) {
|
|
465
|
+
const mergedElement = (0, _apidomDatamodel.cloneShallow)(referencedElement);
|
|
466
|
+
// existing keywords from referencing ChannelItemElement overrides ones from referenced ChannelItemElement
|
|
467
|
+
referencingElement.forEach((value, keyElement, item) => {
|
|
468
|
+
mergedElement.remove((0, _apidomCore.toValue)(keyElement));
|
|
469
|
+
mergedElement.content.push(item);
|
|
470
|
+
});
|
|
471
|
+
mergedElement.remove('$ref');
|
|
392
472
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
473
|
+
// annotate referenced element with info about original referencing element
|
|
474
|
+
mergedElement.meta.set('ref-fields', {
|
|
475
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
476
|
+
});
|
|
477
|
+
// annotate referenced with info about origin
|
|
478
|
+
mergedElement.meta.set('ref-origin', reference.uri);
|
|
479
|
+
mergedElement.meta.set('ref-type', referencingElement.element);
|
|
480
|
+
referencedElement = mergedElement;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Transclude referencing element with merged referenced element.
|
|
485
|
+
*/
|
|
486
|
+
path.replaceWith(referencedElement);
|
|
487
|
+
} catch (error) {
|
|
488
|
+
const $ref = (0, _apidomCore.toValue)(referencingElement.$ref);
|
|
489
|
+
this.handleError(`Error while dereferencing Channel Item Object. Cannot resolve $ref "${$ref}": ${error.message}`, error, referencingElement, '$ref', $ref, path);
|
|
490
|
+
} finally {
|
|
491
|
+
if (this.indirections.length > indirectionsSize) this.indirections.pop();
|
|
492
|
+
}
|
|
397
493
|
}
|
|
398
494
|
}
|
|
399
495
|
var _default = exports.default = AsyncAPI2DereferenceVisitor;
|