@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 _apidomNsOpenapi = require("@speclynx/apidom-ns-openapi-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,25 +27,34 @@ const identityManager = new _apidomCore.IdentityManager();
|
|
|
29
27
|
*/
|
|
30
28
|
class OpenAPI2DereferenceVisitor {
|
|
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
|
-
this.ancestors = new _util.AncestorLineage(...ancestors);
|
|
50
50
|
this.refractCache = refractCache;
|
|
51
|
+
this.ancestors = new _util.AncestorLineage(...ancestors);
|
|
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];
|
|
51
58
|
}
|
|
52
59
|
toBaseURI(uri) {
|
|
53
60
|
return url.resolve(this.reference.uri, url.sanitize(url.stripHash(uri)));
|
|
@@ -55,7 +62,10 @@ class OpenAPI2DereferenceVisitor {
|
|
|
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 OpenAPI2DereferenceVisitor {
|
|
|
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 OpenAPI2DereferenceVisitor {
|
|
|
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 OpenAPI2DereferenceVisitor {
|
|
|
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,118 +212,122 @@ class OpenAPI2DereferenceVisitor {
|
|
|
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, _apidomNsOpenapi.isReferenceElement)(referencedElement)) {
|
|
228
|
+
if (this.refractCache.has(referencedElement)) {
|
|
229
|
+
referencedElement = this.refractCache.get(referencedElement);
|
|
230
|
+
} else if ((0, _apidomNsOpenapi.isReferenceLikeElement)(referencedElement)) {
|
|
231
|
+
// handling generic indirect references
|
|
232
|
+
const sourceElement = referencedElement;
|
|
233
|
+
referencedElement = (0, _apidomNsOpenapi.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, _apidomNsOpenapi.refract)(referencedElement, {
|
|
240
|
+
element: referencedElementType
|
|
241
|
+
});
|
|
242
|
+
this.refractCache.set(sourceElement, referencedElement);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// detect direct or indirect 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 cross-boundary cycle
|
|
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['openapi-2']?.circularReplacer ?? this.options.dereference.circularReplacer;
|
|
276
|
+
const replacement = replacer(refElement);
|
|
277
|
+
path.replaceWith(replacement);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
170
281
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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, _apidomNsOpenapi.isReferenceElement)(referencedElement) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
|
|
294
|
+
// append referencing reference to ancestors lineage
|
|
295
|
+
directAncestors.add(referencingElement);
|
|
296
|
+
const visitor = new OpenAPI2DereferenceVisitor({
|
|
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
|
|
181
305
|
});
|
|
182
|
-
|
|
183
|
-
const replacement = replacer(refElement);
|
|
184
|
-
this.indirections.pop();
|
|
185
|
-
path.replaceWith(replacement);
|
|
186
|
-
return;
|
|
306
|
+
directAncestors.delete(referencingElement);
|
|
187
307
|
}
|
|
188
|
-
}
|
|
189
308
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
* 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
|
|
198
|
-
*/
|
|
199
|
-
const isNonEntryDocument = url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
|
|
200
|
-
const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
|
|
201
|
-
if ((isExternalReference || isNonEntryDocument || (0, _apidomNsOpenapi.isReferenceElement)(referencedElement) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
|
|
202
|
-
// append referencing reference to ancestors lineage
|
|
203
|
-
directAncestors.add(referencingElement);
|
|
204
|
-
const visitor = new OpenAPI2DereferenceVisitor({
|
|
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
|
|
309
|
+
/**
|
|
310
|
+
* Creating a new version of referenced element to avoid modifying the original one.
|
|
311
|
+
*/
|
|
312
|
+
const mergedElement = (0, _apidomDatamodel.cloneShallow)(referencedElement);
|
|
313
|
+
// annotate referenced element with info about original referencing element
|
|
314
|
+
mergedElement.meta.set('ref-fields', {
|
|
315
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
214
316
|
});
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
317
|
+
// annotate fragment with info about origin
|
|
318
|
+
mergedElement.meta.set('ref-origin', reference.uri);
|
|
319
|
+
mergedElement.meta.set('ref-type', referencingElement.element);
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Transclude referencing element with merged referenced element.
|
|
323
|
+
*/
|
|
324
|
+
path.replaceWith(mergedElement);
|
|
325
|
+
} catch (error) {
|
|
326
|
+
const $ref = (0, _apidomCore.toValue)(referencingElement.$ref);
|
|
327
|
+
this.handleError(`Error while dereferencing Reference Object. Cannot resolve $ref "${$ref}": ${error.message}`, error, referencingElement, '$ref', $ref, path);
|
|
328
|
+
} finally {
|
|
329
|
+
if (this.indirections.length > indirectionsSize) this.indirections.pop();
|
|
218
330
|
}
|
|
219
|
-
this.indirections.pop();
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Creating a new version of referenced element to avoid modifying the original one.
|
|
223
|
-
*/
|
|
224
|
-
const mergedElement = (0, _apidomDatamodel.cloneShallow)(referencedElement);
|
|
225
|
-
// assign unique id to merged element
|
|
226
|
-
mergedElement.meta.set('id', identityManager.generateId());
|
|
227
|
-
// annotate referenced element with info about original referencing element
|
|
228
|
-
mergedElement.meta.set('ref-fields', {
|
|
229
|
-
// @ts-ignore
|
|
230
|
-
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
231
|
-
});
|
|
232
|
-
// annotate fragment with info about origin
|
|
233
|
-
mergedElement.meta.set('ref-origin', reference.uri);
|
|
234
|
-
// annotate fragment with info about referencing element
|
|
235
|
-
mergedElement.meta.set('ref-referencing-element-id', identityManager.identify(referencingElement));
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Transclude referencing element with merged referenced element.
|
|
239
|
-
*/
|
|
240
|
-
path.replaceWith(mergedElement);
|
|
241
|
-
return;
|
|
242
331
|
}
|
|
243
332
|
async PathItemElement(path) {
|
|
244
333
|
const referencingElement = path.node;
|
|
@@ -253,7 +342,6 @@ class OpenAPI2DereferenceVisitor {
|
|
|
253
342
|
path.skip();
|
|
254
343
|
return;
|
|
255
344
|
}
|
|
256
|
-
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
|
|
257
345
|
const retrievalURI = this.toBaseURI((0, _apidomCore.toValue)(referencingElement.$ref));
|
|
258
346
|
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
|
|
259
347
|
const isExternalReference = !isInternalReference;
|
|
@@ -268,116 +356,125 @@ class OpenAPI2DereferenceVisitor {
|
|
|
268
356
|
// skip traversing this Path Item element but traverse all it's child elements
|
|
269
357
|
return;
|
|
270
358
|
}
|
|
271
|
-
const reference = await this.toReference((0, _apidomCore.toValue)(referencingElement.$ref));
|
|
272
359
|
const $refBaseURI = url.resolve(retrievalURI, (0, _apidomCore.toValue)(referencingElement.$ref));
|
|
273
|
-
this.indirections.
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
360
|
+
const indirectionsSize = this.indirections.length;
|
|
361
|
+
try {
|
|
362
|
+
const reference = await this.toReference((0, _apidomCore.toValue)(referencingElement.$ref));
|
|
363
|
+
this.indirections.push(referencingElement);
|
|
364
|
+
const jsonPointer = _apidomJsonPointer.URIFragmentIdentifier.fromURIReference($refBaseURI);
|
|
365
|
+
|
|
366
|
+
// possibly non-semantic referenced element
|
|
367
|
+
let referencedElement = (0, _apidomJsonPointer.evaluate)(reference.value.result, jsonPointer);
|
|
368
|
+
|
|
369
|
+
// applying semantics to a referenced element
|
|
370
|
+
if (!(0, _apidomNsOpenapi.isPathItemElement)(referencedElement)) {
|
|
371
|
+
if (this.refractCache.has(referencedElement)) {
|
|
372
|
+
referencedElement = this.refractCache.get(referencedElement);
|
|
373
|
+
} else {
|
|
374
|
+
const sourceElement = referencedElement;
|
|
375
|
+
referencedElement = (0, _apidomNsOpenapi.refractPathItem)(referencedElement);
|
|
376
|
+
this.refractCache.set(sourceElement, referencedElement);
|
|
377
|
+
}
|
|
290
378
|
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// detect direct or indirect reference
|
|
294
|
-
if (referencingElement === referencedElement) {
|
|
295
|
-
throw new _apidomError.ApiDOMError('Recursive Path Item Object reference detected');
|
|
296
|
-
}
|
|
297
379
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// detect second deep dive into the same fragment and avoid it
|
|
304
|
-
if (ancestorsLineage.includes(referencedElement)) {
|
|
305
|
-
reference.refSet.circular = true;
|
|
306
|
-
if (this.options.dereference.circular === 'error') {
|
|
307
|
-
throw new _apidomError.ApiDOMError('Circular reference detected');
|
|
308
|
-
} else if (this.options.dereference.circular === 'replace') {
|
|
309
|
-
const refElement = new _apidomDatamodel.RefElement(referencedElement.id, {
|
|
310
|
-
type: 'path-item',
|
|
311
|
-
uri: reference.uri,
|
|
380
|
+
// detect direct or indirect reference
|
|
381
|
+
if (referencingElement === referencedElement) {
|
|
382
|
+
throw new _apidomError.ApiDOMStructuredError('Recursive Path Item Object reference detected', {
|
|
312
383
|
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
313
384
|
});
|
|
314
|
-
const replacer = this.options.dereference.strategyOpts['openapi-2']?.circularReplacer ?? this.options.dereference.circularReplacer;
|
|
315
|
-
const replacement = replacer(refElement);
|
|
316
|
-
this.indirections.pop();
|
|
317
|
-
path.replaceWith(replacement);
|
|
318
|
-
return;
|
|
319
385
|
}
|
|
320
|
-
}
|
|
321
386
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
* 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
|
|
330
|
-
*/
|
|
331
|
-
const isNonEntryDocument = url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
|
|
332
|
-
const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
|
|
333
|
-
if ((isExternalReference || isNonEntryDocument || (0, _apidomNsOpenapi.isPathItemElement)(referencedElement) && (0, _apidomDatamodel.isStringElement)(referencedElement.$ref) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
|
|
334
|
-
// append referencing reference to ancestors lineage
|
|
335
|
-
directAncestors.add(referencingElement);
|
|
336
|
-
const visitor = new OpenAPI2DereferenceVisitor({
|
|
337
|
-
reference,
|
|
338
|
-
namespace: this.namespace,
|
|
339
|
-
indirections: [...this.indirections],
|
|
340
|
-
options: this.options,
|
|
341
|
-
refractCache: this.refractCache,
|
|
342
|
-
ancestors: ancestorsLineage
|
|
343
|
-
});
|
|
344
|
-
referencedElement = await (0, _apidomTraverse.traverseAsync)(referencedElement, visitor, {
|
|
345
|
-
mutable: true
|
|
346
|
-
});
|
|
387
|
+
// detect maximum depth of dereferencing
|
|
388
|
+
if (this.indirections.length > this.options.dereference.maxDepth) {
|
|
389
|
+
throw new _MaximumDereferenceDepthError.default(`Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`, {
|
|
390
|
+
maxDepth: this.options.dereference.maxDepth,
|
|
391
|
+
uri: this.reference.uri
|
|
392
|
+
});
|
|
393
|
+
}
|
|
347
394
|
|
|
348
|
-
//
|
|
349
|
-
directAncestors.
|
|
350
|
-
|
|
351
|
-
|
|
395
|
+
// detect cross-boundary cycle
|
|
396
|
+
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
|
|
397
|
+
if (ancestorsLineage.includes(referencedElement)) {
|
|
398
|
+
reference.refSet.circular = true;
|
|
399
|
+
if (this.options.dereference.circular === 'error') {
|
|
400
|
+
throw new _apidomError.ApiDOMStructuredError('Circular reference detected', {
|
|
401
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
402
|
+
});
|
|
403
|
+
} else if (this.options.dereference.circular === 'replace') {
|
|
404
|
+
const refElement = new _apidomDatamodel.RefElement($refBaseURI, {
|
|
405
|
+
type: referencingElement.element,
|
|
406
|
+
uri: reference.uri,
|
|
407
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
408
|
+
});
|
|
409
|
+
const replacer = this.options.dereference.strategyOpts['openapi-2']?.circularReplacer ?? this.options.dereference.circularReplacer;
|
|
410
|
+
const replacement = replacer(refElement);
|
|
411
|
+
path.replaceWith(replacement);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
352
415
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
416
|
+
/**
|
|
417
|
+
* Dive deep into the fragment.
|
|
418
|
+
*
|
|
419
|
+
* Cases to consider:
|
|
420
|
+
* 1. We're crossing document boundary
|
|
421
|
+
* 2. Fragment is from non-entry document
|
|
422
|
+
* 3. Fragment is a Path Item Object with $ref field. We need to follow it to get the eventual value
|
|
423
|
+
* 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
|
|
424
|
+
*/
|
|
425
|
+
const isNonEntryDocument = url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
|
|
426
|
+
const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
|
|
427
|
+
if ((isExternalReference || isNonEntryDocument || (0, _apidomNsOpenapi.isPathItemElement)(referencedElement) && (0, _apidomDatamodel.isStringElement)(referencedElement.$ref) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
|
|
428
|
+
// append referencing reference to ancestors lineage
|
|
429
|
+
directAncestors.add(referencingElement);
|
|
430
|
+
const visitor = new OpenAPI2DereferenceVisitor({
|
|
431
|
+
reference,
|
|
432
|
+
indirections: [...this.indirections],
|
|
433
|
+
options: this.options,
|
|
434
|
+
refractCache: this.refractCache,
|
|
435
|
+
ancestors: ancestorsLineage
|
|
436
|
+
});
|
|
437
|
+
referencedElement = await (0, _apidomTraverse.traverseAsync)(referencedElement, visitor, {
|
|
438
|
+
mutable: true
|
|
439
|
+
});
|
|
364
440
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
441
|
+
// remove referencing reference from ancestors lineage
|
|
442
|
+
directAncestors.delete(referencingElement);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Creating a new version of Path Item by merging fields from referenced Path Item with referencing one.
|
|
447
|
+
*/
|
|
448
|
+
if ((0, _apidomNsOpenapi.isPathItemElement)(referencedElement)) {
|
|
449
|
+
const mergedElement = (0, _apidomDatamodel.cloneShallow)(referencedElement);
|
|
450
|
+
// existing keywords from referencing PathItemElement overrides ones from referenced element
|
|
451
|
+
referencingElement.forEach((value, keyElement, item) => {
|
|
452
|
+
mergedElement.remove((0, _apidomCore.toValue)(keyElement));
|
|
453
|
+
mergedElement.content.push(item);
|
|
454
|
+
});
|
|
455
|
+
mergedElement.remove('$ref');
|
|
456
|
+
|
|
457
|
+
// annotate referenced element with info about original referencing element
|
|
458
|
+
mergedElement.meta.set('ref-fields', {
|
|
459
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
460
|
+
});
|
|
461
|
+
// annotate referenced element with info about origin and type
|
|
462
|
+
mergedElement.meta.set('ref-origin', reference.uri);
|
|
463
|
+
mergedElement.meta.set('ref-type', referencingElement.element);
|
|
464
|
+
referencedElement = mergedElement;
|
|
465
|
+
}
|
|
375
466
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
467
|
+
/**
|
|
468
|
+
* Transclude referencing element with merged referenced element.
|
|
469
|
+
*/
|
|
470
|
+
path.replaceWith(referencedElement);
|
|
471
|
+
return;
|
|
472
|
+
} catch (error) {
|
|
473
|
+
const $ref = (0, _apidomCore.toValue)(referencingElement.$ref);
|
|
474
|
+
this.handleError(`Error while dereferencing Path Item Object. Cannot resolve $ref "${$ref}": ${error.message}`, error, referencingElement, '$ref', $ref, path);
|
|
475
|
+
} finally {
|
|
476
|
+
if (this.indirections.length > indirectionsSize) this.indirections.pop();
|
|
477
|
+
}
|
|
381
478
|
}
|
|
382
479
|
async JSONReferenceElement(path) {
|
|
383
480
|
const referencingElement = path.node;
|
|
@@ -387,7 +484,6 @@ class OpenAPI2DereferenceVisitor {
|
|
|
387
484
|
path.skip();
|
|
388
485
|
return;
|
|
389
486
|
}
|
|
390
|
-
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
|
|
391
487
|
const retrievalURI = this.toBaseURI((0, _apidomCore.toValue)(referencingElement.$ref));
|
|
392
488
|
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
|
|
393
489
|
const isExternalReference = !isInternalReference;
|
|
@@ -404,118 +500,124 @@ class OpenAPI2DereferenceVisitor {
|
|
|
404
500
|
path.skip();
|
|
405
501
|
return;
|
|
406
502
|
}
|
|
407
|
-
const reference = await this.toReference((0, _apidomCore.toValue)(referencingElement.$ref));
|
|
408
503
|
const $refBaseURI = url.resolve(retrievalURI, (0, _apidomCore.toValue)(referencingElement.$ref));
|
|
409
|
-
this.indirections.
|
|
410
|
-
|
|
504
|
+
const indirectionsSize = this.indirections.length;
|
|
505
|
+
try {
|
|
506
|
+
const reference = await this.toReference((0, _apidomCore.toValue)(referencingElement.$ref));
|
|
507
|
+
this.indirections.push(referencingElement);
|
|
508
|
+
const jsonPointer = _apidomJsonPointer.URIFragmentIdentifier.fromURIReference($refBaseURI);
|
|
411
509
|
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
referencedElement.id = identityManager.identify(referencedElement);
|
|
510
|
+
// possibly non-semantic fragment
|
|
511
|
+
let referencedElement = (0, _apidomJsonPointer.evaluate)(reference.value.result, jsonPointer);
|
|
415
512
|
|
|
416
|
-
|
|
417
|
-
* Applying semantics to a referenced element if semantics are missing.
|
|
418
|
-
*/
|
|
419
|
-
if ((0, _apidomDatamodel.isPrimitiveElement)(referencedElement)) {
|
|
513
|
+
// applying semantics to a fragment
|
|
420
514
|
const referencedElementType = referencingElement.meta.get('referenced-element');
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
515
|
+
if (referencedElement.element !== referencedElementType && !(0, _apidomNsOpenapi.isJSONReferenceElement)(referencedElement)) {
|
|
516
|
+
if (this.refractCache.has(referencedElement)) {
|
|
517
|
+
referencedElement = this.refractCache.get(referencedElement);
|
|
518
|
+
} else if ((0, _apidomNsOpenapi.isJSONReferenceLikeElement)(referencedElement)) {
|
|
519
|
+
// handling generic indirect references
|
|
520
|
+
const sourceElement = referencedElement;
|
|
521
|
+
referencedElement = (0, _apidomNsOpenapi.refractJSONReference)(referencedElement);
|
|
522
|
+
referencedElement.meta.set('referenced-element', referencedElementType);
|
|
523
|
+
this.refractCache.set(sourceElement, referencedElement);
|
|
524
|
+
} else {
|
|
525
|
+
// handling direct references
|
|
526
|
+
const sourceElement = referencedElement;
|
|
527
|
+
referencedElement = (0, _apidomNsOpenapi.refract)(referencedElement, {
|
|
528
|
+
element: referencedElementType
|
|
529
|
+
});
|
|
530
|
+
this.refractCache.set(sourceElement, referencedElement);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// detect direct or indirect reference
|
|
535
|
+
if (referencingElement === referencedElement) {
|
|
536
|
+
throw new _apidomError.ApiDOMStructuredError('Recursive JSON Reference Object detected', {
|
|
537
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
433
538
|
});
|
|
434
|
-
this.refractCache.set(cacheKey, referencedElement);
|
|
435
539
|
}
|
|
436
|
-
}
|
|
437
540
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
541
|
+
// detect maximum depth of dereferencing
|
|
542
|
+
if (this.indirections.length > this.options.dereference.maxDepth) {
|
|
543
|
+
throw new _MaximumDereferenceDepthError.default(`Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`, {
|
|
544
|
+
maxDepth: this.options.dereference.maxDepth,
|
|
545
|
+
uri: this.reference.uri
|
|
546
|
+
});
|
|
547
|
+
}
|
|
442
548
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
549
|
+
// detect cross-boundary cycle
|
|
550
|
+
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(path);
|
|
551
|
+
if (ancestorsLineage.includes(referencedElement)) {
|
|
552
|
+
reference.refSet.circular = true;
|
|
553
|
+
if (this.options.dereference.circular === 'error') {
|
|
554
|
+
throw new _apidomError.ApiDOMStructuredError('Circular reference detected', {
|
|
555
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
556
|
+
});
|
|
557
|
+
} else if (this.options.dereference.circular === 'replace') {
|
|
558
|
+
const refElement = new _apidomDatamodel.RefElement($refBaseURI, {
|
|
559
|
+
type: referencingElement.element,
|
|
560
|
+
uri: reference.uri,
|
|
561
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
562
|
+
});
|
|
563
|
+
const replacer = this.options.dereference.strategyOpts['openapi-2']?.circularReplacer ?? this.options.dereference.circularReplacer;
|
|
564
|
+
const replacement = replacer(refElement);
|
|
565
|
+
path.replaceWith(replacement);
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
447
569
|
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
570
|
+
/**
|
|
571
|
+
* Dive deep into the fragment.
|
|
572
|
+
*
|
|
573
|
+
* Cases to consider:
|
|
574
|
+
* 1. We're crossing document boundary
|
|
575
|
+
2. Fragment is from non-entry document
|
|
576
|
+
* 3. Fragment is a JSON Reference Object. We need to follow it to get the eventual value
|
|
577
|
+
* 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
|
|
578
|
+
*/
|
|
579
|
+
const isNonEntryDocument = url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
|
|
580
|
+
const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
|
|
581
|
+
if ((isExternalReference || isNonEntryDocument || (0, _apidomNsOpenapi.isJSONReferenceElement)(referencedElement) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
|
|
582
|
+
// append referencing reference to ancestors lineage
|
|
583
|
+
directAncestors.add(referencingElement);
|
|
584
|
+
const visitor = new OpenAPI2DereferenceVisitor({
|
|
585
|
+
reference,
|
|
586
|
+
indirections: [...this.indirections],
|
|
587
|
+
options: this.options,
|
|
588
|
+
refractCache: this.refractCache,
|
|
589
|
+
ancestors: ancestorsLineage
|
|
458
590
|
});
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
591
|
+
referencedElement = await (0, _apidomTraverse.traverseAsync)(referencedElement, visitor, {
|
|
592
|
+
mutable: true
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
// remove referencing reference from ancestors lineage
|
|
596
|
+
directAncestors.delete(referencingElement);
|
|
464
597
|
}
|
|
465
|
-
}
|
|
466
598
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
* 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
|
|
475
|
-
*/
|
|
476
|
-
const isNonEntryDocument = url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
|
|
477
|
-
const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
|
|
478
|
-
if ((isExternalReference || isNonEntryDocument || (0, _apidomNsOpenapi.isJSONReferenceElement)(referencedElement) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
|
|
479
|
-
// append referencing reference to ancestors lineage
|
|
480
|
-
directAncestors.add(referencingElement);
|
|
481
|
-
const visitor = new OpenAPI2DereferenceVisitor({
|
|
482
|
-
reference,
|
|
483
|
-
namespace: this.namespace,
|
|
484
|
-
indirections: [...this.indirections],
|
|
485
|
-
options: this.options,
|
|
486
|
-
refractCache: this.refractCache,
|
|
487
|
-
ancestors: ancestorsLineage
|
|
488
|
-
});
|
|
489
|
-
referencedElement = await (0, _apidomTraverse.traverseAsync)(referencedElement, visitor, {
|
|
490
|
-
mutable: true
|
|
599
|
+
/**
|
|
600
|
+
* Creating a new version of referenced element to avoid modifying the original one.
|
|
601
|
+
*/
|
|
602
|
+
const mergedElement = (0, _apidomDatamodel.cloneShallow)(referencedElement);
|
|
603
|
+
// annotate referenced element with info about original referencing element
|
|
604
|
+
mergedElement.meta.set('ref-fields', {
|
|
605
|
+
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
491
606
|
});
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
607
|
+
// annotate fragment with info about origin
|
|
608
|
+
mergedElement.meta.set('ref-origin', reference.uri);
|
|
609
|
+
mergedElement.meta.set('ref-type', referencingElement.element);
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Transclude referencing element with merged referenced element.
|
|
613
|
+
*/
|
|
614
|
+
path.replaceWith(mergedElement);
|
|
615
|
+
} catch (error) {
|
|
616
|
+
const $ref = (0, _apidomCore.toValue)(referencingElement.$ref);
|
|
617
|
+
this.handleError(`Error while dereferencing JSON Reference Object. Cannot resolve $ref "${$ref}": ${error.message}`, error, referencingElement, '$ref', $ref, path);
|
|
618
|
+
} finally {
|
|
619
|
+
if (this.indirections.length > indirectionsSize) this.indirections.pop();
|
|
495
620
|
}
|
|
496
|
-
this.indirections.pop();
|
|
497
|
-
|
|
498
|
-
/**
|
|
499
|
-
* Creating a new version of referenced element to avoid modifying the original one.
|
|
500
|
-
*/
|
|
501
|
-
const mergedElement = (0, _apidomDatamodel.cloneShallow)(referencedElement);
|
|
502
|
-
// assign unique id to merged element
|
|
503
|
-
mergedElement.meta.set('id', identityManager.generateId());
|
|
504
|
-
// annotate referenced element with info about original referencing element
|
|
505
|
-
mergedElement.meta.set('ref-fields', {
|
|
506
|
-
// @ts-ignore
|
|
507
|
-
$ref: (0, _apidomCore.toValue)(referencingElement.$ref)
|
|
508
|
-
});
|
|
509
|
-
// annotate fragment with info about origin
|
|
510
|
-
mergedElement.meta.set('ref-origin', reference.uri);
|
|
511
|
-
// annotate fragment with info about referencing element
|
|
512
|
-
mergedElement.meta.set('ref-referencing-element-id', identityManager.identify(referencingElement));
|
|
513
|
-
|
|
514
|
-
/**
|
|
515
|
-
* Transclude referencing element with merged referenced element.
|
|
516
|
-
*/
|
|
517
|
-
path.replaceWith(mergedElement);
|
|
518
|
-
return;
|
|
519
621
|
}
|
|
520
622
|
}
|
|
521
623
|
var _default = exports.default = OpenAPI2DereferenceVisitor;
|