Skip to content

Commit 31b628f

Browse files
parsaghksmorimoto
andauthored
Fix enum key generation for values like ENUM_123_VALUE_456 (#1260)
* fix: keep underscores in enum names with numbers * chore: add more use cases * chore: add changeset * Update .changeset/big-sheep-see.md --------- Co-authored-by: Sora Morimoto <[email protected]>
1 parent 3fd6224 commit 31b628f

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

.changeset/big-sheep-see.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"swagger-typescript-api": patch
3+
---
4+
5+
Fix enum key generation for values like `ENUM_123_VALUE_456`.

src/type-name-formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class TypeNameFormatter {
3232
}
3333

3434
// constant names like LEFT_ARROW, RIGHT_FORWARD, ETC_KEY, _KEY_NUM_
35-
if (/^([A-Z_]{1,})$/g.test(name)) {
35+
if (/^(?!\d)([A-Z0-9_]{1,})$/g.test(name)) {
3636
return lodash.compact([typePrefix, name, typeSuffix]).join("_");
3737
}
3838

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`basic > use x-enumNames as the key for enum 1`] = `
4+
"/* eslint-disable */
5+
/* tslint:disable */
6+
// @ts-nocheck
7+
/*
8+
* ---------------------------------------------------------------
9+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
10+
* ## ##
11+
* ## AUTHOR: acacode ##
12+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
13+
* ---------------------------------------------------------------
14+
*/
15+
16+
export enum StringEnumIncludesNumbersAndUnderscore {
17+
VAL_1 = "VAL_1",
18+
VAL_2 = "VAL_2",
19+
VAL_3 = "VAL_3",
20+
_1_VAL = "_1_VAL",
21+
A_1_B_2_C_3 = "A_1_B_2_C_3",
22+
_A_1_B_2_C_3 = "_A_1_B_2_C_3",
23+
_1_A_2_B_3_C = "_1_A_2_B_3_C",
24+
}
25+
"
26+
`;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as fs from "node:fs/promises";
2+
import * as os from "node:os";
3+
import * as path from "node:path";
4+
5+
import { afterAll, beforeAll, describe, expect, test } from "vitest";
6+
7+
import { generateApi } from "../../../src/index.js";
8+
9+
describe("basic", async () => {
10+
let tmpdir = "";
11+
12+
beforeAll(async () => {
13+
tmpdir = await fs.mkdtemp(path.join(os.tmpdir(), "swagger-typescript-api"));
14+
});
15+
16+
afterAll(async () => {
17+
await fs.rm(tmpdir, { recursive: true });
18+
});
19+
20+
test("use x-enumNames as the key for enum", async () => {
21+
await generateApi({
22+
fileName: "schema",
23+
input: path.resolve(import.meta.dirname, "schema.json"),
24+
output: tmpdir,
25+
silent: true,
26+
enumNamesAsValues: false,
27+
generateClient: false,
28+
});
29+
30+
const content = await fs.readFile(path.join(tmpdir, "schema.ts"), {
31+
encoding: "utf8",
32+
});
33+
console.log(path.join(tmpdir, "schema.ts"));
34+
35+
expect(content).toMatchSnapshot();
36+
});
37+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"openapi": "3.0.0",
3+
"tags": [],
4+
"servers": [],
5+
"components": {
6+
"schemas": {
7+
"StringEnumIncludesNumbersAndUnderscore": {
8+
"type": "string",
9+
"enum": [
10+
"VAL_1",
11+
"VAL_2",
12+
"VAL_3",
13+
"_1_VAL",
14+
"A_1_B_2_C_3",
15+
"_A_1_B_2_C_3",
16+
"_1_A_2_B_3_C"
17+
]
18+
}
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)