[Java] Annotation on List items, @NotNull for example

Merged
JP
2mo ago
+190-10

fixes #23705, fixes #19897, fixes #4947 for the @NotNull request

handling of x-field-extra-annotation inside array.items

listString:
  type: array
  items:
    x-field-extra-annotation :'@NotNull'
    type: string

There is a limitation in the swagger parser. $ref and x-field-extra-annotation can't be in the same level.
Workaround:

listOfPets:
  type: array
  items:
    allOf:
       - $ref: '#/components/schemas/Pet'
    x-field-extra-annotation: '@Size(max=10)'

Set default array.items annotation with option --additionalItemsAnnotations=@NotNull

Both options generate List<@NotNull String> listString;

Override of @NotNull if nullable: true

Not covered by this PR:

  • handling of JSpecify @Nullable annotation on add methods
  • uniqueItems

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.

Summary by cubic

Adds item-level annotations to Java generator list item types, fixing #23705.

New Features

  • Item-level x-field-extra-annotation and generator option additionalItemsAnnotations annotate list item types (e.g., List<@NotNull @Size(max=50) String>).
  • A global @NotNull set via additionalItemsAnnotations is dropped for nullable: true items, while explicitly provided annotations are preserved; the fully-qualified jakarta.validation.constraints form is detected too.
  • Adds VendorExtension helpers and tests covering plain arrays, nullable items, allOf items, and referenced item types.
  • JSpecify @Nullable on add methods and uniqueItems are not covered by this PR.