diff --git a/packages/swagger-faker/src/FakerGenerator.ts b/packages/swagger-faker/src/FakerGenerator.ts index 1b50366ce..2bf83b489 100644 --- a/packages/swagger-faker/src/FakerGenerator.ts +++ b/packages/swagger-faker/src/FakerGenerator.ts @@ -243,18 +243,18 @@ export class FakerGenerator extends Generator { - if (extensionKey in schema) { + const extensionEnums = (['x-enumNames', 'x-enum-varnames'] as Array) + .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] } diff --git a/packages/swagger-ts/src/TypeGenerator.ts b/packages/swagger-ts/src/TypeGenerator.ts index 88a784df4..907748afd 100644 --- a/packages/swagger-ts/src/TypeGenerator.ts +++ b/packages/swagger-ts/src/TypeGenerator.ts @@ -280,16 +280,16 @@ export class TypeGenerator extends Generator [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 = (['x-enumNames', 'x-enum-varnames'] as Array) + .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] } diff --git a/packages/swagger-zod/src/ZodGenerator.ts b/packages/swagger-zod/src/ZodGenerator.ts index cc793e274..f1c1075d2 100644 --- a/packages/swagger-zod/src/ZodGenerator.ts +++ b/packages/swagger-zod/src/ZodGenerator.ts @@ -249,19 +249,19 @@ export class ZodGenerator extends Generator { - 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) + .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] }