Skip to content

Commit edc3b9d

Browse files
committed
Use new sanitizer for TypeScript symbol which takes wider variety characters for enum var name
Signed-off-by: moznion <[email protected]>
1 parent 1b8747e commit edc3b9d

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,23 @@ public String toEnumVarName(String name, String datatype) {
315315

316316
if (enumName.isEmpty()) {
317317
// After sanitizing *all* characters (e.g. multibyte characters), the var name becomes an empty string.
318-
// An empty string would cause a syntax error, so this pads the pseudo var name to avoid that.
319-
return "STRING";
318+
// An empty string would cause a syntax error, so this code attempts to re-sanitize the name using another sanitizer that allows a wider variety of characters.
319+
// For backward compatibility, this additional sanitization is only applied if the original sanitized name is empty.
320+
final String sanitized = sanitizeNameForTypeScriptSymbol(name);
321+
if (sanitized.isEmpty()) {
322+
// After re-sanitizing, this pads a pseudo var name ("STRING") if still the name is empty.
323+
return "STRING";
324+
}
325+
return "_" + sanitized;
320326
}
321327

322328
return enumName;
323329
}
324330

331+
private String sanitizeNameForTypeScriptSymbol(String name) {
332+
return sanitizeName(name, "[^\\p{L}\\p{Nd}\\$_]");
333+
}
334+
325335
private String getNameWithEnumPropertyNaming(String name) {
326336
switch (getEnumPropertyNaming()) {
327337
case original:

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/TypeScriptClientCodegenTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,11 @@ public void testForAllSanitizedEnum() throws Exception {
202202
TestUtils.assertFileContains(
203203
Paths.get(output + "/models/Greeting.ts"),
204204
"export enum Greeting {\n" +
205-
" STRING = 'こんにちは',\n" +
206-
" STRING2 = '你好',\n" +
207-
" STRING3 = '안녕하세요'\n" +
205+
" _こんにちは = 'こんにちは',\n" +
206+
" _你好 = '你好',\n" +
207+
" _안녕하세요 = '안녕하세요',\n" +
208+
" STRING = '!@#%',\n" +
209+
" STRING2 = '^&*\uD83C\uDF63'",
208210
"}"
209211
);
210212
}

modules/openapi-generator/src/test/resources/bugs/typescript_enum_var_name_all_sanitized.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ components:
1515
- 'こんにちは'
1616
- '你好'
1717
- '안녕하세요'
18+
- '!@#%'
19+
- '^&*🍣'

0 commit comments

Comments
 (0)