swagger-client 3.36.2 → 3.37.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.
Files changed (37) hide show
  1. package/README.md +8 -7
  2. package/dist/swagger-client.browser.js +29056 -21099
  3. package/dist/swagger-client.browser.min.js +1 -1
  4. package/dist/swagger-client.browser.min.js.map +1 -1
  5. package/es/helpers/openapi-predicates.js +11 -1
  6. package/es/index.js +12 -4
  7. package/es/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/errors/SchemaRefError.js +3 -0
  8. package/es/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/index.js +99 -0
  9. package/es/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/utils/get-root-cause.js +17 -0
  10. package/es/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/utils/to-path.js +23 -0
  11. package/es/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/visitors/all-of.js +103 -0
  12. package/es/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/visitors/dereference.js +677 -0
  13. package/es/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/visitors/parameters.js +40 -0
  14. package/es/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/visitors/properties.js +34 -0
  15. package/es/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/visitors/root.js +45 -0
  16. package/es/resolver/apidom/reference/parse/parsers/openapi-json-3-2/index.js +55 -0
  17. package/es/resolver/apidom/reference/parse/parsers/openapi-yaml-3-2/index.js +56 -0
  18. package/es/resolver/strategies/openapi-3-2-apidom/index.js +28 -0
  19. package/es/resolver/strategies/openapi-3-2-apidom/normalize.js +44 -0
  20. package/es/resolver/strategies/openapi-3-2-apidom/resolve.js +161 -0
  21. package/lib/helpers/openapi-predicates.js +13 -2
  22. package/lib/index.js +29 -21
  23. package/lib/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/errors/SchemaRefError.js +7 -0
  24. package/lib/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/index.js +106 -0
  25. package/lib/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/utils/get-root-cause.js +21 -0
  26. package/lib/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/utils/to-path.js +27 -0
  27. package/lib/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/visitors/all-of.js +108 -0
  28. package/lib/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/visitors/dereference.js +683 -0
  29. package/lib/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/visitors/parameters.js +45 -0
  30. package/lib/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/visitors/properties.js +39 -0
  31. package/lib/resolver/apidom/reference/dereference/strategies/openapi-3-2-swagger-client/visitors/root.js +51 -0
  32. package/lib/resolver/apidom/reference/parse/parsers/openapi-json-3-2/index.js +60 -0
  33. package/lib/resolver/apidom/reference/parse/parsers/openapi-yaml-3-2/index.js +62 -0
  34. package/lib/resolver/strategies/openapi-3-2-apidom/index.js +34 -0
  35. package/lib/resolver/strategies/openapi-3-2-apidom/normalize.js +51 -0
  36. package/lib/resolver/strategies/openapi-3-2-apidom/resolve.js +169 -0
  37. package/package.json +12 -9
@@ -0,0 +1,683 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
+ exports.__esModule = true;
5
+ exports.default = void 0;
6
+ var _apidomCore = require("@swagger-api/apidom-core");
7
+ var _apidomError = require("@swagger-api/apidom-error");
8
+ var _apidomNsOpenapi = require("@swagger-api/apidom-ns-openapi-3-2");
9
+ var _modern = require("@swagger-api/apidom-json-pointer/modern");
10
+ var _empty = require("@swagger-api/apidom-reference/configuration/empty");
11
+ var _openapi = require("@swagger-api/apidom-reference/dereference/strategies/openapi-3-2");
12
+ var _$anchor = require("@swagger-api/apidom-reference/dereference/strategies/openapi-3-2/selectors/$anchor");
13
+ var _uri = require("@swagger-api/apidom-reference/dereference/strategies/openapi-3-2/selectors/uri");
14
+ var _toPath = _interopRequireDefault(require("../utils/to-path.js"));
15
+ var _getRootCause = _interopRequireDefault(require("../utils/get-root-cause.js"));
16
+ var _refs = _interopRequireDefault(require("../../../../../../specmap/lib/refs.js"));
17
+ var _SchemaRefError = _interopRequireDefault(require("../errors/SchemaRefError.js"));
18
+ /* eslint-disable camelcase */
19
+
20
+ const {
21
+ wrapError
22
+ } = _refs.default;
23
+ const visitAsync = _apidomCore.visit[Symbol.for('nodejs.util.promisify.custom')];
24
+
25
+ // initialize element identity manager
26
+ const identityManager = new _apidomCore.IdentityManager();
27
+
28
+ // custom mutation replacer
29
+ const mutationReplacer = (newElement, oldElement, key, parent) => {
30
+ if ((0, _apidomCore.isMemberElement)(parent)) {
31
+ parent.value = newElement; // eslint-disable-line no-param-reassign
32
+ } else if (Array.isArray(parent)) {
33
+ parent[key] = newElement; // eslint-disable-line no-param-reassign
34
+ }
35
+ };
36
+ class OpenAPI3_2SwaggerClientDereferenceVisitor extends _openapi.OpenAPI3_2DereferenceVisitor {
37
+ useCircularStructures;
38
+ allowMetaPatches;
39
+ basePath;
40
+ constructor({
41
+ allowMetaPatches = true,
42
+ useCircularStructures = false,
43
+ basePath = null,
44
+ ...rest
45
+ }) {
46
+ super(rest);
47
+ this.allowMetaPatches = allowMetaPatches;
48
+ this.useCircularStructures = useCircularStructures;
49
+ this.basePath = basePath;
50
+ }
51
+ async ReferenceElement(referencingElement, key, parent, path, ancestors, link) {
52
+ try {
53
+ // skip current referencing element as it's already been access
54
+ if (this.indirections.includes(referencingElement)) {
55
+ return false;
56
+ }
57
+ const [ancestorsLineage, directAncestors] = this.toAncestorLineage([...ancestors, parent]);
58
+ const retrievalURI = this.toBaseURI((0, _apidomCore.toValue)(referencingElement.$ref));
59
+ const isInternalReference = _empty.url.stripHash(this.reference.uri) === retrievalURI;
60
+ const isExternalReference = !isInternalReference;
61
+
62
+ // ignore resolving internal Reference Objects
63
+ if (!this.options.resolve.internal && isInternalReference) {
64
+ return false;
65
+ }
66
+ // ignore resolving external Reference Objects
67
+ if (!this.options.resolve.external && isExternalReference) {
68
+ return false;
69
+ }
70
+ const reference = await this.toReference((0, _apidomCore.toValue)(referencingElement.$ref));
71
+ const $refBaseURI = _empty.url.resolve(retrievalURI, (0, _apidomCore.toValue)(referencingElement.$ref));
72
+ this.indirections.push(referencingElement);
73
+ const jsonPointer = _modern.URIFragmentIdentifier.fromURIReference($refBaseURI);
74
+
75
+ // possibly non-semantic fragment
76
+ let referencedElement = (0, _modern.evaluate)(reference.value.result, jsonPointer);
77
+ referencedElement.id = identityManager.identify(referencedElement);
78
+
79
+ // applying semantics to a fragment
80
+ if ((0, _apidomCore.isPrimitiveElement)(referencedElement)) {
81
+ const referencedElementType = (0, _apidomCore.toValue)(referencingElement.meta.get('referenced-element'));
82
+ const cacheKey = `${referencedElementType}-${(0, _apidomCore.toValue)(identityManager.identify(referencedElement))}`;
83
+ if (this.refractCache.has(cacheKey)) {
84
+ referencedElement = this.refractCache.get(cacheKey);
85
+ } else if ((0, _apidomNsOpenapi.isReferenceLikeElement)(referencedElement)) {
86
+ // handling indirect references
87
+ referencedElement = _apidomNsOpenapi.ReferenceElement.refract(referencedElement);
88
+ referencedElement.setMetaProperty('referenced-element', referencedElementType);
89
+ this.refractCache.set(cacheKey, referencedElement);
90
+ } else {
91
+ // handling direct references
92
+ const ElementClass = this.namespace.getElementClass(referencedElementType);
93
+ referencedElement = ElementClass.refract(referencedElement);
94
+ this.refractCache.set(cacheKey, referencedElement);
95
+ }
96
+ }
97
+
98
+ // detect direct or indirect reference
99
+ if (referencingElement === referencedElement) {
100
+ throw new _apidomError.ApiDOMError('Recursive Reference Object detected');
101
+ }
102
+
103
+ // detect maximum depth of dereferencing
104
+ if (this.indirections.length > this.options.dereference.maxDepth) {
105
+ throw new _empty.MaximumDereferenceDepthError(`Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`);
106
+ }
107
+
108
+ // detect second deep dive into the same fragment and avoid it
109
+ if (ancestorsLineage.includes(referencedElement)) {
110
+ reference.refSet.circular = true;
111
+ if (this.options.dereference.circular === 'error') {
112
+ throw new _apidomError.ApiDOMError('Circular reference detected');
113
+ } else if (this.options.dereference.circular === 'replace') {
114
+ var _this$options$derefer, _this$options$derefer2;
115
+ const refElement = new _apidomCore.RefElement(referencedElement.id, {
116
+ type: 'reference',
117
+ uri: reference.uri,
118
+ $ref: (0, _apidomCore.toValue)(referencingElement.$ref),
119
+ baseURI: $refBaseURI,
120
+ referencingElement
121
+ });
122
+ const replacer = (_this$options$derefer = (_this$options$derefer2 = this.options.dereference.strategyOpts['openapi-3-2']) == null ? void 0 : _this$options$derefer2.circularReplacer) != null ? _this$options$derefer : this.options.dereference.circularReplacer;
123
+ const replacement = replacer(refElement);
124
+ link.replaceWith(refElement, mutationReplacer);
125
+ return !parent ? replacement : false;
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Dive deep into the fragment.
131
+ *
132
+ * Cases to consider:
133
+ * 1. We're crossing document boundary
134
+ * 2. Fragment is from non-root document
135
+ * 3. Fragment is a Reference Object. We need to follow it to get the eventual value
136
+ * 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
137
+ */
138
+ const isNonRootDocument = _empty.url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
139
+ const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
140
+ if ((isExternalReference || isNonRootDocument || (0, _apidomNsOpenapi.isReferenceElement)(referencedElement) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
141
+ var _this$basePath;
142
+ // append referencing reference to ancestors lineage
143
+ directAncestors.add(referencingElement);
144
+ const visitor = new OpenAPI3_2SwaggerClientDereferenceVisitor({
145
+ reference,
146
+ namespace: this.namespace,
147
+ indirections: [...this.indirections],
148
+ options: this.options,
149
+ refractCache: this.refractCache,
150
+ ancestors: ancestorsLineage,
151
+ allowMetaPatches: this.allowMetaPatches,
152
+ useCircularStructures: this.useCircularStructures,
153
+ basePath: (_this$basePath = this.basePath) != null ? _this$basePath : [...(0, _toPath.default)([...ancestors, parent, referencingElement]), '$ref']
154
+ });
155
+ referencedElement = await visitAsync(referencedElement, visitor, {
156
+ keyMap: _apidomNsOpenapi.keyMap,
157
+ nodeTypeGetter: _apidomNsOpenapi.getNodeType
158
+ });
159
+
160
+ // remove referencing reference from ancestors lineage
161
+ directAncestors.delete(referencingElement);
162
+ }
163
+ this.indirections.pop();
164
+ const mergedElement = (0, _apidomCore.cloneShallow)(referencedElement);
165
+
166
+ // annotate fragment with info about original Reference element
167
+ mergedElement.setMetaProperty('ref-fields', {
168
+ $ref: (0, _apidomCore.toValue)(referencingElement.$ref),
169
+ description: (0, _apidomCore.toValue)(referencingElement.description),
170
+ summary: (0, _apidomCore.toValue)(referencingElement.summary)
171
+ });
172
+ // annotate fragment with info about origin
173
+ mergedElement.setMetaProperty('ref-origin', reference.uri);
174
+ // annotate fragment with info about referencing element
175
+ mergedElement.setMetaProperty('ref-referencing-element-id', (0, _apidomCore.cloneDeep)(identityManager.identify(referencingElement)));
176
+
177
+ // override description and summary (outer has higher priority then inner)
178
+ if ((0, _apidomCore.isObjectElement)(referencedElement)) {
179
+ if (referencingElement.hasKey('description') && 'description' in referencedElement) {
180
+ mergedElement.remove('description');
181
+ mergedElement.set('description', referencingElement.get('description'));
182
+ }
183
+ if (referencingElement.hasKey('summary') && 'summary' in referencedElement) {
184
+ mergedElement.remove('summary');
185
+ mergedElement.set('summary', referencingElement.get('summary'));
186
+ }
187
+ }
188
+
189
+ // apply meta patches
190
+ if (this.allowMetaPatches && (0, _apidomCore.isObjectElement)(mergedElement)) {
191
+ // apply meta patch only when not already applied
192
+ if (!mergedElement.hasKey('$$ref')) {
193
+ const baseURI = _empty.url.resolve(retrievalURI, $refBaseURI);
194
+ mergedElement.set('$$ref', baseURI);
195
+ }
196
+ }
197
+
198
+ /**
199
+ * Transclude referencing element with merged referenced element.
200
+ */
201
+ link.replaceWith(mergedElement, mutationReplacer);
202
+
203
+ /**
204
+ * We're at the root of the tree, so we're just replacing the entire tree.
205
+ */
206
+ return !parent ? mergedElement : false;
207
+ } catch (error) {
208
+ var _this$basePath2, _this$options$derefer3;
209
+ const rootCause = (0, _getRootCause.default)(error);
210
+ const wrappedError = wrapError(rootCause, {
211
+ baseDoc: this.reference.uri,
212
+ $ref: (0, _apidomCore.toValue)(referencingElement.$ref),
213
+ pointer: _modern.URIFragmentIdentifier.fromURIReference((0, _apidomCore.toValue)(referencingElement.$ref)),
214
+ fullPath: (_this$basePath2 = this.basePath) != null ? _this$basePath2 : [...(0, _toPath.default)([...ancestors, parent, referencingElement]), '$ref']
215
+ });
216
+ (_this$options$derefer3 = this.options.dereference.dereferenceOpts) == null || (_this$options$derefer3 = _this$options$derefer3.errors) == null || _this$options$derefer3.push == null || _this$options$derefer3.push(wrappedError);
217
+ return undefined;
218
+ }
219
+ }
220
+ async PathItemElement(pathItemElement, key, parent, path, ancestors, link) {
221
+ try {
222
+ // ignore PathItemElement without $ref field
223
+ if (!(0, _apidomCore.isStringElement)(pathItemElement.$ref)) {
224
+ return undefined;
225
+ }
226
+
227
+ // skip current referencing element as it's already been access
228
+ if (this.indirections.includes(pathItemElement)) {
229
+ return false;
230
+ }
231
+
232
+ // skip already identified cycled Path Item Objects
233
+ if ((0, _apidomCore.includesClasses)(['cycle'], pathItemElement.$ref)) {
234
+ return false;
235
+ }
236
+ const [ancestorsLineage, directAncestors] = this.toAncestorLineage([...ancestors, parent]);
237
+ const retrievalURI = this.toBaseURI((0, _apidomCore.toValue)(pathItemElement.$ref));
238
+ const isInternalReference = _empty.url.stripHash(this.reference.uri) === retrievalURI;
239
+ const isExternalReference = !isInternalReference;
240
+
241
+ // ignore resolving internal Path Item Elements
242
+ if (!this.options.resolve.internal && isInternalReference) {
243
+ return undefined;
244
+ }
245
+ // ignore resolving external Path Item Elements
246
+ if (!this.options.resolve.external && isExternalReference) {
247
+ return undefined;
248
+ }
249
+ const reference = await this.toReference((0, _apidomCore.toValue)(pathItemElement.$ref));
250
+ const $refBaseURI = _empty.url.resolve(retrievalURI, (0, _apidomCore.toValue)(pathItemElement.$ref));
251
+ this.indirections.push(pathItemElement);
252
+ const jsonPointer = _modern.URIFragmentIdentifier.fromURIReference($refBaseURI);
253
+
254
+ // possibly non-semantic referenced element
255
+ let referencedElement = (0, _modern.evaluate)(reference.value.result, jsonPointer);
256
+ referencedElement.id = identityManager.identify(referencedElement);
257
+
258
+ // applying semantics to a referenced element
259
+ if ((0, _apidomCore.isPrimitiveElement)(referencedElement)) {
260
+ const cacheKey = `path-item-${(0, _apidomCore.toValue)(identityManager.identify(referencedElement))}`;
261
+ if (this.refractCache.has(cacheKey)) {
262
+ referencedElement = this.refractCache.get(cacheKey);
263
+ } else {
264
+ referencedElement = _apidomNsOpenapi.PathItemElement.refract(referencedElement);
265
+ this.refractCache.set(cacheKey, referencedElement);
266
+ }
267
+ }
268
+
269
+ // detect direct or indirect reference
270
+ if (pathItemElement === referencedElement) {
271
+ throw new _apidomError.ApiDOMError('Recursive Path Item Object reference detected');
272
+ }
273
+
274
+ // detect maximum depth of dereferencing
275
+ if (this.indirections.length > this.options.dereference.maxDepth) {
276
+ throw new _empty.MaximumDereferenceDepthError(`Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`);
277
+ }
278
+
279
+ // detect second deep dive into the same fragment and avoid it
280
+ if (ancestorsLineage.includes(referencedElement)) {
281
+ reference.refSet.circular = true;
282
+ if (this.options.dereference.circular === 'error') {
283
+ throw new _apidomError.ApiDOMError('Circular reference detected');
284
+ } else if (this.options.dereference.circular === 'replace') {
285
+ var _this$options$derefer4, _this$options$derefer5;
286
+ const refElement = new _apidomCore.RefElement(referencedElement.id, {
287
+ type: 'path-item',
288
+ uri: reference.uri,
289
+ $ref: (0, _apidomCore.toValue)(pathItemElement.$ref),
290
+ baseURI: $refBaseURI,
291
+ referencingElement: pathItemElement
292
+ });
293
+ const replacer = (_this$options$derefer4 = (_this$options$derefer5 = this.options.dereference.strategyOpts['openapi-3-2']) == null ? void 0 : _this$options$derefer5.circularReplacer) != null ? _this$options$derefer4 : this.options.dereference.circularReplacer;
294
+ const replacement = replacer(refElement);
295
+ link.replaceWith(refElement, mutationReplacer);
296
+ return !parent ? replacement : false;
297
+ }
298
+ }
299
+
300
+ /**
301
+ * Dive deep into the fragment.
302
+ *
303
+ * Cases to consider:
304
+ * 1. We're crossing document boundary
305
+ * 2. Fragment is from non-root document
306
+ * 3. Fragment is a Path Item Object with $ref field. We need to follow it to get the eventual value
307
+ * 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
308
+ */
309
+ const isNonRootDocument = _empty.url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
310
+ const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
311
+ if ((isExternalReference || isNonRootDocument || (0, _apidomNsOpenapi.isPathItemElement)(referencedElement) && (0, _apidomCore.isStringElement)(referencedElement.$ref) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
312
+ var _this$basePath3;
313
+ // append referencing schema to ancestors lineage
314
+ directAncestors.add(pathItemElement);
315
+
316
+ // dive deep into the referenced element
317
+ const visitor = new OpenAPI3_2SwaggerClientDereferenceVisitor({
318
+ reference,
319
+ namespace: this.namespace,
320
+ indirections: [...this.indirections],
321
+ options: this.options,
322
+ ancestors: ancestorsLineage,
323
+ allowMetaPatches: this.allowMetaPatches,
324
+ useCircularStructures: this.useCircularStructures,
325
+ basePath: (_this$basePath3 = this.basePath) != null ? _this$basePath3 : [...(0, _toPath.default)([...ancestors, parent, pathItemElement]), '$ref']
326
+ });
327
+ referencedElement = await visitAsync(referencedElement, visitor, {
328
+ keyMap: _apidomNsOpenapi.keyMap,
329
+ nodeTypeGetter: _apidomNsOpenapi.getNodeType
330
+ });
331
+
332
+ // remove referencing schema from ancestors lineage
333
+ directAncestors.delete(pathItemElement);
334
+ }
335
+ this.indirections.pop();
336
+
337
+ /**
338
+ * Creating a new version of Path Item by merging fields from referenced Path Item with referencing one.
339
+ */
340
+ if ((0, _apidomNsOpenapi.isPathItemElement)(referencedElement)) {
341
+ const mergedElement = new _apidomNsOpenapi.PathItemElement([...referencedElement.content], (0, _apidomCore.cloneDeep)(referencedElement.meta), (0, _apidomCore.cloneDeep)(referencedElement.attributes));
342
+ // existing keywords from referencing PathItemElement overrides ones from referenced element
343
+ pathItemElement.forEach((value, keyElement, item) => {
344
+ mergedElement.remove((0, _apidomCore.toValue)(keyElement));
345
+ mergedElement.content.push(item);
346
+ });
347
+ mergedElement.remove('$ref');
348
+
349
+ // annotate referenced element with info about original referencing element
350
+ mergedElement.setMetaProperty('ref-fields', {
351
+ $ref: (0, _apidomCore.toValue)(pathItemElement.$ref)
352
+ });
353
+ // annotate referenced element with info about origin
354
+ mergedElement.setMetaProperty('ref-origin', reference.uri);
355
+ // annotate fragment with info about referencing element
356
+ mergedElement.setMetaProperty('ref-referencing-element-id', (0, _apidomCore.cloneDeep)(identityManager.identify(pathItemElement)));
357
+
358
+ // apply meta patches
359
+ if (this.allowMetaPatches) {
360
+ // apply meta patch only when not already applied
361
+ if (typeof mergedElement.get('$$ref') === 'undefined') {
362
+ const baseURI = _empty.url.resolve(retrievalURI, $refBaseURI);
363
+ mergedElement.set('$$ref', baseURI);
364
+ }
365
+ }
366
+ referencedElement = mergedElement;
367
+ }
368
+
369
+ /**
370
+ * Transclude referencing element with merged referenced element.
371
+ */
372
+ link.replaceWith(referencedElement, mutationReplacer);
373
+
374
+ /**
375
+ * We're at the root of the tree, so we're just replacing the entire tree.
376
+ */
377
+ return !parent ? referencedElement : undefined;
378
+ } catch (error) {
379
+ var _this$basePath4, _this$options$derefer6;
380
+ const rootCause = (0, _getRootCause.default)(error);
381
+ const wrappedError = wrapError(rootCause, {
382
+ baseDoc: this.reference.uri,
383
+ $ref: (0, _apidomCore.toValue)(pathItemElement.$ref),
384
+ pointer: _modern.URIFragmentIdentifier.fromURIReference((0, _apidomCore.toValue)(pathItemElement.$ref)),
385
+ fullPath: (_this$basePath4 = this.basePath) != null ? _this$basePath4 : [...(0, _toPath.default)([...ancestors, parent, pathItemElement]), '$ref']
386
+ });
387
+ (_this$options$derefer6 = this.options.dereference.dereferenceOpts) == null || (_this$options$derefer6 = _this$options$derefer6.errors) == null || _this$options$derefer6.push == null || _this$options$derefer6.push(wrappedError);
388
+ return undefined;
389
+ }
390
+ }
391
+ async SchemaElement(referencingElement, key, parent, path, ancestors, link) {
392
+ try {
393
+ // skip current referencing schema as $ref keyword was not defined
394
+ if (!(0, _apidomCore.isStringElement)(referencingElement.$ref)) {
395
+ // skip traversing this schema but traverse all it's child schemas
396
+ return undefined;
397
+ }
398
+
399
+ // skip current referencing element as it's already been access
400
+ if (this.indirections.includes(referencingElement)) {
401
+ return false;
402
+ }
403
+ const [ancestorsLineage, directAncestors] = this.toAncestorLineage([...ancestors, parent]);
404
+
405
+ // compute baseURI using rules around $id and $ref keywords
406
+ let reference = await this.toReference(_empty.url.unsanitize(this.reference.uri));
407
+ let {
408
+ uri: retrievalURI
409
+ } = reference;
410
+ const $refBaseURI = (0, _openapi.resolveSchema$refField)(retrievalURI, referencingElement);
411
+ const $refBaseURIStrippedHash = _empty.url.stripHash($refBaseURI);
412
+ const file = new _empty.File({
413
+ uri: $refBaseURIStrippedHash
414
+ });
415
+ const isUnknownURI = !this.options.resolve.resolvers.some(r => r.canRead(file));
416
+ const isURL = !isUnknownURI;
417
+ let isInternalReference = _empty.url.stripHash(this.reference.uri) === $refBaseURI;
418
+ let isExternalReference = !isInternalReference;
419
+ this.indirections.push(referencingElement);
420
+
421
+ // determining reference, proper evaluation and selection mechanism
422
+ let referencedElement;
423
+ try {
424
+ if (isUnknownURI || isURL) {
425
+ // we're dealing with canonical URI or URL with possible fragment
426
+ retrievalURI = this.toBaseURI($refBaseURI);
427
+ const selector = $refBaseURI;
428
+ const referenceAsSchema = (0, _openapi.maybeRefractToSchemaElement)(reference.value.result);
429
+ referencedElement = (0, _uri.evaluate)(selector, referenceAsSchema);
430
+ referencedElement = (0, _openapi.maybeRefractToSchemaElement)(referencedElement);
431
+ referencedElement.id = identityManager.identify(referencedElement);
432
+
433
+ // ignore resolving internal Schema Objects
434
+ if (!this.options.resolve.internal && isInternalReference) {
435
+ // skip traversing this schema element but traverse all it's child elements
436
+ return undefined;
437
+ }
438
+ // ignore resolving external Schema Objects
439
+ if (!this.options.resolve.external && isExternalReference) {
440
+ // skip traversing this schema element but traverse all it's child elements
441
+ return undefined;
442
+ }
443
+ } else {
444
+ // we're assuming here that we're dealing with JSON Pointer here
445
+ retrievalURI = this.toBaseURI($refBaseURI);
446
+ isInternalReference = _empty.url.stripHash(this.reference.uri) === retrievalURI;
447
+ isExternalReference = !isInternalReference;
448
+
449
+ // ignore resolving internal Schema Objects
450
+ if (!this.options.resolve.internal && isInternalReference) {
451
+ // skip traversing this schema element but traverse all it's child elements
452
+ return undefined;
453
+ }
454
+ // ignore resolving external Schema Objects
455
+ if (!this.options.resolve.external && isExternalReference) {
456
+ // skip traversing this schema element but traverse all it's child elements
457
+ return undefined;
458
+ }
459
+ reference = await this.toReference(_empty.url.unsanitize($refBaseURI));
460
+ const selector = _modern.URIFragmentIdentifier.fromURIReference($refBaseURI);
461
+ const referenceAsSchema = (0, _openapi.maybeRefractToSchemaElement)(reference.value.result);
462
+ referencedElement = (0, _modern.evaluate)(referenceAsSchema, selector);
463
+ referencedElement = (0, _openapi.maybeRefractToSchemaElement)(referencedElement);
464
+ referencedElement.id = identityManager.identify(referencedElement);
465
+ }
466
+ } catch (error) {
467
+ /**
468
+ * No SchemaElement($id=URL) was not found, so we're going to try to resolve
469
+ * the URL and assume the returned response is a JSON Schema.
470
+ */
471
+ if (isURL && error instanceof _uri.EvaluationJsonSchemaUriError) {
472
+ if ((0, _$anchor.isAnchor)((0, _$anchor.uriToAnchor)($refBaseURI))) {
473
+ // we're dealing with JSON Schema $anchor here
474
+ isInternalReference = _empty.url.stripHash(this.reference.uri) === retrievalURI;
475
+ isExternalReference = !isInternalReference;
476
+
477
+ // ignore resolving internal Schema Objects
478
+ if (!this.options.resolve.internal && isInternalReference) {
479
+ // skip traversing this schema element but traverse all it's child elements
480
+ return undefined;
481
+ }
482
+ // ignore resolving external Schema Objects
483
+ if (!this.options.resolve.external && isExternalReference) {
484
+ // skip traversing this schema element but traverse all it's child elements
485
+ return undefined;
486
+ }
487
+ reference = await this.toReference(_empty.url.unsanitize($refBaseURI));
488
+ const selector = (0, _$anchor.uriToAnchor)($refBaseURI);
489
+ const referenceAsSchema = (0, _openapi.maybeRefractToSchemaElement)(reference.value.result);
490
+ referencedElement = (0, _$anchor.evaluate)(selector, referenceAsSchema);
491
+ referencedElement = (0, _openapi.maybeRefractToSchemaElement)(referencedElement);
492
+ referencedElement.id = identityManager.identify(referencedElement);
493
+ } else {
494
+ // we're assuming here that we're dealing with JSON Pointer here
495
+ retrievalURI = this.toBaseURI((0, _apidomCore.toValue)($refBaseURI));
496
+ isInternalReference = _empty.url.stripHash(this.reference.uri) === retrievalURI;
497
+ isExternalReference = !isInternalReference;
498
+
499
+ // ignore resolving internal Schema Objects
500
+ if (!this.options.resolve.internal && isInternalReference) {
501
+ // skip traversing this schema element but traverse all it's child elements
502
+ return undefined;
503
+ }
504
+ // ignore resolving external Schema Objects
505
+ if (!this.options.resolve.external && isExternalReference) {
506
+ // skip traversing this schema element but traverse all it's child elements
507
+ return undefined;
508
+ }
509
+ reference = await this.toReference(_empty.url.unsanitize($refBaseURI));
510
+ const selector = _modern.URIFragmentIdentifier.fromURIReference($refBaseURI);
511
+ const referenceAsSchema = (0, _openapi.maybeRefractToSchemaElement)(reference.value.result);
512
+ referencedElement = (0, _modern.evaluate)(referenceAsSchema, selector);
513
+ referencedElement = (0, _openapi.maybeRefractToSchemaElement)(referencedElement);
514
+ referencedElement.id = identityManager.identify(referencedElement);
515
+ }
516
+ } else {
517
+ throw error;
518
+ }
519
+ }
520
+
521
+ // detect direct or indirect reference
522
+ if (referencingElement === referencedElement) {
523
+ throw new _apidomError.ApiDOMError('Recursive Schema Object reference detected');
524
+ }
525
+
526
+ // detect maximum depth of dereferencing
527
+ if (this.indirections.length > this.options.dereference.maxDepth) {
528
+ throw new _empty.MaximumDereferenceDepthError(`Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`);
529
+ }
530
+
531
+ // detect second deep dive into the same fragment and avoid it
532
+ if (ancestorsLineage.includes(referencedElement)) {
533
+ reference.refSet.circular = true;
534
+ if (this.options.dereference.circular === 'error') {
535
+ throw new _apidomError.ApiDOMError('Circular reference detected');
536
+ } else if (this.options.dereference.circular === 'replace') {
537
+ var _this$options$derefer7, _this$options$derefer8;
538
+ const refElement = new _apidomCore.RefElement(referencedElement.id, {
539
+ type: 'json-schema',
540
+ uri: reference.uri,
541
+ $ref: (0, _apidomCore.toValue)(referencingElement.$ref),
542
+ baseURI: _empty.url.resolve(retrievalURI, $refBaseURI),
543
+ referencingElement
544
+ });
545
+ const replacer = (_this$options$derefer7 = (_this$options$derefer8 = this.options.dereference.strategyOpts['openapi-3-2']) == null ? void 0 : _this$options$derefer8.circularReplacer) != null ? _this$options$derefer7 : this.options.dereference.circularReplacer;
546
+ const replacement = replacer(refElement);
547
+ link.replaceWith(replacement, mutationReplacer);
548
+ return !parent ? replacement : false;
549
+ }
550
+ }
551
+
552
+ /**
553
+ * Dive deep into the fragment.
554
+ *
555
+ * Cases to consider:
556
+ * 1. We're crossing document boundary
557
+ * 2. Fragment is from non-root document
558
+ * 3. Fragment is a Schema Object with $ref field. We need to follow it to get the eventual value
559
+ * 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
560
+ */
561
+ const isNonRootDocument = _empty.url.stripHash(reference.refSet.rootRef.uri) !== reference.uri;
562
+ const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
563
+ if ((isExternalReference || isNonRootDocument || (0, _apidomNsOpenapi.isSchemaElement)(referencedElement) && (0, _apidomCore.isStringElement)(referencedElement.$ref) || shouldDetectCircular) && !ancestorsLineage.includesCycle(referencedElement)) {
564
+ var _this$basePath5;
565
+ // append referencing schema to ancestors lineage
566
+ directAncestors.add(referencingElement);
567
+
568
+ // dive deep into the fragment
569
+ const mergeVisitor = new OpenAPI3_2SwaggerClientDereferenceVisitor({
570
+ reference,
571
+ namespace: this.namespace,
572
+ indirections: [...this.indirections],
573
+ options: this.options,
574
+ useCircularStructures: this.useCircularStructures,
575
+ allowMetaPatches: this.allowMetaPatches,
576
+ ancestors: ancestorsLineage,
577
+ basePath: (_this$basePath5 = this.basePath) != null ? _this$basePath5 : [...(0, _toPath.default)([...ancestors, parent, referencingElement]), '$ref']
578
+ });
579
+ referencedElement = await visitAsync(referencedElement, mergeVisitor, {
580
+ keyMap: _apidomNsOpenapi.keyMap,
581
+ nodeTypeGetter: _apidomNsOpenapi.getNodeType
582
+ });
583
+
584
+ // remove referencing schema from ancestors lineage
585
+ directAncestors.delete(referencingElement);
586
+ }
587
+ this.indirections.pop();
588
+ if ((0, _apidomNsOpenapi.isBooleanJsonSchemaElement)(referencedElement)) {
589
+ const booleanJsonSchemaElement = (0, _apidomCore.cloneDeep)(referencedElement);
590
+ // annotate referenced element with info about original referencing element
591
+ booleanJsonSchemaElement.setMetaProperty('ref-fields', {
592
+ $ref: (0, _apidomCore.toValue)(referencingElement.$ref)
593
+ });
594
+ // annotate referenced element with info about origin
595
+ booleanJsonSchemaElement.setMetaProperty('ref-origin', reference.uri);
596
+ // annotate fragment with info about referencing element
597
+ booleanJsonSchemaElement.setMetaProperty('ref-referencing-element-id', (0, _apidomCore.cloneDeep)(identityManager.identify(referencingElement)));
598
+ link.replaceWith(booleanJsonSchemaElement, mutationReplacer);
599
+ return !parent ? booleanJsonSchemaElement : false;
600
+ }
601
+
602
+ /**
603
+ * Creating a new version of Schema Object by merging fields from referenced Schema Object with referencing one.
604
+ */
605
+ if ((0, _apidomNsOpenapi.isSchemaElement)(referencedElement)) {
606
+ // Schema Object - merge keywords from referenced schema with referencing schema
607
+ const mergedElement = new _apidomNsOpenapi.SchemaElement([...referencedElement.content], (0, _apidomCore.cloneDeep)(referencedElement.meta), (0, _apidomCore.cloneDeep)(referencedElement.attributes));
608
+ // existing keywords from referencing schema overrides ones from referenced schema
609
+ referencingElement.forEach((value, keyElement, item) => {
610
+ mergedElement.remove((0, _apidomCore.toValue)(keyElement));
611
+ mergedElement.content.push(item);
612
+ });
613
+ mergedElement.remove('$ref');
614
+ // annotate referenced element with info about original referencing element
615
+ mergedElement.setMetaProperty('ref-fields', {
616
+ $ref: (0, _apidomCore.toValue)(referencingElement.$ref)
617
+ });
618
+ // annotate fragment with info about origin
619
+ mergedElement.setMetaProperty('ref-origin', reference.uri);
620
+ // annotate fragment with info about referencing element
621
+ mergedElement.setMetaProperty('ref-referencing-element-id', (0, _apidomCore.cloneDeep)(identityManager.identify(referencingElement)));
622
+
623
+ // allowMetaPatches option processing
624
+ if (this.allowMetaPatches) {
625
+ // apply meta patch only when not already applied
626
+ if (typeof mergedElement.get('$$ref') === 'undefined') {
627
+ const baseURI = _empty.url.resolve(retrievalURI, $refBaseURI);
628
+ mergedElement.set('$$ref', baseURI);
629
+ }
630
+ }
631
+ referencedElement = mergedElement;
632
+ }
633
+
634
+ /**
635
+ * Transclude referencing element with merged referenced element.
636
+ */
637
+ link.replaceWith(referencedElement, mutationReplacer);
638
+
639
+ /**
640
+ * We're at the root of the tree, so we're just replacing the entire tree.
641
+ */
642
+ return !parent ? referencedElement : undefined;
643
+ } catch (error) {
644
+ var _this$basePath6, _this$options$derefer9;
645
+ const rootCause = (0, _getRootCause.default)(error);
646
+ const wrappedError = new _SchemaRefError.default(`Could not resolve reference: ${rootCause.message}`, {
647
+ baseDoc: this.reference.uri,
648
+ $ref: (0, _apidomCore.toValue)(referencingElement.$ref),
649
+ fullPath: (_this$basePath6 = this.basePath) != null ? _this$basePath6 : [...(0, _toPath.default)([...ancestors, parent, referencingElement]), '$ref'],
650
+ cause: rootCause
651
+ });
652
+ (_this$options$derefer9 = this.options.dereference.dereferenceOpts) == null || (_this$options$derefer9 = _this$options$derefer9.errors) == null || _this$options$derefer9.push == null || _this$options$derefer9.push(wrappedError);
653
+ return undefined;
654
+ }
655
+ }
656
+
657
+ // eslint-disable-next-line class-methods-use-this
658
+ async LinkElement() {
659
+ /**
660
+ * OpenApi3_2DereferenceVisitor is doing lookup of Operation Objects
661
+ * and assigns them to Link Object metadata. This is not needed in
662
+ * swagger-client context, so we're disabling it here.
663
+ */
664
+ return undefined;
665
+ }
666
+ async ExampleElement(exampleElement, key, parent, path, ancestors, link) {
667
+ try {
668
+ return await super.ExampleElement(exampleElement, key, parent, path, ancestors, link);
669
+ } catch (error) {
670
+ var _this$basePath7, _this$options$derefer0;
671
+ const rootCause = (0, _getRootCause.default)(error);
672
+ const wrappedError = wrapError(rootCause, {
673
+ baseDoc: this.reference.uri,
674
+ externalValue: (0, _apidomCore.toValue)(exampleElement.externalValue),
675
+ fullPath: (_this$basePath7 = this.basePath) != null ? _this$basePath7 : [...(0, _toPath.default)([...ancestors, parent, exampleElement]), 'externalValue']
676
+ });
677
+ (_this$options$derefer0 = this.options.dereference.dereferenceOpts) == null || (_this$options$derefer0 = _this$options$derefer0.errors) == null || _this$options$derefer0.push == null || _this$options$derefer0.push(wrappedError);
678
+ return undefined;
679
+ }
680
+ }
681
+ }
682
+ var _default = exports.default = OpenAPI3_2SwaggerClientDereferenceVisitor;
683
+ /* eslint-enable camelcase */