-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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
-
Save the minimal OpenAPI spec above as
openapi.yaml. -
Run the Kotlin client generator (e.g. via Gradle or the CLI) using:
-
Inspect the generated model for
ExampleRequest. -
Observe the generated line:
val status: WrapperStatus? = {value=Enabled} -
Attempt to compile the generated code.
-
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}