diff --git a/e2e/kubb.config.js b/e2e/kubb.config.js index 0ade3cb71..d9a762c9b 100644 --- a/e2e/kubb.config.js +++ b/e2e/kubb.config.js @@ -15,7 +15,7 @@ const schemas = [ ['requestBody', './schemas/requestBody.yaml'], ['box', './schemas/box.json'], ['digitalocean', './schemas/digitalocean.yaml'], - ['enums', './schemas/enums.yaml'] + ['enums', './schemas/enums.yaml'], ] /** @type {import('@kubb/core').KubbUserConfig} */ diff --git a/packages/swagger-faker/mocks/enums.yaml b/packages/swagger-faker/mocks/enums.yaml new file mode 100644 index 000000000..c3cb89d0a --- /dev/null +++ b/packages/swagger-faker/mocks/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.test.ts b/packages/swagger-faker/src/FakerGenerator.test.ts new file mode 100644 index 000000000..1efc18e78 --- /dev/null +++ b/packages/swagger-faker/src/FakerGenerator.test.ts @@ -0,0 +1,33 @@ +import { OasManager } from '@kubb/swagger' +import { FakerGenerator } from './FakerGenerator' +import path from 'node:path' +import { mockedPluginManager } from '@kubb/core/mocks' +import type { OasTypes } from '@kubb/swagger/oas' + +describe('FakeGenerator enums', async () => { + const schemaPath = path.resolve(__dirname, '../mocks/enums.yaml') + const oas = await new OasManager().parse(schemaPath) + const generator = new FakerGenerator({ + dateType: 'string', + mapper: {}, + seed: 1, + transformers: {}, + }, { + oas, + pluginManager: mockedPluginManager, + }) + + const schemas = oas.getDefinition().components?.schemas + + test('generate x-enum-varnames types', async () => { + const node = generator.build({ schema: schemas?.['enumVarNames.Type'] as OasTypes.SchemaObject, baseName: 'enumVarNames' }) + + expect(node).toMatchSnapshot() + }) + + test('generate x-enumNames types', async () => { + const node = generator.build({ schema: schemas?.['enumNames.Type'] as OasTypes.SchemaObject, baseName: 'enumNames' }) + + expect(node).toMatchSnapshot() + }) +}) diff --git a/packages/swagger-faker/src/FakerGenerator.ts b/packages/swagger-faker/src/FakerGenerator.ts index 44720f393..3c73f7570 100644 --- a/packages/swagger-faker/src/FakerGenerator.ts +++ b/packages/swagger-faker/src/FakerGenerator.ts @@ -244,22 +244,19 @@ export class FakerGenerator extends Generator) - .filter(extensionKey => extensionKey in schema) - .map((extensionKey) => { - return [ - { - keyword: fakerKeywords.enum, - args: [`[${[...new Set(schema[extensionKey] as string[])].map((value) => `\`${value}\``).join(', ')}]`], - }, - ] - }) - + const extensionEnums = ['x-enumNames', 'x-enum-varnames'] + .filter(extensionKey => extensionKey in schema) + .map((extensionKey) => [ + { + keyword: fakerKeywords.enum, + args: [`[${[...new Set(schema[extensionKey as keyof typeof schema] as string[])].map((value) => `\`${value}\``).join(', ')}]`], + }, + ]) + if (extensionEnums.length > 0 && extensionEnums[0]) { return extensionEnums[0] } - if (schema.type === 'number' || schema.type === 'integer') { return [ { diff --git a/packages/swagger-faker/src/__snapshots__/FakerGenerator.test.ts.snap b/packages/swagger-faker/src/__snapshots__/FakerGenerator.test.ts.snap new file mode 100644 index 000000000..0eb02d41d --- /dev/null +++ b/packages/swagger-faker/src/__snapshots__/FakerGenerator.test.ts.snap @@ -0,0 +1,23 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`FakeGenerator enums > generate x-enum-varnames types 1`] = ` +[ + " +export function enumVarNames(): NonNullable { + faker.seed(1) + return faker.helpers.arrayElement([\`Pending\`, \`Received\`]); +} + ", +] +`; + +exports[`FakeGenerator enums > generate x-enumNames types 1`] = ` +[ + " +export function enumNames(): NonNullable { + faker.seed(1) + return faker.helpers.arrayElement([\`Pending\`, \`Received\`]); +} + ", +] +`; diff --git a/packages/swagger-ts/src/TypeGenerator.test.ts b/packages/swagger-ts/src/TypeGenerator.test.ts index f19a643e9..b9d050f58 100644 --- a/packages/swagger-ts/src/TypeGenerator.test.ts +++ b/packages/swagger-ts/src/TypeGenerator.test.ts @@ -314,7 +314,6 @@ describe('TypeGenerator with discriminators', async () => { }) }) - describe('TypeGenerator enums', async () => { const schemaPath = path.resolve(__dirname, '../mocks/enums.yaml') const oas = await new OasManager().parse(schemaPath) @@ -332,10 +331,7 @@ describe('TypeGenerator enums', async () => { const schemas = oas.getDefinition().components?.schemas - test('generate x-enum-varnames types', async () => { - - const node = generator.build({ schema: schemas?.['enumVarNames.Type'] as OasTypes.SchemaObject, baseName: 'enumVarNames' }) const output = print(node, undefined) @@ -346,8 +342,6 @@ describe('TypeGenerator enums', async () => { }) test('generate x-enumNames types', async () => { - - const node = generator.build({ schema: schemas?.['enumNames.Type'] as OasTypes.SchemaObject, baseName: 'enumNames' }) const output = print(node, undefined) @@ -356,4 +350,4 @@ describe('TypeGenerator enums', async () => { expect(await format(output)).toMatchSnapshot() }) -}) \ No newline at end of file +}) diff --git a/packages/swagger-ts/src/TypeGenerator.ts b/packages/swagger-ts/src/TypeGenerator.ts index 907748afd..ee99ae82f 100644 --- a/packages/swagger-ts/src/TypeGenerator.ts +++ b/packages/swagger-ts/src/TypeGenerator.ts @@ -280,20 +280,16 @@ export class TypeGenerator extends Generator [key, key]) - const extensionEnums : Array = (['x-enumNames', 'x-enum-varnames'] as Array) + const extensionEnums: Array = ['x-enumNames', 'x-enum-varnames'] .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 - }) - }) - + .map((extensionKey) => + [...new Set(schema[extensionKey as keyof typeof schema] as string[])].map((key, index) => [key, schema.enum?.[index] as string] as const) + ) + if (extensionEnums.length > 0 && extensionEnums[0]) { enums = extensionEnums[0] } - - this.extraNodes.push( ...factory.createEnumDeclaration({ name: transformers.camelCase(enumName), diff --git a/packages/swagger-zod/src/ZodGenerator.test.ts b/packages/swagger-zod/src/ZodGenerator.test.ts index e23969d3e..064d33cce 100644 --- a/packages/swagger-zod/src/ZodGenerator.test.ts +++ b/packages/swagger-zod/src/ZodGenerator.test.ts @@ -123,8 +123,6 @@ describe('ZodGenerator lazy import', async () => { }) }) - - describe('ZodGenerator enums', async () => { const schemaPath = path.resolve(__dirname, '../mocks/enums.yaml') const oas = await new OasManager().parse(schemaPath) @@ -140,7 +138,6 @@ describe('ZodGenerator enums', async () => { const schemas = oas.getDefinition().components?.schemas - test('generate x-enum-varnames types', async () => { const node = generator.build({ schema: schemas?.['enumVarNames.Type'] as OasTypes.SchemaObject, baseName: 'enumVarNames' }) @@ -152,4 +149,4 @@ describe('ZodGenerator enums', async () => { expect(node).toMatchSnapshot() }) -}) \ No newline at end of file +}) diff --git a/packages/swagger-zod/src/ZodGenerator.ts b/packages/swagger-zod/src/ZodGenerator.ts index e06f8f4e9..386fa1d80 100644 --- a/packages/swagger-zod/src/ZodGenerator.ts +++ b/packages/swagger-zod/src/ZodGenerator.ts @@ -249,23 +249,22 @@ export class ZodGenerator extends Generator) + const extensionEnums = ['x-enumNames', 'x-enum-varnames'] .filter(extensionKey => extensionKey in schema) .map((extensionKey) => { - return [ - { - keyword: zodKeywords.enum, - args: [...new Set(schema[extensionKey] as string[])].map((value: string) => `\`${value}\``), - }, - ...baseItems, - ] + return [ + { + keyword: zodKeywords.enum, + args: [...new Set(schema[extensionKey as keyof typeof schema] as string[])].map((value: string) => `\`${value}\``), + }, + ...baseItems, + ] }) - + if (extensionEnums.length > 0 && extensionEnums[0]) { return extensionEnums[0] } - if (schema.type === 'number' || schema.type === 'integer') { // we cannot use z.enum when enum type is number/integer return [