1 | /* eslint-disable camelcase */
|
---|
2 | import { createNamespace, visit, mergeAllVisitors } from '@swagger-api/apidom-core';
|
---|
3 | import { ReferenceSet, Reference } from '@swagger-api/apidom-reference/configuration/empty';
|
---|
4 | import OpenApi3_1DereferenceStrategy from '@swagger-api/apidom-reference/dereference/strategies/openapi-3-1';
|
---|
5 | import openApi3_1Namespace, { getNodeType, keyMap } from '@swagger-api/apidom-ns-openapi-3-1';
|
---|
6 | import OpenApi3_1SwaggerClientDereferenceVisitor from './visitors/dereference.js';
|
---|
7 | import ParameterMacroVisitor from './visitors/parameters.js';
|
---|
8 | import ModelPropertyMacroVisitor from './visitors/properties.js';
|
---|
9 | import AllOfVisitor from './visitors/all-of.js';
|
---|
10 | const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')];
|
---|
11 | const OpenApi3_1SwaggerClientDereferenceStrategy = OpenApi3_1DereferenceStrategy.compose({
|
---|
12 | props: {
|
---|
13 | useCircularStructures: true,
|
---|
14 | allowMetaPatches: false,
|
---|
15 | parameterMacro: null,
|
---|
16 | modelPropertyMacro: null,
|
---|
17 | mode: 'non-strict',
|
---|
18 | ancestors: null
|
---|
19 | },
|
---|
20 | init({
|
---|
21 | useCircularStructures = this.useCircularStructures,
|
---|
22 | allowMetaPatches = this.allowMetaPatches,
|
---|
23 | parameterMacro = this.parameterMacro,
|
---|
24 | modelPropertyMacro = this.modelPropertyMacro,
|
---|
25 | mode = this.mode,
|
---|
26 | ancestors = []
|
---|
27 | } = {}) {
|
---|
28 | this.name = 'openapi-3-1-swagger-client';
|
---|
29 | this.useCircularStructures = useCircularStructures;
|
---|
30 | this.allowMetaPatches = allowMetaPatches;
|
---|
31 | this.parameterMacro = parameterMacro;
|
---|
32 | this.modelPropertyMacro = modelPropertyMacro;
|
---|
33 | this.mode = mode;
|
---|
34 | this.ancestors = [...ancestors];
|
---|
35 | },
|
---|
36 | methods: {
|
---|
37 | async dereference(file, options) {
|
---|
38 | var _options$dereference$;
|
---|
39 | const visitors = [];
|
---|
40 | const namespace = createNamespace(openApi3_1Namespace);
|
---|
41 | const refSet = (_options$dereference$ = options.dereference.refSet) !== null && _options$dereference$ !== void 0 ? _options$dereference$ : ReferenceSet();
|
---|
42 | let reference;
|
---|
43 | if (!refSet.has(file.uri)) {
|
---|
44 | reference = Reference({
|
---|
45 | uri: file.uri,
|
---|
46 | value: file.parseResult
|
---|
47 | });
|
---|
48 | refSet.add(reference);
|
---|
49 | } else {
|
---|
50 | // pre-computed refSet was provided as configuration option
|
---|
51 | reference = refSet.find(ref => ref.uri === file.uri);
|
---|
52 | }
|
---|
53 |
|
---|
54 | // create main dereference visitor
|
---|
55 | const dereferenceVisitor = OpenApi3_1SwaggerClientDereferenceVisitor({
|
---|
56 | reference,
|
---|
57 | namespace,
|
---|
58 | options,
|
---|
59 | useCircularStructures: this.useCircularStructures,
|
---|
60 | allowMetaPatches: this.allowMetaPatches,
|
---|
61 | ancestors: this.ancestors
|
---|
62 | });
|
---|
63 | visitors.push(dereferenceVisitor);
|
---|
64 |
|
---|
65 | // create parameter macro visitor (if necessary)
|
---|
66 | if (typeof this.parameterMacro === 'function') {
|
---|
67 | const parameterMacroVisitor = ParameterMacroVisitor({
|
---|
68 | parameterMacro: this.parameterMacro,
|
---|
69 | options
|
---|
70 | });
|
---|
71 | visitors.push(parameterMacroVisitor);
|
---|
72 | }
|
---|
73 |
|
---|
74 | // create model property macro visitor (if necessary)
|
---|
75 | if (typeof this.modelPropertyMacro === 'function') {
|
---|
76 | const modelPropertyMacroVisitor = ModelPropertyMacroVisitor({
|
---|
77 | modelPropertyMacro: this.modelPropertyMacro,
|
---|
78 | options
|
---|
79 | });
|
---|
80 | visitors.push(modelPropertyMacroVisitor);
|
---|
81 | }
|
---|
82 |
|
---|
83 | // create allOf visitor (if necessary)
|
---|
84 | if (this.mode !== 'strict') {
|
---|
85 | const allOfVisitor = AllOfVisitor({
|
---|
86 | options
|
---|
87 | });
|
---|
88 | visitors.push(allOfVisitor);
|
---|
89 | }
|
---|
90 |
|
---|
91 | // establish root visitor by visitor merging
|
---|
92 | const rootVisitor = mergeAllVisitors(visitors, {
|
---|
93 | nodeTypeGetter: getNodeType
|
---|
94 | });
|
---|
95 | const dereferencedElement = await visitAsync(refSet.rootRef.value, rootVisitor, {
|
---|
96 | keyMap,
|
---|
97 | nodeTypeGetter: getNodeType
|
---|
98 | });
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Release all memory if this refSet was not provided as a configuration option.
|
---|
102 | * If provided as configuration option, then provider is responsible for cleanup.
|
---|
103 | */
|
---|
104 | if (options.dereference.refSet === null) {
|
---|
105 | refSet.clean();
|
---|
106 | }
|
---|
107 | return dereferencedElement;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | });
|
---|
111 | export default OpenApi3_1SwaggerClientDereferenceStrategy;
|
---|
112 | /* eslint-enable camelcase */ |
---|