Null values from yaml should be stored as empty string
When building a flattened map, the YamlProcessor from Spring Framework, converts a null value to an empty string. We want the null value to also keep track of its origin, which is why this commit creates an `OriginTrackedValue` for an empty string if the original value is null. Fixes gh-10656
This commit is contained in:
@@ -108,7 +108,11 @@ class OriginTrackedYamlLoader extends YamlProcessor {
|
||||
|
||||
private Object constructTrackedObject(Node node, Object value) {
|
||||
Origin origin = getOrigin(node);
|
||||
return OriginTrackedValue.of(value, origin);
|
||||
return OriginTrackedValue.of(getValue(value), origin);
|
||||
}
|
||||
|
||||
private Object getValue(Object value) {
|
||||
return (value != null ? value : "");
|
||||
}
|
||||
|
||||
private Origin getOrigin(Node node) {
|
||||
|
||||
@@ -114,6 +114,16 @@ public class OriginTrackedYamlLoaderTests {
|
||||
assertThat(getLocation(bar2)).isEqualTo("26:19");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void processEmptyAndNullValues() throws Exception {
|
||||
OriginTrackedValue empty = getValue("empty");
|
||||
OriginTrackedValue nullValue = getValue("null-value");
|
||||
assertThat(empty.getValue()).isEqualTo("");
|
||||
assertThat(getLocation(empty)).isEqualTo("27:8");
|
||||
assertThat(nullValue.getValue()).isEqualTo("");
|
||||
assertThat(getLocation(nullValue)).isEqualTo("28:13");
|
||||
}
|
||||
|
||||
private OriginTrackedValue getValue(String name) {
|
||||
if (this.result == null) {
|
||||
this.result = this.loader.load();
|
||||
|
||||
@@ -24,6 +24,8 @@ example:
|
||||
bar:
|
||||
- bar1: baz
|
||||
- bar2: bling
|
||||
empty: ""
|
||||
null-value: null
|
||||
---
|
||||
|
||||
spring:
|
||||
|
||||
Reference in New Issue
Block a user