Skip to content

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

@mikhailsnnt

Description

@mikhailsnnt

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. skipDefaultValues) that omits = null defaults for optional
properties:

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

Implementation:

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions