[java] Add missing @Nullable with useJspecify for additionalProperties, equals and toIndentedString

Merged
NQ
1d ago
+134-61

Partially addresses #25536.

With useJspecify, model packages are @NullMarked, but the restclient, webclient, resttemplate, native and microprofile templates left these nullable values without @Nullable:

  • the additionalProperties map field and getAdditionalProperties(), which stay null until the first putAdditionalProperty call
  • getAdditionalProperty(String), which returns null for a missing key. A qualified type gets the annotation on its simple name (java.time.@Nullable Instant), through the existing jSpecifyNullable/jSpecifyDatatype lambdas.
  • the parameter of equals(Object) and toIndentedString(Object), which both handle null

Nothing changes without useJspecify. Not covered here: nullability of the map's values, and the JavaSpring templates.

Tests:

  • New JavaClientCodegenTest#testJspecifyNullableAdditionalPropertiesAndObjectParameters covers all 5 libraries and fails without the template change.
  • New JavaClientCodegenTest#testNullableAdditionalPropertiesOnlyWithJspecify.
  • JavaClientCodegenTest, JavaModelTest and JavaMicroprofileServerCodegenTest pass (397 tests).
  • The regenerated jspecify samples compile, and so do restclient and native clients generated from the new test spec.

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
    
    I regenerated the six Java client configs that use useJspecify (bin/configs/java-*-jspecify*.yaml), which are the only samples these templates affect. The other jspecify configs (spring-boot-4-*, jaxrs-spec-quarkus-jspecify) use other generators whose templates aren't changed here. No generator options changed, so the generator docs are unaffected.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request: @martin-mfg @KannaKim @Zomzog

Summary by cubic

Adds missing @Nullable annotations to the restclient, webclient, resttemplate, native, and microprofile Java client templates when useJspecify is enabled, since @NullMarked packages would otherwise treat these nullable values as non-null.

  • Annotates the additionalProperties map field and getAdditionalProperties(), which stay null until the first putAdditionalProperty call.
  • Annotates getAdditionalProperty(String), which returns null for a missing key; qualified types get the annotation on their simple name (for example, java.time.@Nullable Instant).
  • Annotates the equals(Object) and toIndentedString(Object) parameters, which both handle null.
  • Adds tests covering all five libraries; output is unchanged without useJspecify.