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
Additional context
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 againstNullPointerExceptioninstead of being guided by the type system at compile time.Describe the solution you'd like
Add a new opt-in configuration option (e.g.
optionalGettersForNullableFieldsOnly) for thejavaandspringgenerators that makes getters of nullable / non-required fields returnOptional<T>, leaving the field and the setter with the raw type:Key points:
Optionalappears only on the getter return type, viaOptional.ofNullable(...).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.).This mirrors what Apache Avro already offers through
optionalGettersForNullableFieldsOnlyin itsavro-maven-plugin.Describe alternatives you've considered
useOptional=true(added in Add Java Optional for POJOs in JavaSpring templates #17202): applies more broadly and, per [BUG][Java/spring] Incorrect Handling of Nullable Fields in OpenAPI Generator #17538, generates setters usingOptional.of(), which throws onnull/empty. The proposed option is narrower (nullable fields only) and avoidsOptionalon the setter entirely.Optional: valid, but not always present; consumers often use generated DTOs directly.Additional context
useOptional), [BUG][Java/spring] Incorrect Handling of Nullable Fields in OpenAPI Generator #17538 (setters useOptional.of()→ NPE), Using Optional.ofNullable() at the fluent setters to prevent NPE #20406 (pending fix to useofNullable).