OpenAPITools / OpenAPITools/openapi-generator
[REQ] [Kotlin] Default optional parameters to null
Nobody has claimed this yet.
- 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.
While non-required properties of generatered DTOs are defaulted to null this is not true to non-required parameters.
Example:
Describe the solution you'd like
= null added to optional parameters
Additional context
Generator: kotlin-server
Library: jaxrs-spec
Options:
<configOptions>
<library>jaxrs-spec</library>
<!-- Required for Quarkus 3.x (Jakarta EE 10): uses jakarta.* instead of javax.* imports -->
<useJakartaEe>true</useJakartaEe>
<useSwaggerAnnotations>false</useSwaggerAnnotations>
<sourceFolder>src/main/java</sourceFolder>
<dateLibrary>java8</dateLibrary>
<serializationLibrary>jackson</serializationLibrary>
<useBeanValidation>true</useBeanValidation>
<openApiNullable>false</openApiNullable>
<sortModelPropertiesByRequiredFlag>true</sortModelPropertiesByRequiredFlag>
<sortParamsByRequiredFlag>true</sortParamsByRequiredFlag>
<interfaceOnly>true</interfaceOnly>
</configOptions>
I got it to work by adding a custom Mustache template querParams.mustache to my template directory:
{{#isQueryParam}}@QueryParam("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}"){{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{^isContainer}}{{#defaultValue}} @DefaultValue({{#lambda.doublequote}}{{{.}}}{{/lambda.doublequote}}){{/defaultValue}}{{/isContainer}} {{paramName}}: {{{dataType}}}{{#isNullable}}?{{/isNullable}}{{^isNullable}}{{^defaultValue}}{{^required}}? = null{{/required}}{{/defaultValue}}{{/isNullable}}{{/isQueryParam}}
(Code above has been generated by Claude Sonnet 5)
Maybe this could be validated by some experienced dev and included in one of the next releases :)
Thank you!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the kotlin-server template used for the jaxrs-spec library and compare its handling of optional parameters with generated DTO properties. Use the supplied custom querParams.mustache template as a reference, then generate a representative server and verify that non-required parameters receive = null without changing required or explicitly defaulted parameters.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 70/100