Skip to content

Commit 2e3187d

Browse files
committed
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
1 parent 182b6f0 commit 2e3187d

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedYamlLoader.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ private void replaceMappingNodeKeys(MappingNode node) {
108108

109109
private Object constructTrackedObject(Node node, Object value) {
110110
Origin origin = getOrigin(node);
111-
return OriginTrackedValue.of(value, origin);
111+
return OriginTrackedValue.of(getValue(value), origin);
112+
}
113+
114+
private Object getValue(Object value) {
115+
return (value != null ? value : "");
112116
}
113117

114118
private Origin getOrigin(Node node) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/OriginTrackedYamlLoaderTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ public void processListOfMaps() throws Exception {
114114
assertThat(getLocation(bar2)).isEqualTo("26:19");
115115
}
116116

117+
@Test
118+
public void processEmptyAndNullValues() throws Exception {
119+
OriginTrackedValue empty = getValue("empty");
120+
OriginTrackedValue nullValue = getValue("null-value");
121+
assertThat(empty.getValue()).isEqualTo("");
122+
assertThat(getLocation(empty)).isEqualTo("27:8");
123+
assertThat(nullValue.getValue()).isEqualTo("");
124+
assertThat(getLocation(nullValue)).isEqualTo("28:13");
125+
}
126+
117127
private OriginTrackedValue getValue(String name) {
118128
if (this.result == null) {
119129
this.result = this.loader.load();

spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/env/test-yaml.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ example:
2424
bar:
2525
- bar1: baz
2626
- bar2: bling
27+
empty: ""
28+
null-value: null
2729
---
2830

2931
spring:

0 commit comments

Comments
 (0)