Skip to content

Commit c4573f3

Browse files
committed
fix: handle empty nested maps in yaml
1 parent c79fc9b commit c4573f3

3 files changed

Lines changed: 58 additions & 5 deletions

File tree

deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/MapUtils.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,15 @@ private static <V> void addIterable(final Iterable<V> value,
171171
{
172172
if (o instanceof Map)
173173
{
174+
final Map map = (Map) o;
175+
176+
if (map.isEmpty())
177+
{
178+
continue;
179+
}
180+
174181
final String keyPrefix = (indexed) ? key + "[" + index++ + "]" : key;
175-
flattenMapProperties((Map) o, output, indexed, keyPrefix);
182+
flattenMapProperties((Map) map, output, indexed, keyPrefix);
176183
}
177184
else
178185
{

deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/MapUtilsTest.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ public void testFlattenWithObjectArrayLists()
160160
}
161161

162162
/**
163+
* Ignore nested null values.
164+
*
163165
* application:
164166
* name: More Apps
165167
* messages:
@@ -201,8 +203,8 @@ public void testFlattenWithObjectArrayListsWithNull()
201203

202204
Assert.assertEquals(3, result.size());
203205
Assert.assertEquals("More Apps", result.get("application.name"));
204-
Assert.assertEquals("one,null,three", result.get("application.messages.source"));
205-
Assert.assertEquals("two,null,four", result.get("application.messages.target"));
206+
Assert.assertEquals("one,three", result.get("application.messages.source"));
207+
Assert.assertEquals("two,four", result.get("application.messages.target"));
206208
}
207209

208210
/**
@@ -246,6 +248,51 @@ public void testFlattenWithObjectArrayIndexed()
246248
Assert.assertEquals("four", result.get("application.messages[1].target"));
247249
}
248250

251+
/**
252+
* Ignore nested null values.
253+
*
254+
* application:
255+
* name: More Apps
256+
* messages:
257+
* - source: one
258+
* target: two
259+
* - source: null
260+
* target: null
261+
* - source: three
262+
* target: four
263+
*/
264+
@Test
265+
public void testFlattenWithObjectArrayIndexedWithNull()
266+
{
267+
Map<String, String> message1 = new HashMap<>();
268+
message1.put("source", "one");
269+
message1.put("target", "two");
270+
271+
Map<String, String> message2 = new HashMap<>();
272+
message2.put("source", "three");
273+
message2.put("target", "four");
274+
275+
List<Map<String, String>> messages = new ArrayList<>();
276+
messages.add(message1);
277+
messages.add(message2);
278+
279+
Map<String, Object> application = new HashMap<>();
280+
application.put("name", "More Apps");
281+
application.put("messages", messages);
282+
283+
Map<String, Object> map = new HashMap<>();
284+
map.put("application", application);
285+
286+
Map<String, String> result = MapUtils.flattenMapProperties(map, true);
287+
288+
Assert.assertEquals(5, result.size());
289+
Assert.assertEquals("More Apps", result.get("application.name"));
290+
Assert.assertEquals("one", result.get("application.messages[0].source"));
291+
Assert.assertEquals("two", result.get("application.messages[0].target"));
292+
Assert.assertEquals("three", result.get("application.messages[1].source"));
293+
Assert.assertEquals("four", result.get("application.messages[1].target"));
294+
}
295+
249296
/**
250297
* application:
251298
* name: Null App

deltaspike/modules/yaml/impl/src/test/java/org/apache/deltaspike/yaml/test/YamlConfigSourceTest.java renamed to deltaspike/modules/yaml/impl/src/test/java/org/apache/deltaspike/yaml/impl/YamlConfigSourceTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.deltaspike.yaml.test;
19+
package org.apache.deltaspike.yaml.impl;
2020

21-
import org.apache.deltaspike.yaml.impl.YamlConfigSource;
2221
import org.junit.Assert;
2322
import org.junit.Test;
2423

0 commit comments

Comments
 (0)