OpenAPITools / OpenAPITools/openapi-generator
[REQ] Optional getters for nullable fields only (opt-in, raw-type setters)
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.
Optionalappears only on the getter return type, viaOptional.ofNullable(...).- The field and setter parameter keep the raw type (no
Optional), so there's no point whereOptional.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 usingOptional.of(), which throws onnull/empty. The proposed option is narrower (nullable fields only) and avoidsOptionalon 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
- Apache Avro reference: https://github.com/apache/avro/blob/master/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java
- Related: #17202 (added
useOptional), #17538 (setters useOptional.of()→ NPE), #20406 (pending fix to useofNullable).
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
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