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

Merged
JP
1mo ago
+4471-204

when useJspecify=true:

  • add @Nullable on fluent setters, all argument constructors and builder for non required attributes. Honor openApiNullable, useOptional and optionalAcceptNullable . fixes #23681 Supersedes #24263
  • fix #24294 for Spring (default value handling)
  • update lambda.jSpecifyNullable to generate @Nullable List<java.time.Instant> instead of List<java.time.@Nullable Instant)

For all spring options:

  • fix #22598 (String toIndentedString(@Nullable Object o)

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.