feat(spring): add x-items-extra-annotation vendor extension for collection items#23710
feat(spring): add x-items-extra-annotation vendor extension for collection items#23710adraarda23 wants to merge 4 commits into
Conversation
…items
Introduces a new field-level OpenAPI vendor extension `x-inner-validation`
that places a custom annotation on the type argument of Java collections
(`List`, `Set`) generated by the Spring server generator, enabling
per-element bean validation constraints such as `@NotNull`. When set on the
items schema, the generated POJO field, getter, setter and fluent builder
all use the annotated parameterized type, e.g.:
private List<@jakarta.validation.constraints.NotNull Stubb> sample;
The implementation precomputes the full datatype string in
`SpringCodegen#postProcessModelProperty` and exposes it via
`x-datatype-with-inner-annotation` on the property; a small mustache
partial (`dataTypeWithInnerAnnotation`) then renders that string or falls
back to `{{datatypeWithEnum}}`, so existing models without the extension
produce byte-identical output. The wrap is also propagated through
`JsonNullable<...>` for nullable containers. Map / `additionalProperties`
is intentionally not covered by this extension.
Scoped to the Spring generator on purpose: the issue author's reference
attempt was on `JavaSpring/pojo.mustache` and Spring has no library
overrides for that template, so coverage is complete here. Other Java and
JaxRS generators have library-specific pojo overrides that would each need
to be wired up separately; that can be added in a follow-up if requested.
Fixes OpenAPITools#23705
There was a problem hiding this comment.
1 issue found across 10 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="docs/generators/spring.md">
<violation number="1" location="docs/generators/spring.md:147">
P2: The docs say `x-inner-validation` is a FIELD extension, but Spring only reads it from array/set `items`, so the placement is misleading.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Address cubic-dev-ai review (P2): the FIELD-level placement was misleading because Spring reads the extension from the array/set items schema, not directly from the property.
|
@adraarda23 The allOf does not seem intuitive. Could you use this syntax ? And I would call that |
…review feedback Move the extension from items.x-inner-validation (with allOf workaround) to the array property itself, and rename to x-item-validation per @jpfinne's PR review. The new syntax keeps $ref natural inside items and is more intuitive: listSample: type: array x-item-validation: '@jakarta.validation.constraints.NotNull' items: $ref: '#/components/schemas/Stubb' Internal mustache helpers (partial filename and precomputed extension key) also renamed for terminology consistency.
|
@adraarda23 I've changed my mind... generating: |
|
@jpfinne how about x-items-extra-annotation ? How does it sound ? |
Vendor extension key renamed; internal property (x-datatype-with-items-annotation) and mustache partial (dataTypeWithItemsAnnotation) renamed in lockstep.
|
Isn't this a Type-annotation? So, |
Not a good idea. List is also a type. How to understand that it is not the same as x-field-extra-annotation? |
Introduces a new field-level OpenAPI vendor extension
x-items-extra-annotationthat places a custom type-use annotation on the type argument of Java collections (List,Set) generated by the Spring server generator, enabling per-element bean validation constraints such as@NotNull. The extension is set on the array property itself; the generated POJO field, getter, setter and fluent builder all use the annotated parameterized type, e.g.:The implementation precomputes the full datatype string in
SpringCodegen#postProcessModelPropertyand exposes it viax-datatype-with-items-annotationon the property; a small mustache partial (dataTypeWithItemsAnnotation) then renders that string or falls back to{{datatypeWithEnum}}, so existing models without the extension produce byte-identical output. The wrap is also propagated throughJsonNullable<...>for nullable containers. WhenuniqueItems: trueis set,Setis used instead ofList. Map /additionalPropertiesis intentionally not covered by this extension.Scoped to the Spring generator on purpose: the issue author's reference attempt was on
JavaSpring/pojo.mustacheand Spring has no library overrides for that template, so coverage is complete here. Other Java and JaxRS generators have library-specific pojo overrides that would each need to be wired up separately; that can be added in a follow-up if requested.Fixes #23705
PR checklist
master