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:
Madhura Bhave 2017-10-24 14:50:28 -07:00
parent 182b6f0d29
commit 2e3187d1f6
3 changed files with 17 additions and 1 deletions

View File

@ -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) {

View File

@ -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();

View File

@ -24,6 +24,8 @@ example:
bar:
- bar1: baz
- bar2: bling
empty: ""
null-value: null
---
spring: