OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Kotlin] enums declared in the model have incorrect names

Open
#18,714 2 comments 5 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Enums that are declared inside models are generated with incorrect names.

openapi-generator version

7.5.0 - issue appeared
7.6.0 - issue reproduced
7.7.0 - 7.15.0 - issue exists

OpenAPI declaration file content or url
{
  "openapi": "3.0.1",
  "info": {
    "title": "Test.Api",
    "version": "1.0.0"
  },
  "components": {
    "schemas": {
      "User.Model": {
        "required": [
          "userType",
          "name"
        ],
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "age": {
            "type": "integer"
          },
          "userType": {
            "enum": [
              "MALE",
              "FEMALE",
              "ANIMAL"
            ],
            "type": "string"
          }
        }
      }
    }
  }
}

results into:

data class UserModel (

    @Json(name = "name")
    val name: kotlin.String,

    @Json(name = "userType")
    val userType: UserModel.UserType,

    @Json(name = "age")
    val age: kotlin.Int? = null

) {
    enum class userType(val value: kotlin.String) {
        @Json(name = "MALE") MALE("MALE"),
        @Json(name = "FEMALE") FEMALE("FEMALE"),
        @Json(name = "ANIMAL") ANIMAL("ANIMAL");
    }
}

here enum name conflicts with the property name giving an error:
image

Generation Details
        task.additionalProperties = [
                useCoroutines: "true",
                library      : "jvm-retrofit2",
                sourceFolder : "src/main/java",
        ]
        task.globalProperties = [
                apiDocs        : "false",
                apiTests       : "false",
                modelDocs      : "false",
                modelTests     : "false",
                apis           : "",
                models         : "",
                supportingFiles: "CollectionFormats.kt"
        ]
        task.configOptions = [
                dateLibrary       : "java8-localdatetime",
                enumPropertyNaming: "UPPERCASE",
                moshiCodeGen      : "true"
        ]
        task.typeMappings = [
                "URI"                     : "kotlin.String",
                "ByteArray"               : "kotlin.String",
        ]

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

Start by reproducing the supplied OpenAPI declaration with the Kotlin generator using the listed Gradle settings, especially the jvm-retrofit2 library and Moshi code generation. Compare the generated UserModel output with the property and nested-enum naming logic; done means the generated Kotlin model compiles without the enum name conflicting with userType.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.