Skip to content

[SPRING] Improve jSpecify Nullable for all args constructor, fluent setters and Builder and toIndentedString - #24563

Open
jpfinne wants to merge 27 commits into
OpenAPITools:masterfrom
jpfinne:feature/jspecifyChainSetter
Open

[SPRING] Improve jSpecify Nullable for all args constructor, fluent setters and Builder and toIndentedString#24563
jpfinne wants to merge 27 commits into
OpenAPITools:masterfrom
jpfinne:feature/jspecifyChainSetter

Conversation

@jpfinne

@jpfinne jpfinne commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

when useJspecify=true:

For all spring options:

Add unit tests in SpringCodegenTest
Add 2 more spring samples

Example using useOptional=true and optionalAcceptNullable=true

public Foo color(@Nullable String color) {
    this.color = Optional.ofNullable(color);
    return this;
  }

useOptional=true and optionalAcceptNullable=false

public Foo color(String color) {
    this.color = Optional.of(color);
    return this;
  }

ps: this PR focuses on jspecify. There are still some missing @jakarta.annotation.Nullable when useJspecify=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

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

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 @Nullable in Spring generators: we annotate all-args constructors and builder/fluent setters, place @Nullable correctly on qualified types and generics, and honor openApiNullable, useOptional, and optionalAcceptNullable. Adds Spring Boot 4 configs/samples, maps OffsetDateTime to java.time.Instant, and updates samples with a color query param and related fields.

  • New Features

    • All-args constructors and builder chain setters add @Nullable when useJspecify=true, honoring openApiNullable, useOptional, and optionalAcceptNullable; packages marked @NullMarked.
    • New Spring Boot 4 configs: spring-boot-4-jspecify-openapiNullable.yaml and spring-boot-4-jspecify-useOptional.yaml; OffsetDateTime -> java.time.Instant, BigDecimal -> java.math.BigDecimal.
    • API/models: add color query param (default red) and related fields; samples regenerated across clients/servers; CI/workflows include new sample dirs and fix naming to springboot-4-jspecify.
  • Bug Fixes

    • Correct JSpecify placement before generics via getLastIndex, yielding java.time.@Nullable Instant and @Nullable List<java.time.Instant>.
    • Use nullableDatatypeWithEnum in Java/nullableArgumentWithEnum.mustache; builder setters use nullableArgument_builder.
    • Split nullableAnnotation.mustache into nullableAnnotation_jspecify.mustache and nullableAnnotation_default.mustache for correct rules and backward compatibility.
    • Mark toIndentedString(@Nullable Object o) across samples and add test assertions for optionalAcceptNullable=true/false.

Written for commit 3f3a174. Summary will update on new commits.

Review in cubic

@jpfinne jpfinne changed the title [SPRING] Improve jspecify Nullable for all args constructor and fluent setters [SPRING] Improve jSpecify Nullable for all args constructor, fluent setters and Builder Aug 2, 2026
@jpfinne
jpfinne marked this pull request as ready for review August 2, 2026 19:54

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@jpfinne

jpfinne commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Cubic warning about invalid java samples is not relevant. Java generators are not updated in this PR.
The issue already existed.

@MelleD

MelleD commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Hey @jpfinne,

I didn't see that case in the PR, but Iam unsure :).

For Optionals. I also have some warnings with ofNullable

  public Dto description(String description) {
    this.description = Optional.ofNullable(description);
    return this;
  }

Bildschirmfoto 2026-08-03 um 09 20 39

@jpfinne

jpfinne commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

@MelleD
in the generated samples/openapi3/server/petstore/springboot-4-jspecify-useOptional/src/main/java/org/openapitools/model/Foo.java

public Foo color(@Nullable String color) {
    this.color = Optional.ofNullable(color);
    return this;
  }

Is it what you want?

@MelleD

MelleD commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Is it what you want?

Yes nice :)

@SubhamAshok SubhamAshok left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 = false with @nullable setter. Resolves #24294, tested that exact example
  • typeMapping to java.time.Instant: java.time.@Nullable Instant and @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.

@jpfinne jpfinne changed the title [SPRING] Improve jSpecify Nullable for all args constructor, fluent setters and Builder [SPRING] Improve jSpecify Nullable for all args constructor, fluent setters and Builder and toIndentedString Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants