@speclynx/apidom-reference 3.0.0 → 3.1.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 +6 -0
- package/README.md +97 -0
- package/dist/apidom-reference.browser.js +3854 -3241
- package/dist/apidom-reference.browser.min.js +1 -1
- package/package.json +25 -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 +288 -199
- package/src/dereference/strategies/arazzo-1/visitor.mjs +291 -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 +323 -229
- package/src/dereference/strategies/asyncapi-2/visitor.mjs +326 -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 +417 -318
- package/src/dereference/strategies/openapi-2/visitor.mjs +422 -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 +403 -286
- package/src/dereference/strategies/openapi-3-0/visitor.mjs +407 -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,137 @@ 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
|
-
|
|
127
|
-
|
|
209
|
+
try {
|
|
210
|
+
const reference = await this.toReference(toValue(referencingElement.$ref));
|
|
211
|
+
this.indirections.push(referencingElement);
|
|
212
|
+
const jsonPointer = URIFragmentIdentifier.fromURIReference($refBaseURI);
|
|
128
213
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
referencedElement.id = identityManager.identify(referencedElement);
|
|
214
|
+
// possibly non-semantic fragment
|
|
215
|
+
let referencedElement = evaluate(reference.value.result, jsonPointer);
|
|
132
216
|
|
|
133
|
-
|
|
134
|
-
* Applying semantics to a referenced element if semantics are missing.
|
|
135
|
-
*/
|
|
136
|
-
if (isPrimitiveElement(referencedElement)) {
|
|
217
|
+
// applying semantics to a fragment
|
|
137
218
|
const referencedElementType = referencingElement.meta.get('referenced-element');
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
219
|
+
if (referencedElement.element !== referencedElementType && !isReferenceElement(referencedElement)) {
|
|
220
|
+
if (this.refractCache.has(referencedElement)) {
|
|
221
|
+
referencedElement = this.refractCache.get(referencedElement);
|
|
222
|
+
} else if (isReferenceLikeElement(referencedElement)) {
|
|
223
|
+
// handling generic indirect references
|
|
224
|
+
const sourceElement = referencedElement;
|
|
225
|
+
referencedElement = refractReference(referencedElement);
|
|
226
|
+
referencedElement.meta.set('referenced-element', referencedElementType);
|
|
227
|
+
this.refractCache.set(sourceElement, referencedElement);
|
|
228
|
+
} else {
|
|
229
|
+
// handling direct references
|
|
230
|
+
const sourceElement = referencedElement;
|
|
231
|
+
referencedElement = refract(referencedElement, {
|
|
232
|
+
element: referencedElementType
|
|
233
|
+
});
|
|
234
|
+
this.refractCache.set(sourceElement, referencedElement);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// detect direct or circular reference
|
|
239
|
+
if (referencingElement === referencedElement) {
|
|
240
|
+
throw new ApiDOMStructuredError('Recursive Reference Object detected', {
|
|
241
|
+
$ref: toValue(referencingElement.$ref)
|
|
150
242
|
});
|
|
151
|
-
this.refractCache.set(cacheKey, referencedElement);
|
|
152
243
|
}
|
|
153
|
-
}
|
|
154
244
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
245
|
+
// detect maximum depth of dereferencing
|
|
246
|
+
if (this.indirections.length > this.options.dereference.maxDepth) {
|
|
247
|
+
throw new MaximumDereferenceDepthError(`Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`, {
|
|
248
|
+
maxDepth: this.options.dereference.maxDepth,
|
|
249
|
+
uri: this.reference.uri
|
|
250
|
+
});
|
|
251
|
+
}
|
|
159
252
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
253
|
+
// detect second deep dive into the same fragment and avoid it
|
|
254
|
+
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
|
|
255
|
+
if (ancestorsLineage.includes(referencedElement)) {
|
|
256
|
+
reference.refSet.circular = true;
|
|
257
|
+
if (this.options.dereference.circular === 'error') {
|
|
258
|
+
throw new ApiDOMStructuredError('Circular reference detected', {
|
|
259
|
+
$ref: toValue(referencingElement.$ref)
|
|
260
|
+
});
|
|
261
|
+
} else if (this.options.dereference.circular === 'replace') {
|
|
262
|
+
const refElement = new RefElement($refBaseURI, {
|
|
263
|
+
type: referencingElement.element,
|
|
264
|
+
uri: reference.uri,
|
|
265
|
+
$ref: toValue(referencingElement.$ref)
|
|
266
|
+
});
|
|
267
|
+
const replacer = this.options.dereference.strategyOpts['asyncapi-2']?.circularReplacer ?? this.options.dereference.circularReplacer;
|
|
268
|
+
const replacement = replacer(refElement);
|
|
269
|
+
this.indirections.pop();
|
|
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
|
-
|
|
172
|
-
|
|
173
|
-
|
|
300
|
+
// remove referencing reference from ancestors lineage
|
|
301
|
+
directAncestors.delete(referencingElement);
|
|
302
|
+
}
|
|
303
|
+
this.indirections.pop();
|
|
304
|
+
|
|
305
|
+
// Boolean JSON Schemas
|
|
306
|
+
if (isBooleanJSONSchemaElement(referencedElement)) {
|
|
307
|
+
const booleanJsonSchemaElement = cloneDeep(referencedElement);
|
|
308
|
+
// annotate referenced element with info about original referencing element
|
|
309
|
+
booleanJsonSchemaElement.meta.set('ref-fields', {
|
|
174
310
|
$ref: toValue(referencingElement.$ref)
|
|
175
311
|
});
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
path.replaceWith(
|
|
312
|
+
// annotate referenced element with info about origin
|
|
313
|
+
booleanJsonSchemaElement.meta.set('ref-origin', reference.uri);
|
|
314
|
+
booleanJsonSchemaElement.meta.set('ref-type', referencingElement.element);
|
|
315
|
+
path.replaceWith(booleanJsonSchemaElement);
|
|
180
316
|
return;
|
|
181
317
|
}
|
|
182
|
-
}
|
|
183
318
|
|
|
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());
|
|
319
|
+
/**
|
|
320
|
+
* Creating a new version of referenced element to avoid modifying the original one.
|
|
321
|
+
*/
|
|
322
|
+
const mergedElement = cloneShallow(referencedElement);
|
|
220
323
|
// annotate referenced element with info about original referencing element
|
|
221
|
-
|
|
324
|
+
mergedElement.meta.set('ref-fields', {
|
|
222
325
|
$ref: toValue(referencingElement.$ref)
|
|
223
326
|
});
|
|
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));
|
|
327
|
+
// annotate fragment with info about origin
|
|
328
|
+
mergedElement.meta.set('ref-origin', reference.uri);
|
|
329
|
+
mergedElement.meta.set('ref-type', referencingElement.element);
|
|
246
330
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
331
|
+
/**
|
|
332
|
+
* Transclude referencing element with merged referenced element.
|
|
333
|
+
*/
|
|
334
|
+
path.replaceWith(mergedElement);
|
|
335
|
+
} catch (error) {
|
|
336
|
+
const $ref = toValue(referencingElement.$ref);
|
|
337
|
+
this.handleError(`Error while dereferencing Reference Object. Cannot resolve $ref "${$ref}": ${error.message}`, error, referencingElement, '$ref', $ref, path);
|
|
338
|
+
}
|
|
251
339
|
}
|
|
252
340
|
async ChannelItemElement(path) {
|
|
253
341
|
const referencingElement = path.node;
|
|
@@ -262,7 +350,6 @@ class AsyncAPI2DereferenceVisitor {
|
|
|
262
350
|
path.skip();
|
|
263
351
|
return;
|
|
264
352
|
}
|
|
265
|
-
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
|
|
266
353
|
const retrievalURI = this.toBaseURI(toValue(referencingElement.$ref));
|
|
267
354
|
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
|
|
268
355
|
const isExternalReference = !isInternalReference;
|
|
@@ -277,117 +364,123 @@ class AsyncAPI2DereferenceVisitor {
|
|
|
277
364
|
// skip traversing this channel item but traverse all it's child elements
|
|
278
365
|
return;
|
|
279
366
|
}
|
|
280
|
-
const reference = await this.toReference(toValue(referencingElement.$ref));
|
|
281
367
|
const $refBaseURI = url.resolve(retrievalURI, toValue(referencingElement.$ref));
|
|
282
|
-
|
|
283
|
-
|
|
368
|
+
try {
|
|
369
|
+
const reference = await this.toReference(toValue(referencingElement.$ref));
|
|
370
|
+
this.indirections.push(referencingElement);
|
|
371
|
+
const jsonPointer = URIFragmentIdentifier.fromURIReference($refBaseURI);
|
|
284
372
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
referencedElement.id = identityManager.identify(referencedElement);
|
|
373
|
+
// possibly non-semantic referenced element
|
|
374
|
+
let referencedElement = evaluate(reference.value.result, jsonPointer);
|
|
288
375
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
this.refractCache.set(cacheKey, referencedElement);
|
|
376
|
+
// applying semantics to a referenced element
|
|
377
|
+
if (!isChannelItemElement(referencedElement)) {
|
|
378
|
+
if (this.refractCache.has(referencedElement)) {
|
|
379
|
+
referencedElement = this.refractCache.get(referencedElement);
|
|
380
|
+
} else {
|
|
381
|
+
const sourceElement = referencedElement;
|
|
382
|
+
referencedElement = refractChannelItem(referencedElement);
|
|
383
|
+
this.refractCache.set(sourceElement, referencedElement);
|
|
384
|
+
}
|
|
299
385
|
}
|
|
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
386
|
|
|
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,
|
|
387
|
+
// detect direct or indirect reference
|
|
388
|
+
if (referencingElement === referencedElement) {
|
|
389
|
+
throw new ApiDOMStructuredError('Recursive Channel Item Object reference detected', {
|
|
321
390
|
$ref: toValue(referencingElement.$ref)
|
|
322
391
|
});
|
|
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
392
|
}
|
|
329
|
-
}
|
|
330
393
|
|
|
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
|
-
});
|
|
394
|
+
// detect maximum depth of dereferencing
|
|
395
|
+
if (this.indirections.length > this.options.dereference.maxDepth) {
|
|
396
|
+
throw new MaximumDereferenceDepthError(`Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`, {
|
|
397
|
+
maxDepth: this.options.dereference.maxDepth,
|
|
398
|
+
uri: this.reference.uri
|
|
399
|
+
});
|
|
400
|
+
}
|
|
356
401
|
|
|
357
|
-
//
|
|
358
|
-
directAncestors.
|
|
359
|
-
|
|
360
|
-
|
|
402
|
+
// detect second deep dive into the same fragment and avoid it
|
|
403
|
+
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
|
|
404
|
+
if (ancestorsLineage.includes(referencedElement)) {
|
|
405
|
+
reference.refSet.circular = true;
|
|
406
|
+
if (this.options.dereference.circular === 'error') {
|
|
407
|
+
throw new ApiDOMStructuredError('Circular reference detected', {
|
|
408
|
+
$ref: toValue(referencingElement.$ref)
|
|
409
|
+
});
|
|
410
|
+
} else if (this.options.dereference.circular === 'replace') {
|
|
411
|
+
const refElement = new RefElement($refBaseURI, {
|
|
412
|
+
type: referencingElement.element,
|
|
413
|
+
uri: reference.uri,
|
|
414
|
+
$ref: toValue(referencingElement.$ref)
|
|
415
|
+
});
|
|
416
|
+
const replacer = this.options.dereference.strategyOpts['asyncapi-2']?.circularReplacer ?? this.options.dereference.circularReplacer;
|
|
417
|
+
const replacement = replacer(refElement);
|
|
418
|
+
this.indirections.pop();
|
|
419
|
+
path.replaceWith(replacement);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
361
423
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
424
|
+
/**
|
|
425
|
+
* Dive deep into the fragment.
|
|
426
|
+
*
|
|
427
|
+
* Cases to consider:
|
|
428
|
+
* 1. We're crossing document boundary
|
|
429
|
+
* 2. Fragment is from non-entry document
|
|
430
|
+
* 3. Fragment is a Channel Item Object with $ref field. We need to follow it to get the eventual value
|
|
431
|
+
* 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
|
|
432
|
+
*/
|
|
433
|
+
const isNonEntryDocument = url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
|
|
434
|
+
const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
|
|
435
|
+
if ((isExternalReference || isNonEntryDocument || isChannelItemElement(referencedElement) && isStringElement(referencedElement.$ref) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
|
|
436
|
+
// append referencing reference to ancestors lineage
|
|
437
|
+
directAncestors.add(referencingElement);
|
|
438
|
+
const visitor = new AsyncAPI2DereferenceVisitor({
|
|
439
|
+
reference,
|
|
440
|
+
indirections: [...this.indirections],
|
|
441
|
+
options: this.options,
|
|
442
|
+
refractCache: this.refractCache,
|
|
443
|
+
ancestors: ancestorsLineage
|
|
444
|
+
});
|
|
445
|
+
referencedElement = await traverseAsync(referencedElement, visitor, {
|
|
446
|
+
mutable: true
|
|
447
|
+
});
|
|
375
448
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
referencedElement
|
|
385
|
-
|
|
449
|
+
// remove referencing reference from ancestors lineage
|
|
450
|
+
directAncestors.delete(referencingElement);
|
|
451
|
+
}
|
|
452
|
+
this.indirections.pop();
|
|
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
|
+
}
|
|
391
484
|
}
|
|
392
485
|
}
|
|
393
486
|
export default AsyncAPI2DereferenceVisitor;
|