Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typescript] Prevent generating invalid enum code due to empty variable names #20699

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,25 @@ public String toEnumVarName(String name, String datatype) {

if (enumName.matches("\\d.*")) { // starts with number
return "_" + enumName;
} else {
return enumName;
}

if (enumName.isEmpty()) {
// After sanitizing *all* characters (e.g. multibyte characters), the var name becomes an empty string.
// 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.
// For backward compatibility, this additional sanitization is only applied if the original sanitized name is empty.
final String sanitized = sanitizeNameForTypeScriptSymbol(name);
if (sanitized.isEmpty()) {
// After re-sanitizing, this pads a pseudo var name ("STRING") if still the name is empty.
return "STRING";
}
return "_" + sanitized;
}

return enumName;
}

private String sanitizeNameForTypeScriptSymbol(String name) {
return sanitizeName(name, "[^\\p{L}\\p{Nd}\\$_]");
}

private String getNameWithEnumPropertyNaming(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.*;
import org.openapitools.codegen.*;
import org.openapitools.codegen.config.CodegenConfigurator;
import org.openapitools.codegen.languages.TypeScriptClientCodegen;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.utils.ModelUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -179,4 +183,31 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {

Assert.assertEquals(codegen.getLicenseName(), licenseName);
}

@Test
public void testForAllSanitizedEnum() throws Exception {
final File output = Files.createTempDirectory("typescriptnodeclient_").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("typescript")
.setInputSpec("src/test/resources/bugs/typescript_enum_var_name_all_sanitized.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
final DefaultGenerator generator = new DefaultGenerator();
final List<File> files = generator.opts(clientOptInput).generate();
files.forEach(File::deleteOnExit);

TestUtils.assertFileContains(
Paths.get(output + "/models/Greeting.ts"),
"export enum Greeting {\n" +
" _こんにちは = 'こんにちは',\n" +
" _你好 = '你好',\n" +
" _안녕하세요 = '안녕하세요',\n" +
" STRING = '!@#%',\n" +
" STRING2 = '^&*\uD83C\uDF63'",
"}"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
openapi: "3.0.0"
info:
title: Sample project
version: '1.0'
description: 'Sample API Check "API Key" '
license:
name: Apache 2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0'
paths: {}
components:
schemas:
Greeting:
type: string
enum:
- 'こんにちは'
- '你好'
- '안녕하세요'
- '!@#%'
- '^&*🍣'
Loading