OpenAPITools / OpenAPITools/openapi-generator

[BUG][KOTLIN] Naming conflict among enum entries in case of integer values

Open
#22,340 0 comments 3 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?
  • 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 have their values as an integer number can have unresolved naming conflicts, which cause compilation errors. The problem is that when the generator chooses a name for the enum entry, it doesn't take into account the sign of the integer value. Meaning that values, for example, 12 and -12 will get the same name - _12.

Please note that such an issue exists in the latest versions. For example, in 6.5.0, there is no such problem.

openapi-generator version

7.16.0

OpenAPI declaration file content or url
{
  "openapi": "3.0.1",
  "info": {
    "title": "Test.Api",
    "version": "1.0.0"
  },
  "components": {
    "schemas": {
      "IntegerEnum": {
        "enum": [
          0,
          1,
          12,
          122,
          -122,
          -12,
          -1
        ],
        "type": "integer",
        "format": "int32"
      }
    }
  }
}

results into:

// ...

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
 * 
 *
 * Values: _0,_1,_12,_122,_12222,_1222,_1222
 */

@JsonClass(generateAdapter = false)
enum class IntegerEnum(val value: kotlin.Int) {

    @Json(name = "0")
    _0(0),

    @Json(name = "1")
    _1(1),

    @Json(name = "12")
    _12(12),

    @Json(name = "122")
    _122(122),

    @Json(name = "-122")
    _12222(-122),

    @Json(name = "-12")
    _1222(-12),

    @Json(name = "-1")
    _1222(-1);
}
Image
Suggestion

Suggestion is to follow such naming - _12 for 12 (positive number), _m12 for -12 (negative number).

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 issue with the OpenAPI declaration in the report and inspect the Kotlin enum generation path. Compare generated names for positive and negative integer values, including the listed collisions. Done means the generated enum entries have distinct valid Kotlin names while preserving their original serialized values.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.