@ConfigurationProperties class's default values are not visible in the Errors instance passed to Validator.validate(Object target, Errors errors)
See original GitHub issueSpring Boot version: 2.4.3-SNAPSHOT
Hi.
I’ve a @ConfigurationProperties
annotated class which implements the Validator
interface to validate some fields, but the method in the subject is never called causing the validation to fail.
In particular, I’ve the following structure:
public class SecurityRequestExtractionProperties {
private CookieExtractionNames cookie = new CookieExtractionNames();
private HeaderExtractionNames header = new HeaderExtractionNames();
// getters and setters here
public static class CookieExtractionNames {
private String requestId = "My-Request-ID";
private String sessionId = "My-Session-ID";
// getters and setters here
}
public static class HeaderExtractionNames {
private String requestId = "My-Request-ID";
private String sessionId = "My-Session-ID";
private String tenant = "My-Tenant";
// getters and setters here
}
}
and
@ConfigurationProperties(prefix = SpringSecurityRequestExtractionProperties.PREFIX)
public class SpringSecurityRequestExtractionProperties
extends SecurityRequestExtractionProperties implements Validator {
public static final String PREFIX = "acme.security.request.extraction";
@Override
public boolean supports(Class<?> clazz) {
return clazz == SpringSecurityRequestExtractionProperties.class;
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmpty(errors, "cookie", "cookie.empty");
}
}
In Spring Boot 2.1.2 the default values specified in the class where used when no value was explicitly configured, but in the specified version the validation actually fails with
Caused by: org.springframework.boot.context.properties.bind.validation.BindValidationException: Binding validation errors on acme.security.request.extraction
- Field error in object 'acme.security.request.extraction' on field 'cookie': rejected value [null]; codes [cookie.empty.acme.security.request.extraction.cookie,cookie.empty.cookie,cookie.empty.acme.commons.security.api.SecurityRequestExtractionProperties$CookieExtractionNames,cookie.empty]; arguments []; default message [null]
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Spring Configuration Properties Nested Custom Validation
I have nested properties class: @ConfigurationProperties(prefix = "myapp", ignoreUnknownFields = false) public class MyAppProperties ...
Read more >Configuring a Spring Boot Module with ... - Reflectoring
An in-depth look at Spring Boot's support to bind external configuration parameters to fields of a Spring bean.
Read more >validation properties spring boot - Berry Health Benefits Symposium
If you will pass invalid values in properties file application through error on the startup . convert(Object, TypeDescriptor, TypeDescriptor) to implement ...
Read more >Validation with Hibernate Validator - Quarkus
It can be extracted and manipulated to display a proper error message. Service method validation. It might not always be handy to have...
Read more >Core Features - Spring
For instance, if you are running your application by using java -jar ... Spring Boot attempts to validate @ConfigurationProperties classes whenever they are ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sorry, I was mistaken above. I’d forgotten that we’d added support for a
@ConfigurationProperties
bean itself being aValidator
.The change in behaviour appears to be a side-effect of the fix for https://github.com/spring-projects/spring-boot/issues/17424.
BeanPropertyBindingResult
will extract a value from the underlying target whereasValidationResult
will not. This means that the latter can only see values that have been bound while the former can also see default values.ValidationBindHandler#onSuccess
is a red herring here as it’s only called when a property’s been bound. That will never happen when relying purely on default values.I’ve just followed the flow from
ValidationUtils.rejectIfEmpty
and ended up inValidationBindHandler
😃