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] Fix incorrect enum literal case #7378

Merged
merged 3 commits into from
Sep 9, 2020
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 @@ -690,13 +690,13 @@ private String getNameUsingEnumPropertyNaming(String name) {
case original:
return name;
case camelCase:
return camelize(name, true);
return camelize(underscore(name), true);
case PascalCase:
return camelize(name);
return camelize(underscore(name));
case snake_case:
return underscore(name);
case UPPERCASE:
return name.toUpperCase(Locale.ROOT);
return underscore(name).toUpperCase(Locale.ROOT);
default:
throw new IllegalArgumentException("Unsupported enum property naming: '" + name);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.openapitools.codegen.typescript;

import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.languages.TypeScriptAxiosClientCodegen;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

public class TypeScriptAxiosClientCodegenTestTest {

TypeScriptAxiosClientCodegen codegen = new TypeScriptAxiosClientCodegen();

@Test
public void testToEnumVarNameOriginalNamingType() {
codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.original.name());
codegen.processOpts();
assertEquals(codegen.toEnumVarName("SCIENCE", "string"), "SCIENCE");
assertEquals(codegen.toEnumVarName("SCIENCE_FICTION", "string"), "SCIENCE_FICTION");
assertEquals(codegen.toEnumVarName("science", "string"), "science");
assertEquals(codegen.toEnumVarName("science_fiction", "string"), "science_fiction");
assertEquals(codegen.toEnumVarName("scienceFiction", "string"), "scienceFiction");
assertEquals(codegen.toEnumVarName("ScienceFiction", "string"), "ScienceFiction");
assertEquals(codegen.toEnumVarName("A", "string"), "A");
assertEquals(codegen.toEnumVarName("b", "string"), "b");
}

@Test
public void testToEnumVarNameCamelCaseNamingType() {
codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase.name());
codegen.processOpts();
assertEquals(codegen.toEnumVarName("SCIENCE", "string"), "science");
assertEquals(codegen.toEnumVarName("SCIENCE_FICTION", "string"), "scienceFiction");
assertEquals(codegen.toEnumVarName("science", "string"), "science");
assertEquals(codegen.toEnumVarName("science_fiction", "string"), "scienceFiction");
assertEquals(codegen.toEnumVarName("scienceFiction", "string"), "scienceFiction");
assertEquals(codegen.toEnumVarName("ScienceFiction", "string"), "scienceFiction");
}

@Test
public void testToEnumVarNamePascalCaseNamingType() {
codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.PascalCase.name());
codegen.processOpts();
assertEquals(codegen.toEnumVarName("SCIENCE", "string"), "Science");
assertEquals(codegen.toEnumVarName("SCIENCE_FICTION", "string"), "ScienceFiction");
assertEquals(codegen.toEnumVarName("science", "string"), "Science");
assertEquals(codegen.toEnumVarName("science_fiction", "string"), "ScienceFiction");
assertEquals(codegen.toEnumVarName("scienceFiction", "string"), "ScienceFiction");
assertEquals(codegen.toEnumVarName("ScienceFiction", "string"), "ScienceFiction");
assertEquals(codegen.toEnumVarName("A", "string"), "A");
assertEquals(codegen.toEnumVarName("b", "string"), "B");
}

@Test
public void testToEnumVarNameSnakeCaseNamingType() {
codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.snake_case.name());
codegen.processOpts();
assertEquals(codegen.toEnumVarName("SCIENCE", "string"), "science");
assertEquals(codegen.toEnumVarName("SCIENCE_FICTION", "string"), "science_fiction");
assertEquals(codegen.toEnumVarName("science", "string"), "science");
assertEquals(codegen.toEnumVarName("science_fiction", "string"), "science_fiction");
assertEquals(codegen.toEnumVarName("scienceFiction", "string"), "science_fiction");
assertEquals(codegen.toEnumVarName("ScienceFiction", "string"), "science_fiction");
assertEquals(codegen.toEnumVarName("A", "string"), "a");
assertEquals(codegen.toEnumVarName("b", "string"), "b");
}

@Test
public void testToEnumVarNameUpperCaseNamingType() {
codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.UPPERCASE.name());
codegen.processOpts();
assertEquals(codegen.toEnumVarName("SCIENCE", "string"), "SCIENCE");
assertEquals(codegen.toEnumVarName("SCIENCE_FICTION", "string"), "SCIENCE_FICTION");
assertEquals(codegen.toEnumVarName("science", "string"), "SCIENCE");
assertEquals(codegen.toEnumVarName("science_fiction", "string"), "SCIENCE_FICTION");
assertEquals(codegen.toEnumVarName("scienceFiction", "string"), "SCIENCE_FICTION");
assertEquals(codegen.toEnumVarName("ScienceFiction", "string"), "SCIENCE_FICTION");
assertEquals(codegen.toEnumVarName("A", "string"), "A");
assertEquals(codegen.toEnumVarName("b", "string"), "B");
}

}