-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
PR #22535 introduced a breaking change in v7.18.0 for the Kotlin client generator with Jackson serialization. The change added @JsonCreator annotation to the decode() companion function in enum classes, which causes invalid enum values to be silently converted to null instead of throwing an exception.
This breaks:
- Input validation - Invalid enum values are silently accepted
- Error messages - No exception is thrown for invalid data
- Distinguishing null/absent/invalid - All three cases now result in null
- ObjectMapper configuration - DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL is bypassed
The Kotlin client template doesn't implement the same conditional logic as Java. The Java template respects isNullable and enumUnknownDefaultCase options, while Kotlin always returns null for unknown values.
openapi-generator version
- Broken: 7.18.0
- Working: 7.17.0
OpenAPI declaration file content or url
openapi: "3.0.3"
info:
title: Enum Test API
version: "1.0.0"
paths:
/test:
post:
operationId: testEndpoint
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/MyRequest'
responses:
'200':
description: OK
components:
schemas:
MyEnumType:
type: string
description: An example enum type
enum:
- VALUE_A
- VALUE_B
- VALUE_C
MyRequest:
type: object
properties:
status:
$ref: '#/components/schemas/MyEnumType'Generation Details
Generate a Kotlin client with Jackson serialization using the spec above:
openapi-generator-cli generate \
-i spec.yaml \
-g kotlin \
--additional-properties=serializationLibrary=jackson \
-o output/Steps to reproduce
- Generate Kotlin client code using the command above
- Look at the generated enum class (e.g., MyEnumType.kt) - note the decode() function with @JsonCreator uses firstOrNull which returns null for unknown values
- In your application, deserialize JSON with an invalid enum value:
val mapper = ObjectMapper().registerModule(KotlinModule.Builder().build())
val json = """{"status":"INVALID_VALUE"}"""
val result = mapper.readValue<MyRequest>(json)
// Expected: throws exception
// Actual: result.status == null (silent failure)- v7.17.0: Jackson uses default enum deserialization → throws InvalidFormatException
- v7.18.0: Jackson uses @JsonCreator decode() → silently returns null
Related issues/PRs
- [Java] new option: Throw an exception for invalid enum values instead of using null #625: Java: Throw exception for invalid enums instead of null -> Fixed in 3.2.0
- [REQ] [kotlin-client] Support Enum Fallback Values #12970: Kotlin: Support enum fallback values -> Open
- [BUG] [kotlin-client] numeric enums deserialize by ordinal instead of value #22534: Kotlin: Numeric enums deserialize by ordinal -> Fixed by fix(kotlin): add JsonCreator/JsonValue to Jackson enums #22535
The Java generator went through the same discussion in #625 and established that:
- Default behavior: Throw
IllegalArgumentExceptionfor unknown enum values - Option
enumUnknownDefaultCase: Return a default value if enabled - Nullable handling: Return null only if the schema allows nullable
Suggest a fix
Modify modules/openapi-generator/src/main/resources/kotlin-client/enum_class.mustache to match Java's conditional logic