-
Notifications
You must be signed in to change notification settings - Fork 40.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validation is not applied for ConfigurationProperties that implement Validator and use @ConstructorBinding #33669
Comments
First analysis points out to these lines: I believe following change would fix the issue (to be tested): if (Validator.class.isAssignableFrom(target.type.getRawClass())) {
if (target.getValue() != null) {
validators.add((Validator) target.getValue().get());
} else {
validators.add((Validator) getBinder().bindOrCreate("validator", target));
}
} |
I think this is a bug, but for what it's worth I don't think it makes as much sense to use For setter based binding, it's useful to have the |
Validate callback also eases aggregation of multiple validation errors in one response for a given properties class. Validate callback also provides out-of-the-box a clear error message in application logs:
I was not able to get this clear error message when throwing explicitly a BindException. In this context, it would still make sense to use Validator with constructor binding. |
In this article (https://reflectoring.io/validate-spring-boot-configuration-parameters-at-startup/ ), it is advised to implement Validator interface in class annotated with
@ConfigurationProperties
for complex checks.This mechanism works well when class is not annotated with @ConstructorBinding.
It doesn't work if class is annotated with
@ConstructorBinding
or if class is implemented as an immutable class in Spring Boot 3.0.0.I've reproduced the issue in a Java project on this github repository for Spring Boot 2.7.7 (branch
sb27
) and for Spring Boot 3.0.0 (branchmaster
).In test
SimpleDocumentationPropertiesTest
, validate method of classSimpleDocumentationProperties
is well called.This class is a simple POJO with
@ConfigurationProperties
annotation.In test
ConstructorBindingDocumentationPropertiesTest
, test fails because validate method of classConstructorBindingDocumentationProperties
is not called.This class is implemented as an immutable class with same
@ConfigurationProperties
annotation and additional@ConstructorBinding
annotation (for Spring Boot 2).Issue can also be reproduced in Kotlin projects when ConfigurationProperties classes are implemented with data classes.
I'd like to use immutable classes for all my configuration properties classes and still be able to use Validator interface for complex checks.
The text was updated successfully, but these errors were encountered: