Skip to content

[BUG][kotlin-client] @JsonCreator on enum decode() silently converts invalid values to null (regression in v7.18.0) #22662

@limoneren

Description

@limoneren

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:

  1. Input validation - Invalid enum values are silently accepted
  2. Error messages - No exception is thrown for invalid data
  3. Distinguishing null/absent/invalid - All three cases now result in null
  4. 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
  1. Generate Kotlin client code using the command above
  2. Look at the generated enum class (e.g., MyEnumType.kt) - note the decode() function with @JsonCreator uses firstOrNull which returns null for unknown values
  3. 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

The Java generator went through the same discussion in #625 and established that:

  • Default behavior: Throw IllegalArgumentException for 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

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