fix(java): place bean-validation @Valid on the type argument instead of the container (HV000271)#24176
fix(java): place bean-validation @Valid on the type argument instead of the container (HV000271)#24176twonky4 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
6 issues found across 579 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="samples/openapi3/server/petstore/springboot-implicitHeaders/src/main/java/org/openapitools/model/ArrayOfArrayOfNumberOnly.java">
<violation number="1" location="samples/openapi3/server/petstore/springboot-implicitHeaders/src/main/java/org/openapitools/model/ArrayOfArrayOfNumberOnly.java:50">
P3: Unused `jakarta.validation.Valid` import remains after the only `@Valid` annotation in the class was removed.</violation>
</file>
<file name="samples/client/petstore/java/rest-assured/src/main/java/org/openapitools/client/model/ArrayOfNumberOnly.java">
<violation number="1" location="samples/client/petstore/java/rest-assured/src/main/java/org/openapitools/client/model/ArrayOfNumberOnly.java:64">
P3: `javax.validation.Valid` import is unused after the only @Valid annotation was removed from the getter. This leaves stale generated code that can trigger lint/checkstyle failures.</violation>
</file>
<file name="samples/client/petstore/java/rest-assured/src/main/java/org/openapitools/client/model/MixedPropertiesAndAdditionalPropertiesClass.java">
<violation number="1" location="samples/client/petstore/java/rest-assured/src/main/java/org/openapitools/client/model/MixedPropertiesAndAdditionalPropertiesClass.java:51">
P2: `@Valid` on the map value type argument is declared in both the field and the getter return type, which may cause duplicate cascaded validation for the same property per Hibernate Validator documentation.</violation>
</file>
Note: This PR contains a large number of files. cubic only reviews up to 200 files per PR, so some files may not have been reviewed. cubic prioritizes the most important files to review.
Re-trigger cubic
| @SerializedName(SERIALIZED_NAME_MAP) | ||
| @javax.annotation.Nullable | ||
| private Map<String, Animal> map = new HashMap<>(); | ||
| private Map<String, @Valid Animal> map = new HashMap<>(); |
There was a problem hiding this comment.
P2: @Valid on the map value type argument is declared in both the field and the getter return type, which may cause duplicate cascaded validation for the same property per Hibernate Validator documentation.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/java/rest-assured/src/main/java/org/openapitools/client/model/MixedPropertiesAndAdditionalPropertiesClass.java, line 51:
<comment>`@Valid` on the map value type argument is declared in both the field and the getter return type, which may cause duplicate cascaded validation for the same property per Hibernate Validator documentation.</comment>
<file context>
@@ -48,7 +48,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
@SerializedName(SERIALIZED_NAME_MAP)
@javax.annotation.Nullable
- private Map<String, Animal> map = new HashMap<>();
+ private Map<String, @Valid Animal> map = new HashMap<>();
public MixedPropertiesAndAdditionalPropertiesClass() {
</file context>
| @@ -47,7 +47,7 @@ public ArrayOfArrayOfNumberOnly addArrayArrayNumberItem(List<BigDecimal> arrayAr | |||
| * Get arrayArrayNumber | |||
There was a problem hiding this comment.
P3: Unused jakarta.validation.Valid import remains after the only @Valid annotation in the class was removed.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/openapi3/server/petstore/springboot-implicitHeaders/src/main/java/org/openapitools/model/ArrayOfArrayOfNumberOnly.java, line 50:
<comment>Unused `jakarta.validation.Valid` import remains after the only `@Valid` annotation in the class was removed.</comment>
<file context>
@@ -47,7 +47,7 @@ public ArrayOfArrayOfNumberOnly addArrayArrayNumberItem(List<BigDecimal> arrayAr
* @return arrayArrayNumber
*/
- @Valid
+
@Schema(name = "ArrayArrayNumber", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("ArrayArrayNumber")
</file context>
| */ | ||
| @javax.annotation.Nullable | ||
| @Valid | ||
|
|
There was a problem hiding this comment.
P3: javax.validation.Valid import is unused after the only @Valid annotation was removed from the getter. This leaves stale generated code that can trigger lint/checkstyle failures.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/java/rest-assured/src/main/java/org/openapitools/client/model/ArrayOfNumberOnly.java, line 64:
<comment>`javax.validation.Valid` import is unused after the only @Valid annotation was removed from the getter. This leaves stale generated code that can trigger lint/checkstyle failures.</comment>
<file context>
@@ -61,7 +61,6 @@ public ArrayOfNumberOnly addArrayNumberItem(BigDecimal arrayNumberItem) {
*/
@javax.annotation.Nullable
- @Valid
public List<BigDecimal> getArrayNumber() {
</file context>
…of the container (HV000271)
Hibernate Validator 9.1+ (bundled with Spring Boot 4) logs HV000271 ("Using
@Valid on a container is deprecated. You should apply the annotation on the
type argument(s)") for every @Valid-annotated List/Set/Map — on both model
properties and API parameters.
This relocates @Valid from the container to its type argument — the
Bean-Validation-2.0 form (List<@Valid T>, Map<String, @Valid V>) — which
cascades identically but is not deprecated. Backward-compatible: element
validation is preserved, never dropped; single-object @Valid is untouched.
Verified on Spring Boot 3.3 (Spring 6.1) and 4.1 (Spring 7): a container
parameter carrying only the type-argument @Valid still triggers element
validation via method validation, with no HV000271.
Scope (Java family): spring, java client, JAX-RS (jersey, resteasy(+eap),
cxf(+extended/cdi), spec), java-camel, java-msf4j.
Models (property/getter): the container @Valid is moved to the type argument.
Map values are gated behind a new
AbstractJavaCodegen#useBeanValidationOnMapValueType() (default false; overridden
true in Spring/JavaClient/JAX-RS) so untouched generators don't silently gain
map-value validation. Arrays/sets already injected the type-argument form.
Parameters (@RequestBody/@RequestParam/@RequestPart): the redundant
container-level @Valid is dropped for container parameters; element validation
is driven by the type-argument @Valid. Single-object parameters keep @Valid.
Reactive Mono/Flux bodies are intentionally untouched (not Jakarta containers,
so no HV000271).
Regenerated all affected samples and added/updated codegen tests in
SpringCodegenTest, JavaClientCodegenTest, JavaJAXRSSpecServerCodegenTest and
JavaValidationArrayPrimitivesTest.
4691ca2 to
fd351f8
Compare
|
A couple of notes to save reviewers some time. On the automated review flagging that dropping the parameter-level
|
Fixes Hibernate Validator HV000271 ("Using
@Validon a container is deprecated. You should apply the annotation on the type argument(s)") across the Java generator family. HV 9.1+ (bundled with Spring Boot 4) logs this at WARN for every@Valid-annotatedList/Set/Map— on both model properties and API parameters.The fix relocates
@Validfrom the container to its type argument — the Bean-Validation-2.0 form (List<@Valid T>,Map<String, @Valid V>) — which cascades identically but is not deprecated. Backward-compatible: element validation is preserved, never dropped.Scope (Java family):
spring,javaclient, JAX-RS (jersey,resteasy(+eap),cxf(+extended/cdi),spec),java-camel.@Validis moved to the type argument. Map values are gated behind a newAbstractJavaCodegen#useBeanValidationOnMapValueType()(defaultfalse; overriddentruein Spring/JavaClient/JAX-RS) so untouched generators don't silently gain map-value validation. Arrays/sets already injected the type-argument form.@RequestBody/@RequestParam/@RequestPart): the redundant container-level@Validis dropped for container parameters; element validation is driven by the type-argument@Valid. Single-object parameters keep their@Valid. ReactiveMono/Fluxbodies are intentionally untouched — they are not Jakarta containers and do not trigger HV000271.The
microprofileclientbodyParamschange is a consistency change with no sample impact (those samples don't enable bean validation).Tests: added
SpringCodegenTest#beanValidationOnContainerTypeArgument_issue23614and#beanValidationOnContainerParameter_issue23614,JavaClientCodegenTest#testBeanValidationOnContainerTypeArgument_issue23614, updatedJavaJAXRSSpecServerCodegenTest; the reactive test now also asserts the parameter-level@Validis retained.Fixes #23614
PR checklist
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.
Summary by cubic
Moves bean-validation
@Validfrom container types to their type arguments to eliminate Hibernate Validator HV000271 while keeping cascade validation. Applies to models and API parameters across Spring, Java client, JAX-RS, and Java Camel; reactive bodies are unchanged, fixing #23614.@ValidtoList/Setitems and map value types; gated viaAbstractJavaCodegen#useBeanValidationOnMapValueType()(overridden totrueinSpringCodegen,JavaClientCodegen,AbstractJavaJAXRSServerCodegen). Removed container-level@Validin Java/JAX-RS/Spring/Java Camel templates.@Validon@RequestBody,@RequestParam,@RequestPartfor containers (incl. MicroProfile/JAX-RS); keep for single objects; reactiveMono/Fluxretain parameter-level@Valid.Written for commit fd351f8. Summary will update on new commits.