Skip to content

Commit

Permalink
fix(core): Referencing combined enums results in a duplicate schema n…
Browse files Browse the repository at this point in the history
…ame error (#1488)
  • Loading branch information
lnkarma authored Jul 4, 2024
1 parent d782619 commit 3b27e47
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
20 changes: 7 additions & 13 deletions packages/core/src/getters/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,26 +173,20 @@ export const combineSchemas = ({
if (isAllEnums && name && items.length > 1) {
const newEnum = `// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const ${pascal(
name,
)} = ${getCombineEnumValue(resolvedData)};\n`;
)} = ${getCombineEnumValue(resolvedData)}`;

return {
value: `typeof ${pascal(name)}[keyof typeof ${pascal(name)}] ${nullable}`,
value: `typeof ${pascal(name)}[keyof typeof ${pascal(name)}] ${nullable}\n\n${newEnum}`,
imports: [
{
name: pascal(name),
},
...resolvedData.imports.map<GeneratorImport>((toImport) => ({
...toImport,
values: true,
})),
],
schemas: [
...resolvedData.schemas,
{
imports: resolvedData.imports.map<GeneratorImport>((toImport) => ({
...toImport,
values: true,
})),
model: newEnum,
name: pascal(name),
},
],
schemas: [...resolvedData.schemas],
isEnum: false,
type: 'object' as SchemaType,
isRef: false,
Expand Down
8 changes: 8 additions & 0 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,12 @@ export default defineConfig({
target: '../generated/default/http-status-mocks/endpoints.ts',
},
},
'combined-enum': {
input: '../specifications/combined-enum.yaml',
output: {
schemas: '../generated/default/combine-enum/schemas',
target: '../generated/default/combine-enum',
mock: true,
},
},
});
32 changes: 32 additions & 0 deletions tests/specifications/combined-enum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
openapi: 3.0.3
info:
title: Combined enums
version: 1.0.0
paths:
/api/colors:
get:
summary: sample colors
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ColorObject'
components:
schemas:
Colors1:
type: string
enum: [red, blue, yellow]
Colors2:
type: string
enum: [green, purple, orange]
Colors:
oneOf:
- $ref: '#/components/schemas/Colors1'
- $ref: '#/components/schemas/Colors2'
ColorObject:
type: object
properties:
color:
$ref: '#/components/schemas/Colors'

0 comments on commit 3b27e47

Please sign in to comment.