Skip to content

Commit

Permalink
fix: handle enums
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaim Lev-Ari committed Dec 18, 2023
1 parent e8e6e8c commit a0b43e8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions packages/swagger-faker/src/FakerGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,18 @@ export class FakerGenerator extends Generator<PluginOptions['resolvedOptions'],
}

if (schema.enum) {
const extensionEnums = ['x-enumNames', 'x-enum-varnames'].map(extensionKey => {
if (extensionKey in schema) {
const extensionEnums = (['x-enumNames', 'x-enum-varnames'] as Array<keyof typeof schema>)
.filter(extensionKey => extensionKey in schema)
.map((extensionKey) => {
return [
{
keyword: fakerKeywords.enum,
args: [`[${[...new Set(schema[extensionKey] as string[])].map((value) => `\`${value}\``).join(', ')}]`],
},
]
}
})

if (extensionEnums.length > 0) {
if (extensionEnums.length > 0 && extensionEnums[0]) {
return extensionEnums[0]
}

Expand Down
18 changes: 9 additions & 9 deletions packages/swagger-ts/src/TypeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,16 @@ export class TypeGenerator extends Generator<PluginOptions['resolvedOptions'], C

let enums: [key: string, value: string | number][] = [...new Set(schema.enum)].map((key) => [key, key])

const extensionEnums = ['x-enumNames', 'x-enum-varnames'].map(extensionKey => {
if (extensionKey in schema) {
return [...new Set(schema[extensionKey] as string[])].map((key, index) => {
return [key, schema.enum?.[index] as string]
})
}
})
const extensionEnums : Array<typeof enums> = (['x-enumNames', 'x-enum-varnames'] as Array<keyof typeof schema>)
.filter(extensionKey => extensionKey in schema)
.map((extensionKey) => {
return [...new Set(schema[extensionKey] as string[])].map((key, index) => {
return [key, schema.enum?.[index] as string] as const
})
})

if (extensionEnums.length > 0) {
return extensionEnums[0]
if (extensionEnums.length > 0 && extensionEnums[0]) {
enums = extensionEnums[0]
}


Expand Down
24 changes: 12 additions & 12 deletions packages/swagger-zod/src/ZodGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,19 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co
}

if (schema.enum) {
const extensionEnums = ['x-enumNames', 'x-enum-varnames'].map(extensionKey => {
if (extensionKey in schema) {
return [
{
keyword: zodKeywords.enum,
args: [...new Set(schema[extensionKey] as string[])].map((value: string) => `\`${value}\``),
},
...baseItems,
]
}
})
const extensionEnums = (['x-enumNames', 'x-enum-varnames'] as Array<keyof typeof schema>)
.filter(extensionKey => extensionKey in schema)
.map((extensionKey) => {
return [
{
keyword: zodKeywords.enum,
args: [...new Set(schema[extensionKey] as string[])].map((value: string) => `\`${value}\``),
},
...baseItems,
]
})

if (extensionEnums.length > 0) {
if (extensionEnums.length > 0 && extensionEnums[0]) {
return extensionEnums[0]
}

Expand Down

0 comments on commit a0b43e8

Please sign in to comment.