Skip to content

Commit 32c84a4

Browse files
committed
fix(typescript): Fix handling of string numbers in enums
1 parent 13d5fb8 commit 32c84a4

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

plugins/typescript/src/core/schemaToEnumDeclaration.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ describe("schemaToTypeAliasDeclaration", () => {
3535
`);
3636
});
3737

38+
it("should generate enums string (numbers)", () => {
39+
const schema: SchemaObject = {
40+
type: "integer",
41+
enum: ["0", "00", "01", "1", "-1", "-01", "2", "3.33"],
42+
};
43+
44+
expect(printSchema(schema)).toMatchInlineSnapshot(`
45+
"export enum Test {
46+
Zero = "0",
47+
"00" = "00",
48+
"01" = "01",
49+
One = "1",
50+
MinusOne = "-1",
51+
"-01" = "-01",
52+
Two = "2",
53+
ThreePointThreeThree = "3.33"
54+
}"
55+
`);
56+
});
57+
3858
it("should generate a int enum (using big numbers)", () => {
3959
const schema: SchemaObject = {
4060
type: "string",

plugins/typescript/src/core/schemaToEnumDeclaration.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ function getEnumMembers(schema: SchemaObject): ts.EnumMember[] {
6363
let enumValueNode: ts.Expression | undefined = undefined;
6464

6565
if (typeof enumValue === "string") {
66-
enumName = enumValue;
66+
if (String(Number(enumValue)) === enumValue) {
67+
enumName = convertNumberToWord(Number(enumValue));
68+
} else {
69+
enumName = enumValue;
70+
}
6771
enumValueNode = f.createStringLiteral(enumValue);
6872
} else if (typeof enumValue === "number") {
6973
enumName = convertNumberToWord(enumValue)

plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ describe("schemaToTypeAliasDeclaration", () => {
216216
expect(printSchema(schema)).toBe(`export type Test = 1 | 2 | 3;`);
217217
});
218218

219+
it("should generate enums string (numbers)", () => {
220+
const schema: SchemaObject = {
221+
type: "integer",
222+
enum: ["1", "2", "3"],
223+
};
224+
225+
expect(printSchema(schema)).toBe(`export type Test = "1" | "2" | "3";`);
226+
});
227+
219228
it("should skip example which contains `*/` to avoid confusion", () => {
220229
const schema: SchemaObject = {
221230
title: "CronTimingCreate",

plugins/typescript/src/utils/getEnumProperties.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ const tens: string[] = [
9393
];
9494

9595
export const convertNumberToWord = (n: number): string => {
96+
if (n < 0) {
97+
return "minus " + convertNumberToWord(-n);
98+
}
99+
100+
if (!Number.isInteger(n)) {
101+
const [intPart, fracPart] = n.toString().split(".");
102+
return (
103+
convertNumberToWord(Number(intPart)) +
104+
" point " +
105+
fracPart
106+
.split("")
107+
.map((digit) => ones[Number(digit)])
108+
.join(" ")
109+
);
110+
}
111+
96112
if (n < 20) {
97113
return ones[n];
98114
}

0 commit comments

Comments
 (0)