Skip to content

[kotlin-spring] Implement skipDefaultValues in mustache templates#23077

Open
mikhailsnnt wants to merge 2 commits intoOpenAPITools:masterfrom
mikhailsnnt:kotlin-default-values-switch
Open

[kotlin-spring] Implement skipDefaultValues in mustache templates#23077
mikhailsnnt wants to merge 2 commits intoOpenAPITools:masterfrom
mikhailsnnt:kotlin-default-values-switch

Conversation

@mikhailsnnt
Copy link

@mikhailsnnt mikhailsnnt commented Mar 1, 2026

Summary

  • Add skipDefaultValues option to KotlinSpringServerCodegen and wire it into mustache templates
  • Wrap default value assignments in dataClassOptVar.mustache and dataClassReqVar.mustache with {{^skipDefaultValues}}...{{/skipDefaultValues}} guards
  • When skipDefaultValues=true, generated Kotlin data class properties no longer include = null or other default value assignments

Test plan

  • Added testSkipDefaultValues test in KotlinSpringServerCodegenTest that generates petstore models with the flag enabled and asserts = null is absent
  • Test passes: mvn test -pl modules/openapi-generator -Dtest=KotlinSpringServerCodegenTest#testSkipDefaultValues

Summary by cubic

Adds OMMIT_DEFAULT_NULL_VALUES to KotlinSpringServerCodegen to skip default null assignments in generated Kotlin data classes. When enabled, optional properties no longer include "= null".

  • New Features

    • Added OMMIT_DEFAULT_NULL_VALUES switch and additional property to control default null omission.
    • Updated kotlin-spring/dataClassOptVar.mustache to guard defaults with ommitDefaultNullValues.
  • Migration

    • Enable with OMMIT_DEFAULT_NULL_VALUES=true via additionalProperties or the generator switch.
    • Replaces the earlier skipDefaultValues; default behavior is unchanged when not set.

Written for commit 383f822. Summary will update on new commits.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

@wing328
Copy link
Member

wing328 commented Mar 4, 2026

thanks for the PR.

what about doing it in the openapi normalizer instead by adding a rule to remove the default value from the schema so that all generators can support this enhancement as well?

@wing328
Copy link
Member

wing328 commented Mar 4, 2026

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 4 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java:80">
P2: Public generator option key was changed from `skipDefaultValues` to misspelled `ommitDefaultNullValues`, breaking backward compatibility and silently ignoring existing caller configs.</violation>
</file>

<file name="modules/openapi-generator/src/main/resources/kotlin-spring/dataClassReqVar.mustache">

<violation number="1">
P1: dataClassReqVar.mustache removed the skipDefaultValues guard instead of replacing it with ommitDefaultNullValues, causing unconditional default rendering for required vars.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

public static final String GRADLE_BUILD_FILE = "gradleBuildFile";
public static final String SERVICE_INTERFACE = "serviceInterface";
public static final String SERVICE_IMPLEMENTATION = "serviceImplementation";
public static final String OMMIT_DEFAULT_NULL_VALUES = "ommitDefaultNullValues";
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Public generator option key was changed from skipDefaultValues to misspelled ommitDefaultNullValues, breaking backward compatibility and silently ignoring existing caller configs.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java, line 80:

<comment>Public generator option key was changed from `skipDefaultValues` to misspelled `ommitDefaultNullValues`, breaking backward compatibility and silently ignoring existing caller configs.</comment>

<file context>
@@ -77,7 +77,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
     public static final String SERVICE_INTERFACE = "serviceInterface";
     public static final String SERVICE_IMPLEMENTATION = "serviceImplementation";
-    public static final String SKIP_DEFAULT_VALUES = "skipDefaultValues";
+    public static final String OMMIT_DEFAULT_NULL_VALUES = "ommitDefaultNullValues";
     public static final String SKIP_DEFAULT_INTERFACE = "skipDefaultInterface";
     public static final String SKIP_DEFAULT_API_INTERFACE = "skipDefaultApiInterface";
</file context>
Fix with Cubic

@Deprecated(message = ""){{/deprecated}}{{#vendorExtensions.x-field-extra-annotation}}
{{{.}}}{{/vendorExtensions.x-field-extra-annotation}}
@get:JsonProperty("{{{baseName}}}"){{#isInherited}} override{{/isInherited}} {{>modelMutable}} {{{name}}}: {{#isEnum}}{{#isArray}}{{baseType}}<{{/isArray}}{{classname}}.{{{nameInPascalCase}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{^defaultValue}}null{{/defaultValue}}{{#defaultValue}}{{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}} No newline at end of file
@get:JsonProperty("{{{baseName}}}"){{#isInherited}} override{{/isInherited}} {{>modelMutable}} {{{name}}}: {{#isEnum}}{{#isArray}}{{baseType}}<{{/isArray}}{{classname}}.{{{nameInPascalCase}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}?{{^defaultValue}}{{^ommitDefaultNullValues}} = null{{/ommitDefaultNullValues}}{{/defaultValue}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}} No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not simply remove = null part without an option?

cc @karismann (2019/03) @Zomzog (2019/04) @andrewemery (2019/10) @4brunu (2019/11) @yutaka0m (2020/03) @stefankoppier (2022/06) @e5l (2024/10) @dennisameling (2026/02)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants