[BUG][KOTLIN] Default object value wrapping enum generates invalid Kotlin initializer
Bug Report Checklist
- [x] Have you provided a full/minimal spec to reproduce the issue?
- [x] Have you validated the input using an OpenAPI validator?
- [x] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [x] 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}
Hello @jacqueme, I think that in general support for more complex default values besides stuff such as strings, numbers, empty arrays, etc. is generally unsupported in kotlin generators. I would actually presume that many other generators would fare similarly well. It has nothing to do with the enum - I presume you would get the same issue e.g. when trying to provide a default value this way for any object with multiple attributes (meaning trying to instantiate complex data class from a json - you can imagine even multiple levels of nested json objects).
Getting it to properly instantiate the default value from the json would be quite difficult. But I can imagine it should at least not break the compilation - maybe just default to null and put the json-defined default next to it as an inline comment to suggest to the developer which default he should manually provide via ?: operator in kotlin?
Would that make sense to you?
Hello @Picazsoo,
In my case setting it to null for this specific case would do the job.