Skip to content

[BUG][KOTLIN] Default object value wrapping enum generates invalid Kotlin initializer #22486

@jacqueme

Description

@jacqueme

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

When using OpenAPI 3.1.0 with the Kotlin client generator (library: jvm-spring-webclient), defining a default object value for a schema that wraps an enum produces invalid Kotlin code.

The generator outputs this incorrect initializer:

val status: WrapperStatus? = {value=Enabled}

This is invalid Kotlin because {value=Enabled} is interpreted as a lambda (() -> Unit). This results in the compiler error:

Type mismatch: inferred type is Function0<Unit> but WrapperStatus? was expected

Expected output should instead construct the object, for example:

val status: WrapperStatus? = WrapperStatus(ExampleStatus.ENABLED)

or:

val status: WrapperStatus? = WrapperStatus()

openapi-generator version

org.openapitools:openapi-generator-gradle-plugin:7.17.0

OpenAPI declaration file content or url

Minimal reproducible OpenAPI 3.1.0 spec:

openapi: 3.1.0
info:
  title: Example API
  version: 1.0.0
paths: {}
components:
  schemas:
    ExampleStatus:
      type: string
      enum:
        - Enabled
        - Disabled
      default: Enabled
    WrapperStatus:
      type: object
      properties:
        value:
          $ref: '#/components/schemas/ExampleStatus'
      default:
        value: Enabled
    ExampleRequest:
      type: object
      properties:
        status:
          type: object
          properties:
            value:
              $ref: '#/components/schemas/ExampleStatus'
          default:
            value: Enabled
Generation Details

generatorName: kotlin
library: jvm-spring-webclient
dateLibrary: java8
serializationLibrary: jackson
inputSpec: openapi.yaml
outputDir: build/generated

Steps to reproduce
  1. Save the minimal OpenAPI spec above as openapi.yaml.

  2. Run the Kotlin client generator (e.g. via Gradle or the CLI) using:

  3. Inspect the generated model for ExampleRequest.

  4. Observe the generated line:

    val status: WrapperStatus? = {value=Enabled}

  5. Attempt to compile the generated code.

  6. The Kotlin compiler reports:

    Type mismatch: inferred type is Function0<Unit> but WrapperStatus? was expected

Related issues/PRs

I searched existing issues and found some related to enum default handling, but none specifically addressing default object values that wrap enum references in the Kotlin generator.

Suggest a fix

When a property schema defines a default object value, the Kotlin generator should render a proper object initializer rather than a lambda block.

For this case, the generated Kotlin should be something like:

WrapperStatus(ExampleStatus.ENABLED)

instead of:

{value=Enabled}

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