Skip to content
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

@ConfigurationProperty validation not working when a key matching the prefix is set #15597

Closed
mamachanko opened this issue Jan 2, 2019 · 3 comments

Comments

Projects
None yet
5 participants
@mamachanko
Copy link

commented Jan 2, 2019

@ConfigurationProperty validation is great as it forces the application to not start if properties are invalid or missing. However, there is a case of partially missing properties that does not work as I expected.

  • Spring Boot version: 2.1.1.RELEASE
  • Java version: 1.8

Steps to reproduce

Consider the following web app which exposes the property app.value at the endpoint /:

package io.github.mamachanko.configurationpropertyvalidation;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

@SpringBootApplication
public class ConfigurationPropertyValidationApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigurationPropertyValidationApplication.class, args);
    }
}

@RestController
class Web {

    private Config config;

    Web(Config config) {
        this.config = config;
    }

    @GetMapping
    public String index() {
        return String.format("The configured value is '%s'.", config.getValue());
    }
}

@Configuration
@ConfigurationProperties(prefix = "app")
@Validated
class Config {

    @NotBlank
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

Assume I provide partially missing configuration in my application.yml as follows:

app:

The application comes up and exposes a null value:

~ curl localhost:8080/
The configured value is 'null'.%

What I would have expected is the same behaviour as for the following case. If I either fail to provide any configuration - application.yml is empty - or fail to provide the value

app:
  value:

... then (as expected) the application fails to come up

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'app' to io.github.mamachanko.configurationpropertyvalidation.Config$$EnhancerBySpringCGLIB$$e06d273 failed:

    Property: app.value
    Value: null
    Reason: must not be blank


Action:

Update your application's configuration

Am I missing an annotation or setting to also fail in the odd case?

Thanks!

@mamachanko mamachanko changed the title `@ConfigurationProperty` validation not working for missing property @ConfigurationProperty validation not working for partially missing property Jan 2, 2019

@mamachanko mamachanko changed the title @ConfigurationProperty validation not working for partially missing property @ConfigurationProperty validation not working for partially missing yaml Jan 2, 2019

@snicoll snicoll added this to the 2.0.x milestone Jan 2, 2019

@snicoll

This comment has been minimized.

Copy link
Member

commented Jan 2, 2019

@mamachanko thanks for the report, it looks like validation does not work properly if a property with key app is set in the Environment (it doesn't matter the value, I've tried app=test and it fails the same way). I've also reproduced the problem without yaml to confirm this isn't yaml specific.

(Side note, please do not flag your @ConfigurationProperties bean as @Configuration: these are not configuration classes providing @Bean definitions but regular @Component classes that are bound to the Environment).

@snicoll snicoll changed the title @ConfigurationProperty validation not working for partially missing yaml @ConfigurationProperty validation not working when a key matching the prefix is set Jan 2, 2019

@mbhave mbhave modified the milestones: 2.0.x, 2.1.x Jan 2, 2019

@mbhave mbhave self-assigned this Jan 2, 2019

mbhave added a commit to mbhave/spring-boot that referenced this issue Jan 2, 2019

@mbhave mbhave closed this in b345fc8 Jan 10, 2019

@izeye

This comment has been minimized.

Copy link
Contributor

commented Jan 10, 2019

It looks the milestone needs to be updated to 2.1.2.

@mbhave mbhave modified the milestones: 2.1.x, 2.1.2 Jan 10, 2019

@mbhave

This comment has been minimized.

Copy link
Contributor

commented Jan 10, 2019

Done. Thanks @izeye!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.