[SPRING] Improve jSpecify Nullable for all args constructor, fluent setters and Builder and toIndentedString - #24563
Conversation
merge master
There was a problem hiding this comment.
1 issue found across 131 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/client/petstore/java/native-jackson3-jspecify/src/main/java/org/openapitools/client/model/Foo.java">
<violation number="1" location="samples/client/petstore/java/native-jackson3-jspecify/src/main/java/org/openapitools/client/model/Foo.java:626">
P2: The new builder chain setters for the nullable properties are missing `@Nullable` on their parameter types, even though this package is `@NullMarked` and the corresponding fluent setters/getters/fields are correctly annotated `@Nullable`. Under JSpecify this makes the builder parameters non-null, so `toBuilder()` passing `getNullableDt()`, `getNullableBinary()`, `getNullableNumber()`, `getColor()`, etc. (all `@Nullable` return types) into those setters is a nullness mismatch, and it directly contradicts the PR's goal of adding `@Nullable` to builder chain setters. Consider emitting `@Nullable` (in the same qualified form used by the fluent setters) for the builder setters of nullable properties so the builder API stays consistent with the rest of the model.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
|
Cubic warning about invalid java samples is not relevant. Java generators are not updated in this PR. |
|
Hey @jpfinne, I didn't see that case in the PR, but Iam unsure :). For Optionals. I also have some warnings with
|
|
@MelleD Is it what you want? |
Yes nice :) |
SubhamAshok
left a comment
There was a problem hiding this comment.
Built this branch and tested locally. Findings:
- jspecify + spring-cloud: fields, getters, toIndentedString all @nullable, imports correct
- full tree diff vs master, jspecify off + useOptional: identical
- full tree diff vs master, jspecify on: only change is fluent setters gaining @nullable, as intended
- defaulted non-required field: now
@Nullable Boolean force = falsewith @nullable setter. Resolves #24294, tested that exact example - typeMapping to java.time.Instant:
java.time.@Nullable Instantand@Nullable List<java.time.Instant>both correct, getLastIndex works
Nit: pojo.mustache line 137 uses {{>nullableAnnotation}}, line 156 {{>nullableAnnotation_default}}. Same output, one style is easier to maintain.
Includes my #24567 fix, happy to close it when this merges.

when useJspecify=true:
@Nullableon fluent setters, all argument constructors and builder for non required attributes. HonoropenApiNullable,useOptionalandoptionalAcceptNullable. fixes [BUG] [JAVA] [SPRING] Missing JSpecify@Nullableon fluent setter parameter for optional properties #23681 Supersedes [JAVA-SPRING] Fix missing nullable on all args constructor and fluent setters #24263lambda.jSpecifyNullableto generate@Nullable List<java.time.Instant>instead ofList<java.time.@Nullable Instant)For all spring options:
String toIndentedString(@Nullable Object o)Add unit tests in SpringCodegenTest
Add 2 more spring samples
Example using
useOptional=trueandoptionalAcceptNullable=trueuseOptional=trueandoptionalAcceptNullable=falseps: this PR focuses on jspecify. There are still some missing
@jakarta.annotation.NullablewhenuseJspecify=false.It avoids regenerating too many samples. That is for another PR. There are comments in the mustache template like
{{!backward compatibility}}to locate the code to fix.PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
Review welcome:
@cachescrubber (2022/02) @welshm (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08) @KannaKim (2026/07)
Summary by cubic
Improves JSpecify
@Nullablein Spring generators: we annotate all-args constructors and builder/fluent setters, place@Nullablecorrectly on qualified types and generics, and honoropenApiNullable,useOptional, andoptionalAcceptNullable. Adds Spring Boot 4 configs/samples, mapsOffsetDateTimetojava.time.Instant, and updates samples with acolorquery param and related fields.New Features
@NullablewhenuseJspecify=true, honoringopenApiNullable,useOptional, andoptionalAcceptNullable; packages marked@NullMarked.spring-boot-4-jspecify-openapiNullable.yamlandspring-boot-4-jspecify-useOptional.yaml;OffsetDateTime->java.time.Instant,BigDecimal->java.math.BigDecimal.colorquery param (defaultred) and related fields; samples regenerated across clients/servers; CI/workflows include new sample dirs and fix naming tospringboot-4-jspecify.Bug Fixes
getLastIndex, yieldingjava.time.@Nullable Instantand@Nullable List<java.time.Instant>.nullableDatatypeWithEnuminJava/nullableArgumentWithEnum.mustache; builder setters usenullableArgument_builder.nullableAnnotation.mustacheintonullableAnnotation_jspecify.mustacheandnullableAnnotation_default.mustachefor correct rules and backward compatibility.toIndentedString(@Nullable Object o)across samples and add test assertions foroptionalAcceptNullable=true/false.Written for commit 3f3a174. Summary will update on new commits.