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