Skip to content

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

@mamupf

Description

@mamupf

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions