Allow to map null value from yaml [SPR-15425]

See original GitHub issue

Simon 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:

Issue Links:

  • #21310 YamlPropertiesFactoryBean loses entries in the YAML document that have an empty array value

4 votes, 5 watchers

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:23
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
snicollcommented, Feb 24, 2021

I think it is important to put this issue in context. While it is true that YamlProcessor is mapping null to an empty String too high in the hierarchy, we took that relaxed approach because Spring’s Environment does not have the capability of storing that information at the moment anyway (null is not handled).

Fixing this bug, in the current state, would only help users that are using YamlProcessor directly. A user of, say Spring Boot, wouldn’t see a single difference if we fixed it for the reason I’ve expressed above.

1reaction
sbrannencommented, Feb 22, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading a map from yaml in Java getting null - Stack Overflow
@Value work as Spring loads the YAML file without any problem using your context file, however it does not mean UserLimitReader file is ......
Read more >
How to represent null values in YAML - Educative.io
The most common way is to use the null keyword: null. Represent null in YAML using "null" keyword · We can also use...
Read more >
Null Language-Independent Type for YAML™ Version 1.1
A null value is used to indicate the lack of a value. This is typically converted into any native null-like value (e.g., undef...
Read more >
Jackson - Working with Maps and nulls - Baeldung
Jackson has a simple but useful way of globally controlling what happens to null values when a Map gets serialized:
Read more >
Problem: Unable to configure fields to ensure null values are ...
Technical Article Details : Problem: Unable to configure fields to ensure null values are not allowed in ArcGIS Field Maps.
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