Skip to content

Commit

Permalink
[typescript] Fix incorrect enum literal case (#7378)
Browse files Browse the repository at this point in the history
* Fix incorrect enum values for typescript clients

* Fix incorrect enum values for typescript clients

* Fix incorrect enum values for typescript clients
  • Loading branch information
Andrey Mochalov authored Sep 9, 2020
1 parent 37de486 commit 4d8ac0e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
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");
}

}

0 comments on commit 4d8ac0e

Please sign in to comment.