Skip to content

[BUG][Kotlin] @JsonCreator decode() in non-nullable enums throws on unknown values instead of returning null (regression in 7.19.0) #23791

@davidlj95

Description

@davidlj95

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?
Description

Since version 7.19.0, the generated @JsonCreator decode() method for non-nullable Kotlin enums throws IllegalArgumentException on unknown values. In 7.18.0 and earlier, the same method returned null, allowing callers to handle unknown enum values gracefully (e.g. from forward-compatible APIs where new enum values may be added server-side before clients update their specs).

This is a silent breaking change for any consumer that relied on decode() returning null for unrecognized values. There was no mention of this behavioral change in the release notes.

openapi-generator version

Regression introduced in 7.19.0 (works correctly in 7.18.0). Still present in 7.21.0.

OpenAPI declaration file content or url
openapi: "3.0.3"
info:
  title: Test
  version: "1.0"
paths: {}
components:
  schemas:
    ChannelType:
      type: string
      enum:
        - ANDROID
        - IOS
        - WEB
Generation Details
openapi-generator generate -g kotlin -i spec.yaml -o out \
  --additional-properties=library=jvm-spring
Steps to reproduce
  1. Generate with openapi-generator 7.18.0ChannelType.decode("UNKNOWN") returns null
  2. Generate with openapi-generator 7.19.0ChannelType.decode("UNKNOWN") throws IllegalArgumentException

Expected: decode() returns null for unknown values (or at minimum, the breaking change is documented and opt-in).

Actual: decode() throws, breaking any downstream code that handled null gracefully.

Related issues/PRs
Suggest a fix

Either:

  1. Revert to the previous behavior (return null) — this requires removing @JsonCreator from decode(), otherwise there's an inconsistency where Jackson deserialization throws on unknown values but calling decode() directly would return null. Without @JsonCreator, Jackson's default enum handling applies and decode() remains a standalone utility.
  2. Make the throwing behavior opt-in via a configuration flag
  3. This changes the public API contract of generated models — if kept intentionally, it warrants a major version bump (8.0.0), not a patch/minor release, since consumers rely on the return type and nullability semantics of generated code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions