OpenAPITools / OpenAPITools/openapi-generator

[BUG][KOTLIN] Numeric enum generates @SerializedName with numeric values instead of using x-enum-varnames (or human-friendly names)

Open
#22,773 0 comments 0 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

When generating Kotlin models (library: jvm-retrofit2, gson), an integer backed enum which includes x-enum-varnames and x-enum-descriptions produces enum entries where @SerializedName equals the numeric enum values (e.g., "12", "20").

Expected behavior is to have @SerializedName use the string names provided via x-enum-varnames, while keeping the underlying val value: Int as the wire value.

👉 See documentation

openapi-generator version
  • 7.16.0
  • 7.20.0-SNAPSHOT (commit : cc045ab)
OpenAPI declaration file content or url

Minimal reproducible spec:

openapi: 3.1.0
info:
  title: Bar API
  version: 1.0.0
components:
  schemas:
    GlassSize:
      description: Size of the glass to order.
      type: integer
      enum:
        - 20
        - 12
        - 16
        - 40
      x-enum-descriptions:
        - 'Pint - Conical with bulge near top'
        - 'Pilsner - Slender, hourglass shape'
        - 'Stemmed - Short stem with wide, short bowl'
        - 'Mug - Straight sides with handle; may feature dimples'
      x-enum-varnames:
        - Pint
        - Pilsner
        - Stemmed
        - Mug
      default: 12
Generation Details

Config:

generatorName: 'kotlin'
library: 'jvm-retrofit2'
additionalProperties:
  enumPropertyNaming: 'UPPERCASE' # 'original'
  serializationLibrary: 'gson'

CLI:


openapi-generator generate \
  -g kotlin \
  -i example-api.yml \
  -o ./generated \
  -c config.yaml
Steps to reproduce
  1. Save the YAML spec above as example-api.yml.
  2. Save the config as config.yaml.
  3. Run the CLI command shown above.
  4. Inspect the generated GlassSize enum.

Actual output

enum class GlassSize(val value: kotlin.Int) {

    @SerializedName(value = "20")
    Pint(20),

    @SerializedName(value = "12")
    Pilsner(12),

    @SerializedName(value = "16")
    Stemmed(16),

    @SerializedName(value = "40")
    Mug(40);

    // ...
}

Notes:
Even with enumPropertyNaming: UPPERCASE, the enum identifiers aren’t uppercased in this case (secondary issue), and
@SerializedName is set to the numeric values as strings.

Expected output

enum class GlassSize(val value: kotlin.Int) {

    @SerializedName(value = "Pint")
    PINT(20),

    @SerializedName(value = "Pilsner")
    PILSNER(12),

    @SerializedName(value = "Stemmed")
    STEMMED(16),

    @SerializedName(value = "Mug")
    MUG(40);

    // ...
}
Related issues/PRs
Suggest a fix

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 example-api.yml spec, config.yaml, and the shown openapi-generator CLI command. Inspect Kotlin jvm-retrofit2 Gson enum generation for how x-enum-varnames and numeric enum values are used in GlassSize. Done means @SerializedName uses the human-readable names, enum identifiers honor UPPERCASE, and val value remains Int with the numeric wire values.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.