[kotlin-spring] Implement skipDefaultValues in mustache templates#23077
[kotlin-spring] Implement skipDefaultValues in mustache templates#23077mikhailsnnt wants to merge 2 commits intoOpenAPITools:masterfrom
Conversation
|
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? |
|
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. |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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>
| @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 |
There was a problem hiding this comment.
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)
Summary
skipDefaultValuesoption toKotlinSpringServerCodegenand wire it into mustache templatesdataClassOptVar.mustacheanddataClassReqVar.mustachewith{{^skipDefaultValues}}...{{/skipDefaultValues}}guardsskipDefaultValues=true, generated Kotlin data class properties no longer include= nullor other default value assignmentsTest plan
testSkipDefaultValuestest inKotlinSpringServerCodegenTestthat generates petstore models with the flag enabled and asserts= nullis absentmvn test -pl modules/openapi-generator -Dtest=KotlinSpringServerCodegenTest#testSkipDefaultValuesSummary by cubic
Adds
OMMIT_DEFAULT_NULL_VALUEStoKotlinSpringServerCodegento skip default null assignments in generated Kotlin data classes. When enabled, optional properties no longer include "= null".New Features
OMMIT_DEFAULT_NULL_VALUESswitch and additional property to control default null omission.kotlin-spring/dataClassOptVar.mustacheto guard defaults withommitDefaultNullValues.Migration
OMMIT_DEFAULT_NULL_VALUES=truevia additionalProperties or the generator switch.skipDefaultValues; default behavior is unchanged when not set.Written for commit 383f822. Summary will update on new commits.