Skip to content

Kotlin enum class does not compile - enum value equal to enum name #1815

Description

@gregopet

I tried creating types from multiple examples returned by an API. The result was a Kotlin file that did not compile. It creates an enum class where one of the enum values has the same name as the enum itself which will not work (in general, JVM enum values should not be CamelCased; SCREAMING_SNAKE_CASE or lowercaseCamelCase are preferred).

The generated code is:

enum class Category(val value: String) {
    Category(" "), // Does not compile because enum value is same as the class name
    Empty("\u0000");

    companion object {
        fun fromValue(value: String): Category = when (value) {
            " "      -> Category
            "\u0000" -> Empty
            else     -> throw IllegalArgumentException()
        }
    }
}

The JSON input that produces this error are a bunch of files with a property that contain only two possible values (omitting the rest here for brevity): { "category" : " " } and { "category" : "\u0000" }. I invoked quicktype with npx quicktype --src someclass --src-lang json --lang kotlin --package com.example --framework jackson --out src/main/kotlin/someclass.kt

I've uploaded the above code (plus enough .json files that enum conversion happens) to https://publicstash.s3.us-west-1.amazonaws.com/quicktype-bug-report.tar.gz

The package includes the generation code (the quicktype-convert script) as well as a quick Gradle file that starts a build (just run ./gradlew compileKotlin, you only need a recent Java installed.. and use gradlew.bat if on Windows).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions