OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Kotlin] Generate properties being required and nullable:true

Open
#14,021 3 comments 3 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

We are using the Kotlin-Spring openapi generator to generate model.dtos in TS and Kotlin.
Now we have come across an issue regarding the required flag, it is interpreted differently:

for Kotlin the required flag means that the property can not be null
while in ts required means it can be null but the property has to be part of the object.

For our other projects, where we use the "spring"-generator (java), we can overcome this discrepancy by implementing the Jackson Nullable dependency, which allows us to introduce a second flag called nullable.

Using this flag allows us to have a model.dto where properties are required (so the frontend knows the property has to be part of the object) and can be null.

However, as far as our research goes, there is currently no way to tell the Kotli generator that a property should be required while being nullable: true.

openapi-generator version
<openapi.codegen.maven.plugin.version>5.4.0</openapi.codegen.maven.plugin.version>
OpenAPI declaration file content or url
(
    Partner:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
          nullable: true
      required:
        - id
        - name
)

Generated model in Kotlin (how it is)

(
data class PartnerDto(

    @field:JsonProperty("id", required = true) val id: kotlin.String,

    @field:JsonProperty("name", required = true) val name: kotlin.String
) {

}
)

Generated model in Kotlin (how it should be)

(
data class PartnerDto(

    @field:JsonProperty("id", required = true) val id: kotlin.String,

    @field:JsonProperty("name", required = true) val name: kotlin.String?
) {

}
)
Steps to reproduce
  1. use spring-kotlin openapi generator
  2. make property required and nullable:true
  3. generate sources for Kotlin: property generated as non-nullable
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/13531
https://github.com/OpenAPITools/openapi-generator/issues/10832

Suggest a fix

It would be great if the Kotlin generator would be able to interpret nullable:true in combination with required as described above.

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 Spring generator and the supplied OpenAPI declaration, then compare the generated model with the expected Kotlin output. The issue is done when a property marked both required and nullable:true remains required while its generated Kotlin type is nullable.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.