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
Environment variable that does not follow guidelines for use of _ is still successfully bound if another property source contains a property that is bound to the same target #14479
Comments
Here's a minimal example that reproduces the behaviour described above: package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
@EnableConfigurationProperties(YonaProperties.class)
public class Gh14479Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Gh14479Application.class, args);
YonaProperties yonaProperties = context.getBean(YonaProperties.class);
System.out.println((yonaProperties.getAnalysisService().getAlpha()));
System.out.println((yonaProperties.getAnalysisService().getBravo()));
}
}
@ConfigurationProperties("yona")
class YonaProperties {
private final AnalysisServiceProperties analysisService = new AnalysisServiceProperties();
public AnalysisServiceProperties getAnalysisService() {
return analysisService;
}
public class AnalysisServiceProperties {
private String alpha;
private String bravo;
public String getAlpha() {
return alpha;
}
public void setAlpha(String alpha) {
this.alpha = alpha;
}
public String getBravo() {
return bravo;
}
public void setBravo(String bravo) {
this.bravo = bravo;
}
}
} Run with no properties:
Run with an environment variable and a command line property and both take effect:
Run with just a command line property and it takes effect:
Run with just an environment variable and it has no effect:
|
That's it indeed. Thanks for making the repro! |
When only the environment variable is set, the configuration property name When the property |
The behaviour described above is a symptom of the environment variable not meeting the documented requirements. Specifically, I'm going to leave this open for now as the current behaviour is (to me at least) a bit confusing. Ideally, an environment variable that doesn't meet the required guidelines should never bind, irrespective of any other properties that have been configured. |
That's actually not the case. See the property class implementation here. The property name is camelCased, see |
Sorry, I'm not sure what you think isn't the case. The property class and the configuration in |
Mmm. Is that the aspect of relaxed binding being a little less relaxed in 2.0? :) |
Yes, this is part of the relaxed binding not being quite so relaxed. |
OK, clear. It's a bit unfortunate that it sometimes works, depending on whether or not the property was bound otherwise. |
We'd like to fix this by the binding ignoring the environment variable that's malformed. |
Fixing this as we'd like will regress #10873. I think we have a few options:
2 feels like the least bad option, but I'm not sure how realistic it is. |
I think we should revert the change for #10873 because it's confusing that the 1.5 style properties only work sometimes. |
I wonder if we could make the ancestor checking more lenient? What about ignoring everything that isn’t a letter, and then, ignoring case, seeing if one starts with the other. Occasional false positives aren’t the end of the world as it’s just used as a way of short-circuiting binding isn’t it? |
The ancestor check is also used in the |
I've tried adding a separate, more lenient ancestor check and only calling it from the various
This is happening due to a call to |
I tried something here and it seems like it would work: mbhave@b926b59 @wilkinsona @philwebb What do you think? |
This may have broken spring cloud stream |
We have custom properties, built like this:
Properties would be set on
AnalysisServiceProperties
in this way:Or through an environment variable, like this:
With Spring Boot 1.5, it was OK to not have any
yona.analysisService.xxx
property inapplication.properties
and still set one through an environment variable likeYONA_ANALYSIS_SERVICE_XXX
.With Spring Boot 2.0.4, that doesn't work anymore. At least one
yona.analysisService.xxx
must exist inapplication.properties
. It's OK to setxxx
inapplication.properties
andyyy
through the environment, but there needs to be at least one.The text was updated successfully, but these errors were encountered: