From 50908b1f743de612cb66debd27dfcb5fe0539fb8 Mon Sep 17 00:00:00 2001 From: Hikaru Yoshino Date: Mon, 15 Jul 2024 12:47:57 +0900 Subject: [PATCH 1/4] chore(test): add InvalidPropertyNameChars with enum flag --- packages/openapi-typescript/test/node-api.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/openapi-typescript/test/node-api.test.ts b/packages/openapi-typescript/test/node-api.test.ts index ef29893b7..a751e8380 100644 --- a/packages/openapi-typescript/test/node-api.test.ts +++ b/packages/openapi-typescript/test/node-api.test.ts @@ -617,6 +617,10 @@ export type operations = Record;`, "x-enumNames": ["Uno", "Dos", "Tres"], "x-enumDescriptions": ["El número uno", "El número dos", "El número tres"], }, + InvalidPropertyNameChars: { + type: "string", + enum: ["=", "!=", ">", "~", "^", "TE=ST"], + } }, }, }, @@ -660,6 +664,8 @@ export interface components { XEnumVarnames: XEnumVarnames; /** @enum {number} */ XEnumNames: XEnumNames; + /** @enum {string} */ + InvalidPropertyNameChars: InvalidPropertyNameChars; }; responses: never; parameters: never; @@ -700,6 +706,14 @@ export enum XEnumNames { // El número tres Tres = 3 } +export enum InvalidPropertyNameChars { + "=" = "=", + "!=" = "!=", + ">" = ">", + "~" = "~", + "^" = "^", + TE_ST = "TE=ST" +} export type operations = Record;`, options: { enum: true }, }, From ff6880e64df74da00857eac41e8154fbad8fb290 Mon Sep 17 00:00:00 2001 From: Hikaru Yoshino Date: Mon, 15 Jul 2024 12:50:36 +0900 Subject: [PATCH 2/4] fix: Quote the name if it consists entirely of invalid characters and replace only the invalid characters with "_" if they are part of the name --- packages/openapi-typescript/src/lib/ts.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/openapi-typescript/src/lib/ts.ts b/packages/openapi-typescript/src/lib/ts.ts index f0c3f9313..3b2c75555 100644 --- a/packages/openapi-typescript/src/lib/ts.ts +++ b/packages/openapi-typescript/src/lib/ts.ts @@ -285,7 +285,15 @@ export function tsEnumMember(value: string | number, metadata: { name?: string; } else if (name[0] === "-") { name = `ValueMinus${name.slice(1)}`; } - name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, "_"); + + const invalidCharMatch = name.match(JS_PROPERTY_INDEX_INVALID_CHARS_RE); + if (invalidCharMatch) { + if (invalidCharMatch[0] === name) { + name = `"${name}"`; + } else { + name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, "_"); + } + } } let member: ts.EnumMember; From 2f5eb63fdfaaaf6dcecadf1891eccda949148c22 Mon Sep 17 00:00:00 2001 From: Hikaru Yoshino Date: Mon, 15 Jul 2024 13:13:26 +0900 Subject: [PATCH 3/4] chore: fix lint error --- packages/openapi-typescript/test/node-api.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openapi-typescript/test/node-api.test.ts b/packages/openapi-typescript/test/node-api.test.ts index a751e8380..96c226cca 100644 --- a/packages/openapi-typescript/test/node-api.test.ts +++ b/packages/openapi-typescript/test/node-api.test.ts @@ -620,7 +620,7 @@ export type operations = Record;`, InvalidPropertyNameChars: { type: "string", enum: ["=", "!=", ">", "~", "^", "TE=ST"], - } + }, }, }, }, From 59c60334fd6669b53b7353960659b001f0f6b1e8 Mon Sep 17 00:00:00 2001 From: Hikaru Yoshino Date: Wed, 17 Jul 2024 00:14:37 +0900 Subject: [PATCH 4/4] chore: add changeset patch md --- .changeset/brave-lizards-juggle.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/brave-lizards-juggle.md diff --git a/.changeset/brave-lizards-juggle.md b/.changeset/brave-lizards-juggle.md new file mode 100644 index 000000000..d6f6faf0d --- /dev/null +++ b/.changeset/brave-lizards-juggle.md @@ -0,0 +1,5 @@ +--- +"openapi-typescript": patch +--- + +fix: Enum option cannot generate enums if values are not valid property names