OpenAPITools / OpenAPITools/openapi-generator
[BUG] Kotlin-generator uses immutable empty initializers when using modelMutable
Nobody has claimed this yet.
- 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 (example)?
- 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?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
openapi-generator version
7.12.0
OpenAPI declaration file content or url
Generation Details
Using the latest gradle-plugin, the kotlin-client-generator and mutable models
Steps to reproduce
Server: Simple Spring Boot WebMvc app using SpringDoc. Having a Response object like
@Schema(
description = "Membership roles",
example = """["ADMIN", "USER"]""",
defaultValue = "[]"
)
@JsonView(ApiView.Get::class)
val roles: Set<String> = emptySet(),
generates a schema like
"roles": {
"uniqueItems": true,
"type": "array",
"description": "Membership roles",
"example": ["ADMIN", "USER_ADMIN"],
"items": {
"type": "string",
"description": "Membership roles",
"example": "[\"ADMIN\",\"USER\"]",
"default": "[]"
},
"default": []
},
Client: Using this schema with the kotlin-client-generator together with modelMutable=true will correctly use a MutableSet yet the defaultValue-initializer will still point to an immutable setOf()
/* Membership roles */
@get:JsonProperty("roles")
var roles: kotlin.collections.MutableSet<kotlin.String>? = setOf(),
Related issues/PRs
The mutableModel-features has been addressed in #11088
- https://github.com/OpenAPITools/openapi-generator/issues/11088
- https://github.com/OpenAPITools/openapi-generator/issues/10866
Suggest a fix
As a workaround, one can skip the default-Value annotation and agree on not skipping null/empty-values in json-serialization between server and client.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Kotlin client generator and its modelMutable handling, using the Gradle plugin and the supplied schema to reproduce the generated model. Compare the MutableSet property type with its default initializer; done means mutable models receive a compatible mutable empty initializer instead of setOf().
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100