-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathvisitor.ts
675 lines (578 loc) · 23.9 KB
/
visitor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
import { propEq } from 'ramda';
import { isUndefined } from 'ramda-adjunct';
import {
isPrimitiveElement,
isStringElement,
isMemberElement,
isElement,
IdentityManager,
visit,
find,
cloneShallow,
cloneDeep,
toValue,
Element,
RefElement,
Namespace,
} from '@swagger-api/apidom-core';
import { ApiDOMError } from '@swagger-api/apidom-error';
import { evaluate, uriToPointer } from '@swagger-api/apidom-json-pointer';
import {
getNodeType,
keyMap,
ReferenceElement,
ExampleElement,
LinkElement,
OperationElement,
PathItemElement,
isReferenceElement,
isOperationElement,
isPathItemElement,
isReferenceLikeElement,
} from '@swagger-api/apidom-ns-openapi-3-0';
import MaximumDereferenceDepthError from '../../../errors/MaximumDereferenceDepthError.ts';
import MaximumResolveDepthError from '../../../errors/MaximumResolveDepthError.ts';
import * as url from '../../../util/url.ts';
import parse from '../../../parse/index.ts';
import Reference from '../../../Reference.ts';
import ReferenceSet from '../../../ReferenceSet.ts';
import { AncestorLineage } from '../../util.ts';
import type { ReferenceOptions } from '../../../options/index.ts';
// @ts-ignore
const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')];
// initialize element identity manager
const identityManager = new IdentityManager();
/**
* Custom mutation replacer.
* @public
*/
export const mutationReplacer = (
newElement: Element,
oldElement: Element,
key: string | number,
parent: Element | undefined,
) => {
if (isMemberElement(parent)) {
parent.value = newElement; // eslint-disable-line no-param-reassign
} else if (Array.isArray(parent)) {
parent[key] = newElement; // eslint-disable-line no-param-reassign
}
};
/**
* @public
*/
export interface OpenAPI3_0DereferenceVisitorOptions {
readonly namespace: Namespace;
readonly reference: Reference;
readonly options: ReferenceOptions;
readonly indirections?: Element[];
readonly ancestors?: AncestorLineage<Element>;
readonly refractCache?: Map<string, Element>;
}
/**
* @public
*/
class OpenAPI3_0DereferenceVisitor {
protected readonly indirections: Element[];
protected readonly namespace: Namespace;
protected readonly reference: Reference;
protected readonly options: ReferenceOptions;
protected readonly ancestors: AncestorLineage<Element>;
protected readonly refractCache: Map<string, Element>;
constructor({
reference,
namespace,
options,
indirections = [],
ancestors = new AncestorLineage(),
refractCache = new Map(),
}: OpenAPI3_0DereferenceVisitorOptions) {
this.indirections = indirections;
this.namespace = namespace;
this.reference = reference;
this.options = options;
this.ancestors = new AncestorLineage(...ancestors);
this.refractCache = refractCache;
}
protected toBaseURI(uri: string): string {
return url.resolve(this.reference.uri, url.sanitize(url.stripHash(uri)));
}
protected async toReference(uri: string): Promise<Reference> {
// detect maximum depth of resolution
if (this.reference.depth >= this.options.resolve.maxDepth) {
throw new MaximumResolveDepthError(
`Maximum resolution depth of ${this.options.resolve.maxDepth} has been exceeded by file "${this.reference.uri}"`,
);
}
const baseURI = this.toBaseURI(uri);
const { refSet } = this.reference as { refSet: ReferenceSet };
// we've already processed this Reference in past
if (refSet.has(baseURI)) {
return refSet.find(propEq(baseURI, 'uri'))!;
}
const parseResult = await parse(url.unsanitize(baseURI), {
...this.options,
parse: { ...this.options.parse, mediaType: 'text/plain' },
});
// register new mutable reference with a refSet
const mutableReference = new Reference({
uri: baseURI,
value: cloneDeep(parseResult),
depth: this.reference.depth + 1,
});
refSet.add(mutableReference);
if (this.options.dereference.immutable) {
// register new immutable reference with a refSet
const immutableReference = new Reference({
uri: `immutable://${baseURI}`,
value: parseResult,
depth: this.reference.depth + 1,
});
refSet.add(immutableReference);
}
return mutableReference;
}
protected toAncestorLineage(
ancestors: (Element | Element[] | undefined)[],
): [AncestorLineage<Element>, Set<Element>] {
/**
* Compute full ancestors lineage.
* Ancestors are flatten to unwrap all Element instances.
*/
const directAncestors = new Set<Element>(ancestors.filter(isElement));
const ancestorsLineage = new AncestorLineage(...this.ancestors, directAncestors);
return [ancestorsLineage, directAncestors];
}
public async ReferenceElement(
referencingElement: ReferenceElement,
key: string | number,
parent: Element | undefined,
path: (string | number)[],
ancestors: [Element | Element[]],
link: { replaceWith: (element: Element, replacer: typeof mutationReplacer) => void },
) {
// skip current referencing element as it's already been access
if (this.indirections.includes(referencingElement)) {
return false;
}
const [ancestorsLineage, directAncestors] = this.toAncestorLineage([...ancestors, parent]);
const retrievalURI = this.toBaseURI(toValue(referencingElement.$ref));
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
const isExternalReference = !isInternalReference;
// ignore resolving internal Reference Objects
if (!this.options.resolve.internal && isInternalReference) {
// skip traversing this reference element
return false;
}
// ignore resolving external Reference Objects
if (!this.options.resolve.external && isExternalReference) {
// skip traversing this reference element
return false;
}
const reference = await this.toReference(toValue(referencingElement.$ref));
const $refBaseURI = url.resolve(retrievalURI, toValue(referencingElement.$ref));
this.indirections.push(referencingElement);
const jsonPointer = uriToPointer($refBaseURI);
// possibly non-semantic fragment
let referencedElement = evaluate(jsonPointer, reference.value.result as Element);
referencedElement.id = identityManager.identify(referencedElement);
/**
* Applying semantics to a referenced element if semantics are missing.
*/
if (isPrimitiveElement(referencedElement)) {
const referencedElementType = toValue(referencingElement.meta.get('referenced-element'));
const cacheKey = `${referencedElementType}-${toValue(identityManager.identify(referencedElement))}`;
if (this.refractCache.has(cacheKey)) {
referencedElement = this.refractCache.get(cacheKey)!;
} else if (isReferenceLikeElement(referencedElement)) {
// handling indirect references
referencedElement = ReferenceElement.refract(referencedElement);
referencedElement.setMetaProperty('referenced-element', referencedElementType);
this.refractCache.set(cacheKey, referencedElement);
} else {
// handling direct references
const ElementClass = this.namespace.getElementClass(referencedElementType);
referencedElement = ElementClass.refract(referencedElement);
this.refractCache.set(cacheKey, referencedElement);
}
}
// detect direct or circular reference
if (referencingElement === referencedElement) {
throw new ApiDOMError('Recursive Reference Object detected');
}
// detect maximum depth of dereferencing
if (this.indirections.length > this.options.dereference.maxDepth) {
throw new MaximumDereferenceDepthError(
`Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`,
);
}
// detect second deep dive into the same fragment and avoid it
if (ancestorsLineage.includes(referencedElement)) {
reference.refSet!.circular = true;
if (this.options.dereference.circular === 'error') {
throw new ApiDOMError('Circular reference detected');
} else if (this.options.dereference.circular === 'replace') {
const refElement = new RefElement(referencedElement.id, {
type: 'reference',
uri: reference.uri,
$ref: toValue(referencingElement.$ref),
});
const replacer =
this.options.dereference.strategyOpts['openapi-3-0']?.circularReplacer ??
this.options.dereference.circularReplacer;
const replacement = replacer(refElement);
link.replaceWith(replacement, mutationReplacer);
return !parent ? replacement : false;
}
}
/**
* Dive deep into the fragment.
*
* Cases to consider:
* 1. We're crossing document boundary
* 2. Fragment is from non-entry document
* 3. Fragment is a Reference Object. We need to follow it to get the eventual value
* 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
*/
const isNonEntryDocument = url.stripHash(reference.refSet!.rootRef!.uri) !== reference.uri;
const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
if (
(isExternalReference ||
isNonEntryDocument ||
isReferenceElement(referencedElement) ||
shouldDetectCircular) &&
!ancestorsLineage.includesCycle(referencedElement)
) {
// append referencing reference to ancestors lineage
directAncestors.add(referencingElement);
const visitor = new OpenAPI3_0DereferenceVisitor({
reference,
namespace: this.namespace,
indirections: [...this.indirections],
options: this.options,
refractCache: this.refractCache,
ancestors: ancestorsLineage,
});
referencedElement = await visitAsync(referencedElement, visitor, {
keyMap,
nodeTypeGetter: getNodeType,
});
// remove referencing reference from ancestors lineage
directAncestors.delete(referencingElement);
}
this.indirections.pop();
/**
* Creating a new version of referenced element to avoid modifying the original one.
*/
const mergedElement = cloneShallow(referencedElement);
// assign unique id to merged element
mergedElement.setMetaProperty('id', identityManager.generateId());
// annotate referenced element with info about original referencing element
mergedElement.setMetaProperty('ref-fields', {
$ref: toValue(referencingElement.$ref),
});
// annotate fragment with info about origin
mergedElement.setMetaProperty('ref-origin', reference.uri);
// annotate fragment with info about referencing element
mergedElement.setMetaProperty(
'ref-referencing-element-id',
cloneDeep(identityManager.identify(referencingElement)),
);
/**
* Transclude referencing element with merged referenced element.
*/
link.replaceWith(mergedElement, mutationReplacer);
/**
* We're at the root of the tree, so we're just replacing the entire tree.
*/
return !parent ? mergedElement : false;
}
public async PathItemElement(
referencingElement: PathItemElement,
key: string | number,
parent: Element | undefined,
path: (string | number)[],
ancestors: [Element | Element[]],
link: { replaceWith: (element: Element, replacer: typeof mutationReplacer) => void },
) {
// ignore PathItemElement without $ref field
if (!isStringElement(referencingElement.$ref)) {
return undefined;
}
// skip current referencing element as it's already been access
if (this.indirections.includes(referencingElement)) {
return false;
}
const [ancestorsLineage, directAncestors] = this.toAncestorLineage([...ancestors, parent]);
const retrievalURI = this.toBaseURI(toValue(referencingElement.$ref));
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
const isExternalReference = !isInternalReference;
// ignore resolving internal Path Item Objects
if (!this.options.resolve.internal && isInternalReference) {
// skip traversing this Path Item element but traverse all it's child elements
return undefined;
}
// ignore resolving external Path Item Objects
if (!this.options.resolve.external && isExternalReference) {
// skip traversing this Path Item element but traverse all it's child elements
return undefined;
}
const reference = await this.toReference(toValue(referencingElement.$ref));
const $refBaseURI = url.resolve(retrievalURI, toValue(referencingElement.$ref));
this.indirections.push(referencingElement);
const jsonPointer = uriToPointer($refBaseURI);
// possibly non-semantic referenced element
let referencedElement = evaluate(jsonPointer, reference.value.result as Element);
referencedElement.id = identityManager.identify(referencedElement);
/**
* Applying semantics to a referenced element if semantics are missing.
*/
if (!isPathItemElement(referencedElement)) {
const cacheKey = `path-item-${toValue(identityManager.identify(referencedElement))}`;
if (this.refractCache.has(cacheKey)) {
referencedElement = this.refractCache.get(cacheKey)!;
} else {
referencedElement = PathItemElement.refract(referencedElement);
this.refractCache.set(cacheKey, referencedElement);
}
}
// detect direct or circular reference
if (referencingElement === referencedElement) {
throw new ApiDOMError('Recursive Path Item Object reference detected');
}
// detect maximum depth of dereferencing
if (this.indirections.length > this.options.dereference.maxDepth) {
throw new MaximumDereferenceDepthError(
`Maximum dereference depth of "${this.options.dereference.maxDepth}" has been exceeded in file "${this.reference.uri}"`,
);
}
// detect second deep dive into the same fragment and avoid it
if (ancestorsLineage.includes(referencedElement)) {
reference.refSet!.circular = true;
if (this.options.dereference.circular === 'error') {
throw new ApiDOMError('Circular reference detected');
} else if (this.options.dereference.circular === 'replace') {
const refElement = new RefElement(referencedElement.id, {
type: 'path-item',
uri: reference.uri,
$ref: toValue(referencingElement.$ref),
});
const replacer =
this.options.dereference.strategyOpts['openapi-3-0']?.circularReplacer ??
this.options.dereference.circularReplacer;
const replacement = replacer(refElement);
link.replaceWith(replacement, mutationReplacer);
return !parent ? replacement : undefined;
}
}
/**
* Dive deep into the fragment.
*
* Cases to consider:
* 1. We're crossing document boundary
* 2. Fragment is from non-entry document
* 3. Fragment is a Path Item Object with $ref field. We need to follow it to get the eventual value
* 4. We are dereferencing the fragment lazily/eagerly depending on circular mode
*/
const isNonEntryDocument = url.stripHash(reference.refSet!.rootRef!.uri) !== reference.uri;
const shouldDetectCircular = ['error', 'replace'].includes(this.options.dereference.circular);
if (
(isExternalReference ||
isNonEntryDocument ||
(isPathItemElement(referencedElement) && isStringElement(referencedElement.$ref)) ||
shouldDetectCircular) &&
!ancestorsLineage.includesCycle(referencedElement)
) {
// append referencing reference to ancestors lineage
directAncestors.add(referencingElement);
const visitor = new OpenAPI3_0DereferenceVisitor({
reference,
namespace: this.namespace,
indirections: [...this.indirections],
options: this.options,
refractCache: this.refractCache,
ancestors: ancestorsLineage,
});
referencedElement = await visitAsync(referencedElement, visitor, {
keyMap,
nodeTypeGetter: getNodeType,
});
// remove referencing reference from ancestors lineage
directAncestors.delete(referencingElement);
}
this.indirections.pop();
/**
* Creating a new version of Path Item by merging fields from referenced Path Item with referencing one.
*/
if (isPathItemElement(referencedElement)) {
const mergedElement = new PathItemElement(
[...referencedElement.content] as any,
cloneDeep(referencedElement.meta),
cloneDeep(referencedElement.attributes),
);
// assign unique id to merged element
mergedElement.setMetaProperty('id', identityManager.generateId());
// existing keywords from referencing PathItemElement overrides ones from referenced element
referencingElement.forEach((value: Element, keyElement: Element, item: Element) => {
mergedElement.remove(toValue(keyElement));
mergedElement.content.push(item);
});
mergedElement.remove('$ref');
// annotate referenced element with info about original referencing element
mergedElement.setMetaProperty('ref-fields', {
$ref: toValue(referencingElement.$ref),
});
// annotate referenced element with info about origin
mergedElement.setMetaProperty('ref-origin', reference.uri);
// annotate fragment with info about referencing element
mergedElement.setMetaProperty(
'ref-referencing-element-id',
cloneDeep(identityManager.identify(referencingElement)),
);
referencedElement = mergedElement;
}
/**
* Transclude referencing element with merged referenced element.
*/
link.replaceWith(referencedElement, mutationReplacer);
/**
* We're at the root of the tree, so we're just replacing the entire tree.
*/
return !parent ? referencedElement : undefined;
}
public async LinkElement(
linkElement: LinkElement,
key: string | number,
parent: Element | undefined,
path: (string | number)[],
ancestors: [Element | Element[]],
link: { replaceWith: (element: Element, replacer: typeof mutationReplacer) => void },
) {
// ignore LinkElement without operationRef or operationId field
if (!isStringElement(linkElement.operationRef) && !isStringElement(linkElement.operationId)) {
return undefined;
}
// operationRef and operationId fields are mutually exclusive
if (isStringElement(linkElement.operationRef) && isStringElement(linkElement.operationId)) {
throw new ApiDOMError(
'LinkElement operationRef and operationId fields are mutually exclusive.',
);
}
let operationElement: Element | undefined;
if (isStringElement(linkElement.operationRef)) {
// possibly non-semantic referenced element
const jsonPointer = uriToPointer(toValue(linkElement.operationRef));
const retrievalURI = this.toBaseURI(toValue(linkElement.operationRef));
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
const isExternalReference = !isInternalReference;
// ignore resolving internal Operation Object reference
if (!this.options.resolve.internal && isInternalReference) {
// skip traversing this Link element but traverse all it's child elements
return undefined;
}
// ignore resolving external Operation Object reference
if (!this.options.resolve.external && isExternalReference) {
// skip traversing this Link element but traverse all it's child elements
return undefined;
}
const reference = await this.toReference(toValue(linkElement.operationRef));
operationElement = evaluate(jsonPointer, reference.value.result as Element);
// applying semantics to a referenced element
if (isPrimitiveElement(operationElement)) {
const cacheKey = `operation-${toValue(identityManager.identify(operationElement))}`;
if (this.refractCache.has(cacheKey)) {
operationElement = this.refractCache.get(cacheKey)!;
} else {
operationElement = OperationElement.refract(operationElement);
this.refractCache.set(cacheKey, operationElement);
}
}
// create shallow clone to be able to annotate with metadata
operationElement = cloneShallow(operationElement);
// annotate operation element with info about origin
operationElement.setMetaProperty('ref-origin', reference.uri);
const linkElementCopy = cloneShallow(linkElement);
linkElementCopy.operationRef?.meta.set('operation', operationElement);
/**
* Transclude Link Object containing Operation Object in its meta.
*/
link.replaceWith(linkElementCopy, mutationReplacer);
/**
* We're at the root of the tree, so we're just replacing the entire tree.
*/
return !parent ? linkElementCopy : undefined;
}
if (isStringElement(linkElement.operationId)) {
const operationId = toValue(linkElement.operationId);
const reference = await this.toReference(url.unsanitize(this.reference.uri));
operationElement = find(
(e) =>
isOperationElement(e) && isElement(e.operationId) && e.operationId.equals(operationId),
reference.value.result as Element,
);
// OperationElement not found by its operationId
if (isUndefined(operationElement)) {
throw new ApiDOMError(`OperationElement(operationId=${operationId}) not found.`);
}
const linkElementCopy = cloneShallow(linkElement);
linkElementCopy.operationId?.meta.set('operation', operationElement);
/**
* Transclude Link Object containing Operation Object in its meta.
*/
link.replaceWith(linkElementCopy, mutationReplacer);
/**
* We're at the root of the tree, so we're just replacing the entire tree.
*/
return !parent ? linkElementCopy : undefined;
}
return undefined;
}
public async ExampleElement(
exampleElement: ExampleElement,
key: string | number,
parent: Element | undefined,
path: (string | number)[],
ancestors: [Element | Element[]],
link: { replaceWith: (element: Element, replacer: typeof mutationReplacer) => void },
) {
// ignore ExampleElement without externalValue field
if (!isStringElement(exampleElement.externalValue)) {
return undefined;
}
// value and externalValue fields are mutually exclusive
if (exampleElement.hasKey('value') && isStringElement(exampleElement.externalValue)) {
throw new ApiDOMError(
'ExampleElement value and externalValue fields are mutually exclusive.',
);
}
const retrievalURI = this.toBaseURI(toValue(exampleElement.externalValue));
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
const isExternalReference = !isInternalReference;
// ignore resolving external Example Objects
if (!this.options.resolve.internal && isInternalReference) {
// skip traversing this Example element but traverse all it's child elements
return undefined;
}
// ignore resolving external Example Objects
if (!this.options.resolve.external && isExternalReference) {
// skip traversing this Example element but traverse all it's child elements
return undefined;
}
const reference = await this.toReference(toValue(exampleElement.externalValue));
// shallow clone of the referenced element
const valueElement = cloneShallow(reference.value.result as Element);
// annotate operation element with info about origin
valueElement.setMetaProperty('ref-origin', reference.uri);
const exampleElementCopy = cloneShallow(exampleElement);
exampleElementCopy.value = valueElement;
/**
* Transclude Example Object containing external value.
*/
link.replaceWith(exampleElementCopy, mutationReplacer);
/**
* We're at the root of the tree, so we're just replacing the entire tree.
*/
return !parent ? exampleElementCopy : undefined;
}
}
export default OpenAPI3_0DereferenceVisitor;