Allow to map null value from yaml [SPR-15425]
See original GitHub issueSimon Stratmann opened SPR-15425 and commented
The following issue occured to me while using Spring Boot but is caused by Spring code.
With Spring Boot I have a String property that is mapped to an application.yml by using @ConfigurationProperties. The value in the yml file is null (or ~). But the values are initialized with “” (empty String). According to YAML specification it does allow null values.
The culprit is org.springframework.beans.factory.config.YamlProcessor#buildFlattenedMap which executes result.put(key, (value != null ? value : "")).
I’ve extended the file and configured it to be used and this seems to work fine. But I have to take over a lot of final and private code which makes it a bit brittle. I think the class should support null values, explicitly remark that their converted to “” or make a change in behavior a bit more comfortable.
Code excerpts:
test:
setting: ~
@ConfigurationProperties("test")
public class Test {
String setting;
public void setSetting(String setting) {
this.setting = setting; //Will set "setting" to "" instead of null
}
}
Thanks for your time.
Affects: 4.3.7
Attachments:
- demo.zip (5.25 kB)
Issue Links:
- #21310 YamlPropertiesFactoryBean loses entries in the YAML document that have an empty array value
4 votes, 5 watchers
Issue Analytics
- State:
- Created 6 years ago
- Reactions:23
- Comments:11 (2 by maintainers)
Top Related StackOverflow Question
I think it is important to put this issue in context. While it is true that
YamlProcessoris mapping null to an empty String too high in the hierarchy, we took that relaxed approach because Spring’sEnvironmentdoes not have the capability of storing that information at the moment anyway (nullis not handled).Fixing this bug, in the current state, would only help users that are using
YamlProcessordirectly. A user of, say Spring Boot, wouldn’t see a single difference if we fixed it for the reason I’ve expressed above.@incodemode, as @wilkinsona hinted at in https://github.com/spring-projects/spring-boot/issues/4877#issuecomment-782233621, this issue is still open and waiting for triage (i.e., waiting for further consideration/investigation).
The original PR for this issue (#1798) was declined as explained in https://github.com/spring-projects/spring-framework/issues/19986#issuecomment-453453915 above.