question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@ConfigurationProperties class's default values are not visible in the Errors instance passed to Validator.validate(Object target, Errors errors)

See original GitHub issue

Spring 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:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Feb 18, 2021

Sorry, I was mistaken above. I’d forgotten that we’d added support for a @ConfigurationProperties bean itself being a Validator.

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 whereas ValidationResult 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.

0reactions
cdpretecommented, Feb 18, 2021

I’ve just followed the flow from ValidationUtils.rejectIfEmpty and ended up in ValidationBindHandler 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found