diff --git a/e2e/kubb.config.js b/e2e/kubb.config.js index 40a4576ae..0ade3cb71 100644 --- a/e2e/kubb.config.js +++ b/e2e/kubb.config.js @@ -15,6 +15,7 @@ const schemas = [ ['requestBody', './schemas/requestBody.yaml'], ['box', './schemas/box.json'], ['digitalocean', './schemas/digitalocean.yaml'], + ['enums', './schemas/enums.yaml'] ] /** @type {import('@kubb/core').KubbUserConfig} */ diff --git a/e2e/schemas/enums.yaml b/e2e/schemas/enums.yaml new file mode 100644 index 000000000..c3cb89d0a --- /dev/null +++ b/e2e/schemas/enums.yaml @@ -0,0 +1,39 @@ +definitions: + enumVarNames.Type: + enum: + - 0 + - 1 + type: integer + x-enum-varnames: + - Pending + - Received + enumNames.Type: + enum: + - 0 + - 1 + x-enumNames: + - Pending + - Received +swagger: "2.0" +info: + title: test + version: "1.0.0" +paths: + /var-names: + get: + consumes: + - application/json + responses: + 200: + description: Success + schema: + $ref: '#/definitions/enumVarNames.Type' + /names: + get: + consumes: + - application/json + responses: + 200: + description: Success + schema: + $ref: '#/definitions/enumNames.Type' \ No newline at end of file diff --git a/packages/swagger-faker/src/FakerGenerator.ts b/packages/swagger-faker/src/FakerGenerator.ts index e1655e2b5..1b50366ce 100644 --- a/packages/swagger-faker/src/FakerGenerator.ts +++ b/packages/swagger-faker/src/FakerGenerator.ts @@ -243,15 +243,22 @@ export class FakerGenerator extends Generator `\`${value}\``).join(', ')}]`], - }, - ] + const extensionEnums = ['x-enumNames', 'x-enum-varnames'].map(extensionKey => { + if (extensionKey in schema) { + return [ + { + keyword: fakerKeywords.enum, + args: [`[${[...new Set(schema[extensionKey] as string[])].map((value) => `\`${value}\``).join(', ')}]`], + }, + ] + } + }) + + if (extensionEnums.length > 0) { + return extensionEnums[0] } + if (schema.type === 'number' || schema.type === 'integer') { return [ { diff --git a/packages/swagger-ts/src/TypeGenerator.ts b/packages/swagger-ts/src/TypeGenerator.ts index 00c2714c2..88a784df4 100644 --- a/packages/swagger-ts/src/TypeGenerator.ts +++ b/packages/swagger-ts/src/TypeGenerator.ts @@ -280,12 +280,20 @@ export class TypeGenerator extends Generator [key, key]) - if ('x-enumNames' in schema) { - enums = [...new Set(schema['x-enumNames'] as string[])].map((key: string, index) => { - return [key, schema.enum?.[index] as string] - }) + 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] + }) + } + }) + + if (extensionEnums.length > 0) { + return extensionEnums[0] } + + this.extraNodes.push( ...factory.createEnumDeclaration({ name: transformers.camelCase(enumName), diff --git a/packages/swagger-zod/src/ZodGenerator.ts b/packages/swagger-zod/src/ZodGenerator.ts index 1a628bbf4..cc793e274 100644 --- a/packages/swagger-zod/src/ZodGenerator.ts +++ b/packages/swagger-zod/src/ZodGenerator.ts @@ -249,16 +249,23 @@ export class ZodGenerator extends Generator `\`${value}\``), - }, - ...baseItems, - ] + 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, + ] + } + }) + + if (extensionEnums.length > 0) { + return extensionEnums[0] } + if (schema.type === 'number' || schema.type === 'integer') { // we cannot use z.enum when enum type is number/integer return [