Skip to content

[BUG] [kotlin-client] numeric enums deserialize by ordinal instead of value #22534

@ramononis

Description

@ramononis
Description

Kotlin client enums generated with the Jackson serialization library are deserialized by ordinal instead of by the numeric value defined in the OpenAPI schema. When a server legitimately returns -1 for an enum whose values
are [-1, 0, 1], Jackson throws InvalidFormatException: index value outside legal index range [0..2].

openapi-generator version

7.17.0 (regression vs Java templates, which already emit the proper annotations)

OpenAPI declaration file content or url
  openapi: 3.0.0
  info:
    title: Numeric enum repro
    version: latest
  paths:
    /example:
      get:
        operationId: getExample
        responses:
          '200':
            description: ok
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/ExampleModel'
  components:
    schemas:
      ExampleModel:
        type: object
        required:
          - source
        properties:
          source:
            $ref: '#/components/schemas/ExampleNumericEnum'
      ExampleNumericEnum:
        type: integer
        format: int32
        enum:
          - -1
          - 0
          - 1
Generation Details

CLI (but same via Gradle/Maven plugin):

openapi-generator-cli generate -g kotlin -i numeric-enum.yaml -o out --library jvm-retrofit2 --additional-properties=serializationLibrary=jackson
Steps to reproduce
  1. Generate the Kotlin client as above.
  2. Call the generated API when the server responds with {"source":-1}.
  3. Observe Jackson throwing InvalidFormatException: index value outside legal index range [0..2] because ExampleNumericEnum lacks @JsonCreator/@jsonvalue.
Related issues/PRs
Suggest a fix

Annotate Kotlin enums the same way the Java templates already do: add @get:JsonValue to the backing value and @JsonCreator @JvmStatic to the factory method so Jackson binds numeric values by value instead of by ordinal.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions