Swagger-codegen: Bug in generating kotlin enums

Created on 27 Oct 2017  路  8Comments  路  Source: swagger-api/swagger-codegen

Description

Given the following enum declarations in the yaml file:

      authorizationType:
        enum:
        - ldap
        - internal
        type: string
      eventPublishSubscriptionMode:
        enum:
        - 'off'
        - on-with-format-v1
        - on-with-no-unsubscribe-events-on-disconnect-format-v1
        - on-with-format-v2
        - on-with-no-unsubscribe-events-on-disconnect-format-v2
 ```

The kotlin code generated has errors.

``` kotlin
    enum class AuthorizationType(val value: kotlin.String) {
        ldap("ldap"),
        internal("internal");  // internal is a keyword in kotlin
    }

    enum class EventPublishSubscriptionMode(val value: kotlin.String) {
        off("off"),
        on-with-format-v1("on-with-format-v1"),  // We cannot use '-' here
        on-with-no-unsubscribe-events-on-disconnect-format-v1("on-with-no-unsubscribe-events-on-disconnect-format-v1"),
        on-with-format-v2("on-with-format-v2"),
        on-with-no-unsubscribe-events-on-disconnect-format-v2("on-with-no-unsubscribe-events-on-disconnect-format-v2");
    }
Swagger-codegen version

2.2.3 and master

Swagger declaration file content or url

http://sftp.solace.com/download/VMR_SEMPV2_SCHEMA_YAML
http://sftp.solace.com/download/VMR_SEMPV2_SCHEMA_JSON

Command line used for generation

java -jar swagger-codegen-cli.jar generate -l kotlin -i semp-v2-swagger-config.yaml

Suggest a fix/enhancement

Not sure how to fix this, the Java code generation looks correct when dealing with enums:

``` java
@JsonAdapter(AuthorizationTypeEnum.Adapter.class)
public enum AuthorizationTypeEnum {
LDAP("ldap"),
INTERNAL("internal");
}
@JsonAdapter(EventPublishSubscriptionModeEnum.Adapter.class)
public enum EventPublishSubscriptionModeEnum {
OFF("off"),
ON_WITH_FORMAT_V1("on-with-format-v1"),
ON_WITH_NO_UNSUBSCRIBE_EVENTS_ON_DISCONNECT_FORMAT_V1("on-with-no-unsubscribe-events-on-disconnect-format-v1"),
ON_WITH_FORMAT_V2("on-with-format-v2"),
ON_WITH_NO_UNSUBSCRIBE_EVENTS_ON_DISCONNECT_FORMAT_V2("on-with-no-unsubscribe-events-on-disconnect-format-v2");
}

Kotlin Enum Bug

All 8 comments

Looks like I'll need to clean up the enums to be all caps, numbers and underscores and ensure the names start with a letter.

This should also include some tests for enums per #5730 TODO item, which can probably be made as integration tests similar to those I added in #6851.

@wari I've opened #6858. Are you able to evaluate whether this resolves your issue?

To match the Java enum properties you listed, you should be able to do:

--additional-properties=enumPropertyNaming=UPPERCASE

Once my changes for integration tests from #6851 are merged, I'll create some integration tests for Kotlin enums as well.

I can't seem to build when I checkout your copy:

$git remote add jims https://github.com/jimschubert/swagger-codegen
$git fetch jims
$git checkout jims/kotlin-enum-6830
$git reset --hard
HEAD is now at 57ad6be65 [kotlin] Nested enum naming fix, and naming options via CLI


$mvn clean package
...
...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] swagger-codegen-project ............................ SUCCESS [  1.554 s]
[INFO] swagger-codegen (core library) ..................... FAILURE [ 23.483 s]
[INFO] swagger-codegen (executable) ....................... SKIPPED
[INFO] swagger-codegen (maven-plugin) ..................... SKIPPED
[INFO] swagger-generator .................................. SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

@wari Sorry about that, I forgot to push the test for the new option. I've pushed, so you should be able to build now.

Hi Jim,

I found that UPPERCASE and PascalCase works well.

The others fails, because kotlin keywords (like internal) are not treated specially, which I do believe will happen for certain enum words. PS: Yes I know how tedious it would be if you want to take care of all languages special/internal keywords. But at least, there's an option to override it.

Thanks.

@wari Thanks for the further testing. I thought enum names were escaped prior to the meeting I've overridden, but it seems I'll need to check and escape reserved words here as well. for instance, INTERNAL in a spec with camelCase would become internal.

@wari thanks again for all that testing. I've pushed a change that escapes all keywords and special characters documented on https://kotlinlang.org/docs/reference/keyword-reference.html. There was also an issue where multiple special characters would all be converted to _("value"), but _,__, and __ are all special characters in Kotlin so I've fixed this issue as well. You've helped make the Kotlin enum support way more solid, and I appreciate that!

PR by @jimschubert has been merged into master.

Was this page helpful?
0 / 5 - 0 ratings