You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a JSR 303 implementation is on the classpath, Spring Boot validates all Configuration Properties classes that are annotated with @Validated (see ConfigurationPropertiesBinder::getValidators). But the validation applies to all nested objects, even if they're not annotated with @Valid. This doesn't comply with the JSR 303 spec (see the notion of traversable properties) and can lead to some surprising results. For example, the bean below will fail validation given a single property props.nested.myprop1=value, even though the field private Nested nested isn't annotated with @Valid. Likewise, nested collections and maps will fail validation if they contain objects that are themselves annotated with jsr 303 annotations (ex: @NotEmpty), even if validation isn't cascaded with an @Valid. See this reproducer.
The documentation is also somewhat confusing for this. It states:
To ensure that validation is always triggered for nested properties, even when no properties are found, the associated field must be annotated with @Valid.
But nested objects will not be validated if there are no properties to bind. For example, adding @Valid to the private Nested nested field above will not result in a failure if there is no props.nested.myprop1 property.
I can think of two solutions:
Propagate validation to @Valid fields only.
Modify the documentation to explicitly say that all nested objects will be validated if the configuration properties class is @Validated.
I personally prefer option 1, because it corresponds to behaviour that is expected from a JSR 303 validator.
The text was updated successfully, but these errors were encountered:
When a JSR 303 implementation is on the classpath, Spring Boot validates all Configuration Properties classes that are annotated with
@Validated
(seeConfigurationPropertiesBinder::getValidators
). But the validation applies to all nested objects, even if they're not annotated with@Valid
. This doesn't comply with the JSR 303 spec (see the notion of traversable properties) and can lead to some surprising results. For example, the bean below will fail validation given a single propertyprops.nested.myprop1=value
, even though the fieldprivate Nested nested
isn't annotated with@Valid
. Likewise, nested collections and maps will fail validation if they contain objects that are themselves annotated with jsr 303 annotations (ex:@NotEmpty
), even if validation isn't cascaded with an@Valid
. See this reproducer.The documentation is also somewhat confusing for this. It states:
But nested objects will not be validated if there are no properties to bind. For example, adding
@Valid
to theprivate Nested nested
field above will not result in a failure if there is noprops.nested.myprop1
property.I can think of two solutions:
@Valid
fields only.@Validated
.I personally prefer option 1, because it corresponds to behaviour that is expected from a JSR 303 validator.
The text was updated successfully, but these errors were encountered: