OpenAPITools / OpenAPITools/openapi-generator

[BUG] Uppercase enum field causes kotlin code generation to fail compile

Open
#14,510 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

If you have an embedded enum such as the following:

          "OS": {
            "type": "string",
            "description": "the device operating system",
            "example": "IOS",
            "enum": [
              "IOS",
              "ANDROID",
              "WATCH_OS"
            ]
          },

The kotlin code generator will generate both a field and inner class name of "OS". This causes a compilation error, since it can't have both an inner class and a field with the same name.

Conflicting declarations: public final enum class OS : Enum<DeviceSpecificDetails.OS>, public final val OS: DeviceSpecificDetails.OS?

@JsonClass(generateAdapter = true)

data class DeviceSpecificDetails (
    /* the device operating system */
    @Json(name = "OS")
    val OS: DeviceSpecificDetails.OS? = null,
) {
    /**
     * the device operating system
     *
     * Values: IOS,ANDROID,WATCH_OS
     */
    enum class OS(val value: kotlin.String) {
        @Json(name = "IOS") IOS("IOS"),
        @Json(name = "ANDROID") ANDROID("ANDROID"),
        @Json(name = "WATCH_OS") WATCH_OS("WATCH_OS");
    }
openapi-generator version

6.2.1

Generation Details

We are using the following configuration to generate our code:

    openApiGenerate {
        generatorName.set("kotlin")
        inputSpec.set(extension.inputSpec)
        outputDir.set(extension.outputDirPath)
        packageName.set("${extension.rootPackageName}")
        apiPackage.set("${extension.rootPackageName}.api")
        modelPackage.set("${extension.rootPackageName}.models")
        invokerPackage.set("${extension.rootPackageName}.client")
        library.set("jvm-retrofit2")
        configOptions.set(
            mapOf(
                "useCoroutines" to "true",
                "artifactId" to project.name,
                "groupId" to project.group.toString(),
                "artifactVersion" to project.version.toString(),
                "serializationLibrary" to "moshi",
                "moshiCodeGen" to "true",
                "dateLibrary" to "threetenbp",
                "enumPropertyNaming" to "UPPERCASE"
            )
        )
    }
Steps to reproduce

Attempt to generate and then compile the generated code.

Suggest a fix

Java code generation appends an "Enum" to any enum class that is generated (at least for inner classes). I suggest that perhaps the kotlin generate needs to do the same thing, or at least give the user a configuration option for how to handle this sort of case.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce Kotlin generation from the embedded OS enum example using the listed Moshi and enumPropertyNaming settings, then inspect the generated DeviceSpecificDetails declaration. The payload names no source file or test; done means the generated Kotlin code no longer has conflicting field and inner enum declarations and compiles successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.