Commit 2e3187d1 authored by Madhura Bhave's avatar Madhura Bhave

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
parent 182b6f0d
...@@ -108,7 +108,11 @@ class OriginTrackedYamlLoader extends YamlProcessor { ...@@ -108,7 +108,11 @@ class OriginTrackedYamlLoader extends YamlProcessor {
private Object constructTrackedObject(Node node, Object value) { private Object constructTrackedObject(Node node, Object value) {
Origin origin = getOrigin(node); 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) { private Origin getOrigin(Node node) {
......
...@@ -114,6 +114,16 @@ public class OriginTrackedYamlLoaderTests { ...@@ -114,6 +114,16 @@ public class OriginTrackedYamlLoaderTests {
assertThat(getLocation(bar2)).isEqualTo("26:19"); 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) { private OriginTrackedValue getValue(String name) {
if (this.result == null) { if (this.result == null) {
this.result = this.loader.load(); this.result = this.loader.load();
......
...@@ -24,6 +24,8 @@ example: ...@@ -24,6 +24,8 @@ example:
bar: bar:
- bar1: baz - bar1: baz
- bar2: bling - bar2: bling
empty: ""
null-value: null
--- ---
spring: spring:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment