diff --git a/.changeset/big-sheep-see.md b/.changeset/big-sheep-see.md new file mode 100644 index 00000000..097a890d --- /dev/null +++ b/.changeset/big-sheep-see.md @@ -0,0 +1,5 @@ +--- +"swagger-typescript-api": patch +--- + +Fix enum key generation for values like `ENUM_123_VALUE_456`. diff --git a/src/type-name-formatter.ts b/src/type-name-formatter.ts index 4c5d3595..58743a86 100644 --- a/src/type-name-formatter.ts +++ b/src/type-name-formatter.ts @@ -32,7 +32,7 @@ export class TypeNameFormatter { } // constant names like LEFT_ARROW, RIGHT_FORWARD, ETC_KEY, _KEY_NUM_ - if (/^([A-Z_]{1,})$/g.test(name)) { + if (/^(?!\d)([A-Z0-9_]{1,})$/g.test(name)) { return lodash.compact([typePrefix, name, typeSuffix]).join("_"); } diff --git a/tests/spec/enumIncludesNumber/__snapshots__/basic.test.ts.snap b/tests/spec/enumIncludesNumber/__snapshots__/basic.test.ts.snap new file mode 100644 index 00000000..2542a62e --- /dev/null +++ b/tests/spec/enumIncludesNumber/__snapshots__/basic.test.ts.snap @@ -0,0 +1,26 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`basic > use x-enumNames as the key for enum 1`] = ` +"/* eslint-disable */ +/* tslint:disable */ +// @ts-nocheck +/* + * --------------------------------------------------------------- + * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ## + * ## ## + * ## AUTHOR: acacode ## + * ## SOURCE: https://github.com/acacode/swagger-typescript-api ## + * --------------------------------------------------------------- + */ + +export enum StringEnumIncludesNumbersAndUnderscore { + VAL_1 = "VAL_1", + VAL_2 = "VAL_2", + VAL_3 = "VAL_3", + _1_VAL = "_1_VAL", + A_1_B_2_C_3 = "A_1_B_2_C_3", + _A_1_B_2_C_3 = "_A_1_B_2_C_3", + _1_A_2_B_3_C = "_1_A_2_B_3_C", +} +" +`; diff --git a/tests/spec/enumIncludesNumber/basic.test.ts b/tests/spec/enumIncludesNumber/basic.test.ts new file mode 100644 index 00000000..a58b9489 --- /dev/null +++ b/tests/spec/enumIncludesNumber/basic.test.ts @@ -0,0 +1,37 @@ +import * as fs from "node:fs/promises"; +import * as os from "node:os"; +import * as path from "node:path"; + +import { afterAll, beforeAll, describe, expect, test } from "vitest"; + +import { generateApi } from "../../../src/index.js"; + +describe("basic", async () => { + let tmpdir = ""; + + beforeAll(async () => { + tmpdir = await fs.mkdtemp(path.join(os.tmpdir(), "swagger-typescript-api")); + }); + + afterAll(async () => { + await fs.rm(tmpdir, { recursive: true }); + }); + + test("use x-enumNames as the key for enum", async () => { + await generateApi({ + fileName: "schema", + input: path.resolve(import.meta.dirname, "schema.json"), + output: tmpdir, + silent: true, + enumNamesAsValues: false, + generateClient: false, + }); + + const content = await fs.readFile(path.join(tmpdir, "schema.ts"), { + encoding: "utf8", + }); + console.log(path.join(tmpdir, "schema.ts")); + + expect(content).toMatchSnapshot(); + }); +}); diff --git a/tests/spec/enumIncludesNumber/schema.json b/tests/spec/enumIncludesNumber/schema.json new file mode 100644 index 00000000..40ae9f46 --- /dev/null +++ b/tests/spec/enumIncludesNumber/schema.json @@ -0,0 +1,21 @@ +{ + "openapi": "3.0.0", + "tags": [], + "servers": [], + "components": { + "schemas": { + "StringEnumIncludesNumbersAndUnderscore": { + "type": "string", + "enum": [ + "VAL_1", + "VAL_2", + "VAL_3", + "_1_VAL", + "A_1_B_2_C_3", + "_A_1_B_2_C_3", + "_1_A_2_B_3_C" + ] + } + } + } +}