OpenAPITools / OpenAPITools/openapi-generator

[REQ][kotlin-spring] Option to disable default value generation for optional model properties

Open
#23,076 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

The kotlin-spring generator adds = null to all optional properties in data classes:

  data class UserDto(
      val name: String,
      val email: String? = null,
      val role: RoleEnum? = null
  )

This causes two problems:

  1. Silent data loss at scale. In projects with hundreds of models, default = null lets
    callers omit fields without any compiler warning. When a new field is added, every existing
    call site compiles fine and the missing field goes unnoticed until production.
  2. Incorrect behavior with some libraries. Tools like MapStruct don't handle Kotlin default
    values correctly, leading to bugs in mapping logic.

Describe the solution you'd like

A config option (e.g. ommitDefaultNullValues) that omits = null defaults for optional
properties:

  data class UserDto(
      val name: String,
      val email: String?,
      val role: RoleEnum?
  )

Implementation:

https://github.com/OpenAPITools/openapi-generator/pull/23077

a switch in KotlinSpringServerCodegen.java + conditional wrap in
dataClassOptVar.mustache.

Describe alternatives you've considered

  • Custom templates (--template-dir): works but requires maintaining a template fork across
    upgrades.

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 KotlinSpringServerCodegen.java and dataClassOptVar.mustache, as identified in the issue, and review pull request 23077. Verify that the new configuration switch controls whether optional properties receive = null while preserving the existing default behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin, spring
Domain
tooling
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.