OpenAPITools / OpenAPITools/openapi-generator

[BUG] Kotlin-generator uses immutable empty initializers when using modelMutable

Open
#21,004 0 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 (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

Image

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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.