OpenAPITools / OpenAPITools/openapi-generator

[REQ] [Kotlin] Default optional parameters to null

Open
#24,958 1 comment 0 reactions 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.

While non-required properties of generatered DTOs are defaulted to null this is not true to non-required parameters.

Example:

Image

Describe the solution you'd like

= null added to optional parameters

Image

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.