[Java] Annotation on List items, @NotNull for example
Merged
JP
2mo ago+190+190+012345678901234567890123456789-10-10-01234567890123456789
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
@Nullableannotation on add methods - uniqueItems
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
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-annotationand generator optionadditionalItemsAnnotationsannotate list item types (e.g.,List<@NotNull @Size(max=50) String>). - A global
@NotNullset viaadditionalItemsAnnotationsis dropped fornullable: trueitems, while explicitly provided annotations are preserved; the fully-qualifiedjakarta.validation.constraintsform is detected too. - Adds
VendorExtensionhelpers and tests covering plain arrays, nullable items, allOf items, and referenced item types. - JSpecify
@Nullableon add methods anduniqueItemsare not covered by this PR.