Skip to content

[BUG][Kotlin] Multiplatform serializes enum constant name instead of value #23764

Description

@brandon-level

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator? No validation issues detected.
  • 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

When generating Kotlin multiplatform code, an enum defined in /components/schemas using x-enum-varnames generates an enum with toString() overridden to return its value, like in this abbreviated example:

@Serializable
enum class AllowFoo(val value: kotlin.String) {
    @SerialName(value = "D")
    DISALLOW("D"),

    @SerialName(value = "A")
    ALLOW("A");

    override fun toString(): kotlin.String = value
}

But when an enum is declared inline, the generated class does not override toString() and it serializes to the constant's name in code. In the example spec (link below,) the X-Allow-Foo-Ref header declaration generates XAllowFooExampleGet, whose values serialize to "DISALLOW" and "ALLOW" respectively, instead of "D" and "A".

openapi-generator version

Discovered in 7.21.0, verified in master.

OpenAPI declaration file content or url

See https://gist.github.com/brandon-level/bbcf8d4a35beb1887b063ab815c91b02

Generation Details
java -jar openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g kotlin -i x-enum-varnames-demo.yml -o ~/Desktop/openapi-demo-out -p library=multiplatform --additional-properties dateLibrary=kotlinx-datetime
Steps to reproduce

Generate classes from the schema, compare generated classes

Suggest a fix

Update code generation for inline enums using x-enum-varnames to override toString(), like for -level enum declarations.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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