From 0d516755b969e2f60ad8796ab6abc1e9a596a483 Mon Sep 17 00:00:00 2001 From: Gregory Haddow Date: Mon, 19 Aug 2024 15:20:51 +0100 Subject: [PATCH] fix: fix string enum keys for values with spaces --- plugins/typescript/src/core/schemaToEnumDeclaration.test.ts | 5 +++-- plugins/typescript/src/core/schemaToEnumDeclaration.ts | 1 + .../typescript/src/generators/generateSchemaTypes.test.ts | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/typescript/src/core/schemaToEnumDeclaration.test.ts b/plugins/typescript/src/core/schemaToEnumDeclaration.test.ts index a34c23f..606c5af 100644 --- a/plugins/typescript/src/core/schemaToEnumDeclaration.test.ts +++ b/plugins/typescript/src/core/schemaToEnumDeclaration.test.ts @@ -7,14 +7,15 @@ describe("schemaToTypeAliasDeclaration", () => { it("should generate a string enums", () => { const schema: SchemaObject = { type: "string", - enum: ["AVAILABLE", "PENDING", "SOLD"], + enum: ["AVAILABLE", "PENDING", "SOLD", "WITH SPACE"], }; expect(printSchema(schema)).toMatchInlineSnapshot(` "export enum Test { AVAILABLE = "AVAILABLE", PENDING = "PENDING", - SOLD = "SOLD" + SOLD = "SOLD", + "WITH SPACE" = "WITH SPACE" }" `); }); diff --git a/plugins/typescript/src/core/schemaToEnumDeclaration.ts b/plugins/typescript/src/core/schemaToEnumDeclaration.ts index b497187..29b7604 100644 --- a/plugins/typescript/src/core/schemaToEnumDeclaration.ts +++ b/plugins/typescript/src/core/schemaToEnumDeclaration.ts @@ -48,6 +48,7 @@ function getEnumMembers( if (typeof enumValue === "string") { enumName = enumValue.match(/^\d/) ? `"_${enumValue}"` : enumValue; + enumName = enumName.match(/[^"]+.*\s.*/) ? `"${enumName}"` : enumName; enumValueNode = f.createStringLiteral(enumValue); } else if (typeof enumValue === "number") { enumName = convertNumberToWord(enumValue) diff --git a/plugins/typescript/src/generators/generateSchemaTypes.test.ts b/plugins/typescript/src/generators/generateSchemaTypes.test.ts index 64232dc..6402eb7 100644 --- a/plugins/typescript/src/generators/generateSchemaTypes.test.ts +++ b/plugins/typescript/src/generators/generateSchemaTypes.test.ts @@ -233,7 +233,7 @@ describe("generateSchemaTypes", () => { export enum DogBreed { saimois = "saimois", bengal = "bengal", - british shorthair = "british shorthair" + "british shorthair" = "british shorthair" } /**