|
1 | 1 | import { isObjectElement, toValue } from '@swagger-api/apidom-core';
|
2 | 2 |
|
3 |
| -const ModelPropertyMacroVisitor = ({ modelPropertyMacro }) => ({ |
4 |
| - SchemaElement: { |
5 |
| - leave(schemaElement) { |
6 |
| - if (typeof schemaElement.properties === 'undefined') return; |
7 |
| - if (!isObjectElement(schemaElement.properties)) return; |
| 3 | +import compose from '../utils/compose.js'; |
| 4 | +import toPath from '../utils/to-path.js'; |
8 | 5 |
|
9 |
| - schemaElement.properties.forEach((property) => { |
10 |
| - if (!isObjectElement(property)) return; |
| 6 | +const ModelPropertyMacroVisitor = compose({ |
| 7 | + init({ modelPropertyMacro, options }) { |
| 8 | + this.modelPropertyMacro = modelPropertyMacro; |
| 9 | + this.options = options; |
| 10 | + }, |
| 11 | + props: { |
| 12 | + modelPropertyMacro: null, |
| 13 | + options: null, |
| 14 | + SchemaElement: { |
| 15 | + leave(schemaElement, key, parent, path, ancestors) { |
| 16 | + if (typeof schemaElement.properties === 'undefined') return; |
| 17 | + if (!isObjectElement(schemaElement.properties)) return; |
| 18 | + |
| 19 | + schemaElement.properties.forEach((property) => { |
| 20 | + if (!isObjectElement(property)) return; |
11 | 21 |
|
12 |
| - property.set('default', modelPropertyMacro(toValue(property))); |
13 |
| - }); |
| 22 | + try { |
| 23 | + const macroValue = this.modelPropertyMacro(toValue(property)); |
| 24 | + property.set('default', macroValue); |
| 25 | + } catch (error) { |
| 26 | + const macroError = new Error(error, { cause: error }); |
| 27 | + macroError.fullPath = [...toPath([...ancestors, parent, schemaElement]), 'properties']; |
| 28 | + this.options.dereference.dereferenceOpts?.errors?.push?.(macroError); |
| 29 | + } |
| 30 | + }); |
| 31 | + }, |
14 | 32 | },
|
15 | 33 | },
|
16 | 34 | });
|
|
0 commit comments