From 2e3187d1f6a3e84908aaffc16e489c84b319ad49 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Tue, 24 Oct 2017 14:50:28 -0700 Subject: [PATCH] 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 --- .../boot/env/OriginTrackedYamlLoader.java | 6 +++++- .../boot/env/OriginTrackedYamlLoaderTests.java | 10 ++++++++++ .../org/springframework/boot/env/test-yaml.yml | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedYamlLoader.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedYamlLoader.java index d5415e18e8..c38030c1b8 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedYamlLoader.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedYamlLoader.java @@ -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) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/OriginTrackedYamlLoaderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/OriginTrackedYamlLoaderTests.java index af062965c7..4b8c777f44 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/OriginTrackedYamlLoaderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/OriginTrackedYamlLoaderTests.java @@ -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(); diff --git a/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/env/test-yaml.yml b/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/env/test-yaml.yml index dd5c3ed6e6..ef4aeb09c9 100644 --- a/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/env/test-yaml.yml +++ b/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/env/test-yaml.yml @@ -24,6 +24,8 @@ example: bar: - bar1: baz - bar2: bling +empty: "" +null-value: null --- spring: