-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Closed
Closed
Copy link
Milestone
Description
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
- Generate the Kotlin client as above.
- Call the generated API when the server responds with {"source":-1}.
- Observe Jackson throwing InvalidFormatException: index value outside legal index range [0..2] because ExampleNumericEnum lacks @JsonCreator/@jsonvalue.
Related issues/PRs
- Similar to, but distinct from, [Bug][Java] jaxrs-cxf-client missing annotation when generating Enum #5077 (which was fixed for Java clients only).
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.
Reactions are currently unavailable