swagger-codegen-generators icon indicating copy to clipboard operation
swagger-codegen-generators copied to clipboard

[kotlin] Enum naming does not keep underscores

Open takn opened this issue 3 years ago • 1 comments

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);

    }

takn avatar Mar 24 '21 21:03 takn

Any option to configure this kind of thing? This tool and the OpenApiGenerator seems to have a problem with translating the OAS into idiomatic code. I could like to configure so that PascalCase as an OAS enum value for instance gets a Java enum of PASCAL_CASE("PascalCase") but there doesn't seem to be this level of control and you end up with stuff like PASCALCASE("PascalCase") - irritating. Any ideas on better toolings or more cfg options?

Johnlon avatar Jan 15 '22 00:01 Johnlon