swagger-api / swagger-api/swagger-codegen-generators
[kotlin] Enum naming does not keep underscores
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 299
- Forks
- 439
- PR merge metrics
- No merged PRs in 30d
Description
Problem: When a enum containing underscores is used in the spec, the underscores are removed.
Example spec:
StatusType:
type: string
enum:
- APPROVAL_REQUIRED
- PENDING_APPROVAL
- APPROVED
- DENIED
- FAILED
Results in
enum class StatusType(val value: kotlin.String){
APPROVALREQUIRED("APPROVAL_REQUIRED"),// :/
PENDINGAPPROVAL("PENDING_APPROVAL"),// :/
APPROVED("APPROVED"),// :/
DENIED("DENIED"),// :/
FAILED("FAILED");// :/
}
Expected result should be:
enum class StatusType(val value: kotlin.String){
APPROVAL_REQUIRED("APPROVAL_REQUIRED"),// :/
PENDING_APPROVAL("PENDING_APPROVAL"),// :/
APPROVED("APPROVED"),// :/
DENIED("DENIED"),// :/
FAILED("FAILED");// :/
}
The problem is in codegen.toEnumVarName
I added a test condition to KotlinClientCodegenModelTest.sanitizeEnumVarNames() {"VALUE_1", "VALUE_1"}, to verify and the test fails..
The problem can be traced to this method:
/**
* Remove characters not suitable for variable or method name from the input and camelize it
*
* @param name string to be camelize
* @return camelized string
*/
@SuppressWarnings("static-method")
public String removeNonNameElementToCamelCase(String name) {
return removeNonNameElementToCamelCase(name, "[-_:;#]");
}
Note that there is an underscore passed in to removeNonNameElementToCamelCase.
The tests pass if I remove the underscore, but that's probably not the ideal solution...
return removeNonNameElementToCamelCase(name, "[-:;#]");
Full test below for reference.
@DataProvider
public static Object[][] enumNames() {
return new Object[][]{
{"VALUE_1", "VALUE_1"},
{"VALUE1", "VALUE1"},
{"1", "_1"},
{"1X2", "_1X2"},
{"1x2", "_1X2"}
};
}
@Test(dataProvider = "enumNames", description = "sanitize Enum var names")
public void sanitizeEnumVarNames(final String name, final String expectedName) {
final KotlinClientCodegen codegen = new KotlinClientCodegen();
Assert.assertEquals(codegen.toEnumVarName(name, "String"), expectedName);
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with KotlinClientCodegenModelTest.sanitizeEnumVarNames() and the codegen.toEnumVarName entry point, then inspect removeNonNameElementToCamelCase(String). Run the named enum-name test with the VALUE_1 case; done means underscores are preserved in generated enum identifiers while numeric names still receive the expected prefix.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100