OpenAPITools / OpenAPITools/openapi-generator

[REQ] Optional getters for nullable fields only (opt-in, raw-type setters)

Open
#24,002 1 comment 0 reactions 0 assignees View on GitHub

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.

When generating Java DTOs, getters for nullable / non-required fields return the raw type, which can be null. This pushes null-safety to runtime: consumers must defensively guard against NullPointerException instead of being guided by the type system at compile time.

private BigDecimal amount;

public BigDecimal getAmount() {
    return amount; // can be null; no compile-time hint
}

Describe the solution you'd like

Add a new opt-in configuration option (e.g. optionalGettersForNullableFieldsOnly) for the java and spring generators that makes getters of nullable / non-required fields return Optional<T>, leaving the field and the setter with the raw type:

public Optional<PlanRequestPriceDTO> getPrice() {
    return Optional.ofNullable(this.price);
}

public void setPrice(PlanRequestPriceDTO price) {
    this.price = price;
}

Key points:

  • Disabled by default — existing behavior is unchanged unless the option is explicitly enabled.
  • Optional appears only on the getter return type, via Optional.ofNullable(...).
  • The field and setter parameter keep the raw type (no Optional), so there's no point where Optional.get() on an empty value could throw, and setters remain symmetric with classic JavaBeans (compatible with MapStruct, Jackson, binders, etc.).
  • Applies only to nullable / non-required fields, not all getters.

This mirrors what Apache Avro already offers through optionalGettersForNullableFieldsOnly in its avro-maven-plugin.

Describe alternatives you've considered

  • useOptional=true (added in #17202): applies more broadly and, per #17538, generates setters using Optional.of(), which throws on null/empty. The proposed option is narrower (nullable fields only) and avoids Optional on the setter entirely.
  • Post-processing the generated sources (AST rewriting): works but is fragile, hard to maintain, and not reusable across projects.
  • Mapping DTOs to a separate domain layer that uses Optional: valid, but not always present; consumers often use generated DTOs directly.

Additional context

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

The payload names no files or tests; start by tracing the existing useOptional option in the Java and Spring generators and reviewing the cited issues #17202, #17538, and #20406. Done means the new option is disabled by default, affects only nullable or non-required getter return types, and leaves fields and setter parameters raw.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.