[JAVA] [Spring] JSpecify, fix nullable + required field

Merged
JP
4mo ago
+8591-524

fix #24686

For a type:

type: object
required:
  - name
properties:
   name:
     type: string
     nullable: true

Ensure that the following json are accepted:

jsonvalid
{ "name": "john" }yes
{ "name": null }yes
{ }no

Keep jspecify @Nullable annotation for builder with an improved RemoveAnnotationLambda. Other annotations like @Valid are still removed.
Ensure setter, chain setters, builder, constructors contain the correct JSpecify @Nullable annotation.

Add more combination of readonly, nullable and required in the samples.

For nullable attributes:

Update @Schema annotation to add nullable=true

For nullable container attributes + openapiNullable

Fix a potential NullPointerException in add and put item. For example new Foo()._list(null).addListItem("item")

For required+nullable attributes:

java

  • correct missing JSpecify, javax or jakarta @Nullable annotation

Spring

  • openapiNullable=false: remove @NotNull. The bean validation has no way to find out if the value is absent.
  • openapiNullable=true: remove @NotNull. This is a not intuitive. It should be possible to distinguish between null and absent. This is not the case of the Spring bean validation. It uses JsonNullableValueExtractor or JsonNullableJakartaValueExtractor to extract the value. So @NotNull fails for null and for absent.

Workaround: use an annotation @Present with a validator like the following. (it might be a nice addition to the jackson-databind-nullable project)

public class PresentValidator implements ConstraintValidator<Present, JsonNullable<?>> {
    @Override
    public boolean isValid(JsonNullable<?> value, ConstraintValidatorContext context) {
        return value != null && value.isPresent();
    }
}

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.

Java | @bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08) @KannaKim (2026/07)

Java Spring | @cachescrubber (2022/02) @welshm (2022/02) @MelleD (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

Aligns Java and Spring generators with jspecify for required+nullable fields and fixes nullable container NPEs. Previously Spring emitted @NotNull and some constructors/builders missed @Nullable; now generators apply @Nullable consistently, only emit @NotNull for required non‑nullable fields, and add a /* @Present */ hint when openApiNullable is on.

  • Java/Spring templates: add constructor and chain‑setter partials for nullable args; standardize nullable_var_annotations.mustache; expose removeAnnotations via RemoveAnnotationLambda; compute the nullable annotation from javaxPackage so javax.annotation.Nullable is not emitted under jspecify.
  • Spring Bean Validation: move @NotNull to notNull.mustache; emit only when “required AND not readOnly AND not nullable”; add /* @Present */ comment for required+nullable with openApiNullable.
  • OpenAPI/annotations: generate @Schema(nullable = true) across Java libraries.
  • JsonNullable containers: guard add/put helpers against isPresent() && get() == null to prevent NPEs.
  • Samples/tests: add RequiredAndNullable schema and endpoints; ensure constructor/chain‑setter @Nullable and absence of javax.annotation.Nullable under jspecify; change FileApi to return FileContent with Accept: application/json.

Migration

  • Spring generators no longer emit @NotNull for required+nullable, including with openApiNullable. If you relied on Bean Validation to reject nulls, add explicit constraints or mark fields non‑nullable.
  • With openApiNullable enabled, required+nullable JsonNullable<T> fields default to null. If you depended on undefined(), update handling accordingly.