diff --git a/packages/core/src/getters/combine.ts b/packages/core/src/getters/combine.ts index 2a9dd03f2..3d1462625 100644 --- a/packages/core/src/getters/combine.ts +++ b/packages/core/src/getters/combine.ts @@ -157,7 +157,7 @@ export const combineSchemas = ({ const isAllEnums = resolvedData.isEnum.every((v) => v); - let resolvedValue; + let resolvedValue: ScalarValue | undefined; if (schema.properties) { resolvedValue = getScalar({ item: omit(schema, separator), name, context }); @@ -171,19 +171,29 @@ export const combineSchemas = ({ }); if (isAllEnums && name && items.length > 1) { - const newEnum = `\n\n// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const ${pascal( + const newEnum = `// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const ${pascal( name, - )} = ${getCombineEnumValue(resolvedData)}`; + )} = ${getCombineEnumValue(resolvedData)};\n`; return { - value: - `typeof ${pascal(name)}[keyof typeof ${pascal(name)}] ${nullable};` + - newEnum, - imports: resolvedData.imports.map((toImport) => ({ - ...toImport, - values: true, - })), - schemas: resolvedData.schemas, + value: `typeof ${pascal(name)}[keyof typeof ${pascal(name)}] ${nullable}`, + imports: [ + ...resolvedData.imports.map((toImport) => ({ + ...toImport, + values: true, + })), + { + name: pascal(name), + }, + ], + schemas: [ + ...resolvedData.schemas, + { + imports: [], + model: newEnum, + name: pascal(name), + }, + ], isEnum: false, type: 'object' as SchemaType, isRef: false, diff --git a/tests/configs/default.config.ts b/tests/configs/default.config.ts index e67db0925..473453939 100644 --- a/tests/configs/default.config.ts +++ b/tests/configs/default.config.ts @@ -87,6 +87,14 @@ export default defineConfig({ mock: true, }, }, + 'any-of': { + input: '../specifications/any-of.yaml', + output: { + schemas: '../generated/default/any-of/model', + target: '../generated/default/any-of/endpoints.ts', + mock: true, + }, + }, 'deeply-nested-refs': { input: '../specifications/deeply-nested-refs.yaml', output: { diff --git a/tests/specifications/any-of.yaml b/tests/specifications/any-of.yaml new file mode 100644 index 000000000..62b47daf5 --- /dev/null +++ b/tests/specifications/any-of.yaml @@ -0,0 +1,22 @@ +openapi: '3.0.0' +info: + version: 1.0.0 + title: AnyOf Schema + license: + name: MIT +paths: + /test: + get: + summary: Gets one of the items + operationId: getItems + parameters: + - name: test + in: query + schema: + anyOf: + - type: string + enum: ['A'] + - type: string + enum: ['B'] + - type: string + enum: ['C']