OpenAPITools / OpenAPITools/openapi-generator

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

Open
#22,486 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Kotlin client generator using the jvm-spring-webclient library and reproduce the issue with the minimal OpenAPI 3.1.0 specification. Inspect the generated ExampleRequest model and compile the generated code; done means the default object value is emitted as a valid WrapperStatus initializer rather than {value=Enabled}.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.