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