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 2 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,18 +690,35 @@ private String getNameUsingEnumPropertyNaming(String name) {
case original:
return name;
case camelCase:
return camelize(name, true);
return camelize(prepareEnumString(name), true);
case PascalCase:
return camelize(name);
return camelize(prepareEnumString(name));
case snake_case:
return underscore(name);
case UPPERCASE:
return name.toUpperCase(Locale.ROOT);
return prepareEnumString(name).toUpperCase(Locale.ROOT);
default:
throw new IllegalArgumentException("Unsupported enum property naming: '" + name);
}
}

private String prepareEnumString(String name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add some docs to explain what this function does, and include an example

I am just asking myself: why can't we use the underscore method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can use the underscore method. I deleted prepareEnumString method. Thanks.

if (name == null || name.isEmpty()) {
return name;
}

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(name.charAt(0));
for (int i = 1; i < name.length(); i++) {
if (Character.isLowerCase(name.charAt(i - 1)) && Character.isUpperCase(name.charAt(i))) {
stringBuilder.append('_').append(name.charAt(i));
} else {
stringBuilder.append(name.charAt(i));
}
}
return StringUtils.lowerCase(stringBuilder.toString());
}

@Override
protected void addImport(CodegenModel m, String type) {
if (type == null) {
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");
}

}