Skip to content

Commit 43f2334

Browse files
committed
Keep YAML entries that haven an empty array value
Prior to this commit, a YAML entry that define an empty array value was lost. This commit makes sure to flag it with an empty String, which corresponds as an empty comma separated list of entries in the properties format. Issue: SPR-16769
1 parent 6c6e44b commit 43f2334

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,14 @@ else if (value instanceof Collection) {
293293
// Need a compound key
294294
@SuppressWarnings("unchecked")
295295
Collection<Object> collection = (Collection<Object>) value;
296-
int count = 0;
297-
for (Object object : collection) {
298-
buildFlattenedMap(result,
299-
Collections.singletonMap("[" + (count++) + "]", object), key);
296+
if (collection.isEmpty()) {
297+
result.put(key, "");
298+
} else {
299+
int count = 0;
300+
for (Object object : collection) {
301+
buildFlattenedMap(result, Collections.singletonMap(
302+
"[" + (count++) + "]", object), key);
303+
}
300304
}
301305
}
302306
else {

spring-beans/src/test/java/org/springframework/beans/factory/config/YamlMapFactoryBeanTests.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.util.LinkedHashMap;
22+
import java.util.List;
2223
import java.util.Map;
2324

2425
import org.junit.Test;
@@ -116,6 +117,20 @@ public void testMapWithIntegerValue() throws Exception {
116117
assertEquals(Integer.valueOf(3), sub.get("key1.key2"));
117118
}
118119

120+
@Test
121+
public void mapWithEmptyArrayValue() {
122+
this.factory.setResources(new ByteArrayResource("a: alpha\ntest: []".getBytes()));
123+
assertTrue(this.factory.getObject().containsKey("test"));
124+
assertEquals(((List<?>)this.factory.getObject().get("test")).size(), 0);
125+
}
126+
127+
@Test
128+
public void mapWithEmptyValue() {
129+
this.factory.setResources(new ByteArrayResource("a: alpha\ntest:".getBytes()));
130+
assertTrue(this.factory.getObject().containsKey("test"));
131+
assertNull(this.factory.getObject().get("test"));
132+
}
133+
119134
@Test
120135
public void testDuplicateKey() throws Exception {
121136
this.factory.setResources(new ByteArrayResource("mymap:\n foo: bar\nmymap:\n bar: foo".getBytes()));

spring-beans/src/test/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBeanTests.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -207,6 +207,15 @@ public void testLoadNull() throws Exception {
207207
assertThat(properties.getProperty("spam"), equalTo(""));
208208
}
209209

210+
@Test
211+
public void testLoadEmptyArrayValue() {
212+
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
213+
factory.setResources(new ByteArrayResource("a: alpha\ntest: []".getBytes()));
214+
Properties properties = factory.getObject();
215+
assertThat(properties.getProperty("a"), equalTo("alpha"));
216+
assertThat(properties.getProperty("test"), equalTo(""));
217+
}
218+
210219
@Test
211220
public void testLoadArrayOfString() throws Exception {
212221
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();

0 commit comments

Comments
 (0)