|
4 | 4 | isPrimitiveElement,
|
5 | 5 | isStringElement,
|
6 | 6 | visit,
|
| 7 | + toValue, |
7 | 8 | includesClasses,
|
8 | 9 | } from '@swagger-api/apidom-core';
|
9 | 10 | import {
|
@@ -40,17 +41,28 @@ import {
|
40 | 41 |
|
41 | 42 | const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')];
|
42 | 43 |
|
43 |
| -const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.compose({ |
44 |
| - props: { |
45 |
| - useCircularStructures: true, |
46 |
| - allowMetaPatches: false, |
47 |
| - }, |
48 |
| - init({ useCircularStructures, allowMetaPatches }) { |
| 44 | +const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.init( |
| 45 | + function _OpenApi3_1SwaggerClientDereferenceVisitor({ |
| 46 | + useCircularStructures = true, |
| 47 | + allowMetaPatches = false, |
| 48 | + parameterMacro = null, |
| 49 | + }) { |
| 50 | + const instance = this; |
| 51 | + let parameterMacroOperation = null; |
| 52 | + |
| 53 | + // props |
49 | 54 | this.useCircularStructures = useCircularStructures;
|
50 | 55 | this.allowMetaPatches = allowMetaPatches;
|
51 |
| - }, |
52 |
| - methods: { |
53 |
| - async ReferenceElement(referenceElement, key, parent, path, ancestors) { |
| 56 | + this.parameterMacro = parameterMacro; |
| 57 | + |
| 58 | + // methods |
| 59 | + this.ReferenceElement = async function _ReferenceElement( |
| 60 | + referenceElement, |
| 61 | + key, |
| 62 | + parent, |
| 63 | + path, |
| 64 | + ancestors |
| 65 | + ) { |
54 | 66 | const [ancestorsLineage, directAncestors] = this.toAncestorLineage(ancestors);
|
55 | 67 |
|
56 | 68 | // skip already identified cycled Path Item Objects
|
@@ -119,6 +131,7 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
|
119 | 131 | ancestors: ancestorsLineage,
|
120 | 132 | allowMetaPatches: this.allowMetaPatches,
|
121 | 133 | useCircularStructures: this.useCircularStructures,
|
| 134 | + parameterMacro: this.parameterMacro, |
122 | 135 | });
|
123 | 136 | fragment = await visitAsync(fragment, visitor, { keyMap, nodeTypeGetter: getNodeType });
|
124 | 137 |
|
@@ -176,9 +189,15 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
|
176 | 189 |
|
177 | 190 | // transclude the element for a fragment
|
178 | 191 | return fragment;
|
179 |
| - }, |
180 |
| - |
181 |
| - async PathItemElement(pathItemElement, key, parent, path, ancestors) { |
| 192 | + }; |
| 193 | + |
| 194 | + this.PathItemElement = async function _PathItemElement( |
| 195 | + pathItemElement, |
| 196 | + key, |
| 197 | + parent, |
| 198 | + path, |
| 199 | + ancestors |
| 200 | + ) { |
182 | 201 | const [ancestorsLineage, directAncestors] = this.toAncestorLineage(ancestors);
|
183 | 202 |
|
184 | 203 | // ignore PathItemElement without $ref field
|
@@ -242,6 +261,7 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
|
242 | 261 | ancestors: ancestorsLineage,
|
243 | 262 | allowMetaPatches: this.allowMetaPatches,
|
244 | 263 | useCircularStructures: this.useCircularStructures,
|
| 264 | + parameterMacro: this.parameterMacro, |
245 | 265 | });
|
246 | 266 | referencedElement = await visitAsync(referencedElement, visitor, {
|
247 | 267 | keyMap,
|
@@ -302,9 +322,15 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
|
302 | 322 |
|
303 | 323 | // transclude referencing element with merged referenced element
|
304 | 324 | return mergedPathItemElement;
|
305 |
| - }, |
306 |
| - |
307 |
| - async SchemaElement(referencingElement, key, parent, path, ancestors) { |
| 325 | + }; |
| 326 | + |
| 327 | + this.SchemaElement = async function _SchemaElement( |
| 328 | + referencingElement, |
| 329 | + key, |
| 330 | + parent, |
| 331 | + path, |
| 332 | + ancestors |
| 333 | + ) { |
308 | 334 | const [ancestorsLineage, directAncestors] = this.toAncestorLineage(ancestors);
|
309 | 335 |
|
310 | 336 | // skip current referencing schema as $ref keyword was not defined
|
@@ -414,6 +440,7 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
|
414 | 440 | options: this.options,
|
415 | 441 | useCircularStructures: this.useCircularStructures,
|
416 | 442 | allowMetaPatches: this.allowMetaPatches,
|
| 443 | + parameterMacro: this.parameterMacro, |
417 | 444 | ancestors: ancestorsLineage,
|
418 | 445 | });
|
419 | 446 | referencedElement = await visitAsync(referencedElement, mergeVisitor, {
|
@@ -490,9 +517,31 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
|
490 | 517 |
|
491 | 518 | // transclude referencing element with merged referenced element
|
492 | 519 | return mergedSchemaElement;
|
493 |
| - }, |
494 |
| - }, |
495 |
| -}); |
| 520 | + }; |
| 521 | + |
| 522 | + this.OperationElement = { |
| 523 | + enter(operationElement) { |
| 524 | + parameterMacroOperation = operationElement; |
| 525 | + }, |
| 526 | + leave() { |
| 527 | + parameterMacroOperation = null; |
| 528 | + }, |
| 529 | + }; |
| 530 | + |
| 531 | + this.ParameterElement = { |
| 532 | + leave(parameterElement) { |
| 533 | + if (typeof instance.parameterMacro !== 'function') return; |
| 534 | + |
| 535 | + const pojoOperation = |
| 536 | + parameterMacroOperation === null ? null : toValue(parameterMacroOperation); |
| 537 | + const pojoParameter = toValue(parameterElement); |
| 538 | + const defaultValue = instance.parameterMacro(pojoOperation, pojoParameter); |
| 539 | + |
| 540 | + parameterElement.set('default', defaultValue); |
| 541 | + }, |
| 542 | + }; |
| 543 | + } |
| 544 | +); |
496 | 545 |
|
497 | 546 | export default OpenApi3_1SwaggerClientDereferenceVisitor;
|
498 | 547 | /* eslint-enable camelcase */
|
0 commit comments