Skip to content

Commit 8043c5d

Browse files
authored
fix: Enum option cannot generate enums if values are not valid property names (#1761)
* chore(test): add InvalidPropertyNameChars with enum flag * 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 * chore: fix lint error * chore: add changeset patch md
1 parent 61782d7 commit 8043c5d

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

.changeset/brave-lizards-juggle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
fix: Enum option cannot generate enums if values are not valid property names

packages/openapi-typescript/src/lib/ts.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,15 @@ export function tsEnumMember(value: string | number, metadata: { name?: string;
285285
} else if (name[0] === "-") {
286286
name = `ValueMinus${name.slice(1)}`;
287287
}
288-
name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, "_");
288+
289+
const invalidCharMatch = name.match(JS_PROPERTY_INDEX_INVALID_CHARS_RE);
290+
if (invalidCharMatch) {
291+
if (invalidCharMatch[0] === name) {
292+
name = `"${name}"`;
293+
} else {
294+
name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, "_");
295+
}
296+
}
289297
}
290298

291299
let member: ts.EnumMember;

packages/openapi-typescript/test/node-api.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,10 @@ export type operations = Record<string, never>;`,
617617
"x-enumNames": ["Uno", "Dos", "Tres"],
618618
"x-enumDescriptions": ["El número uno", "El número dos", "El número tres"],
619619
},
620+
InvalidPropertyNameChars: {
621+
type: "string",
622+
enum: ["=", "!=", ">", "~", "^", "TE=ST"],
623+
},
620624
},
621625
},
622626
},
@@ -660,6 +664,8 @@ export interface components {
660664
XEnumVarnames: XEnumVarnames;
661665
/** @enum {number} */
662666
XEnumNames: XEnumNames;
667+
/** @enum {string} */
668+
InvalidPropertyNameChars: InvalidPropertyNameChars;
663669
};
664670
responses: never;
665671
parameters: never;
@@ -700,6 +706,14 @@ export enum XEnumNames {
700706
// El número tres
701707
Tres = 3
702708
}
709+
export enum InvalidPropertyNameChars {
710+
"=" = "=",
711+
"!=" = "!=",
712+
">" = ">",
713+
"~" = "~",
714+
"^" = "^",
715+
TE_ST = "TE=ST"
716+
}
703717
export type operations = Record<string, never>;`,
704718
options: { enum: true },
705719
},

0 commit comments

Comments
 (0)