From 43f2334e82bdbe3a2a41120ae68130360f1f88e8 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sun, 29 Apr 2018 10:29:47 +0200 Subject: [PATCH] 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 --- .../beans/factory/config/YamlProcessor.java | 12 ++++++++---- .../factory/config/YamlMapFactoryBeanTests.java | 17 ++++++++++++++++- .../config/YamlPropertiesFactoryBeanTests.java | 11 ++++++++++- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java index 2a75fa50cde..fb55ab37ccd 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java @@ -293,10 +293,14 @@ public abstract class YamlProcessor { // Need a compound key @SuppressWarnings("unchecked") Collection collection = (Collection) value; - int count = 0; - for (Object object : collection) { - buildFlattenedMap(result, - Collections.singletonMap("[" + (count++) + "]", object), key); + if (collection.isEmpty()) { + result.put(key, ""); + } else { + int count = 0; + for (Object object : collection) { + buildFlattenedMap(result, Collections.singletonMap( + "[" + (count++) + "]", object), key); + } } } else { diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlMapFactoryBeanTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlMapFactoryBeanTests.java index bbd3c742407..91c0053e682 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlMapFactoryBeanTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlMapFactoryBeanTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package org.springframework.beans.factory.config; import java.io.IOException; import java.io.InputStream; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import org.junit.Test; @@ -116,6 +117,20 @@ public class YamlMapFactoryBeanTests { assertEquals(Integer.valueOf(3), sub.get("key1.key2")); } + @Test + public void mapWithEmptyArrayValue() { + this.factory.setResources(new ByteArrayResource("a: alpha\ntest: []".getBytes())); + assertTrue(this.factory.getObject().containsKey("test")); + assertEquals(((List)this.factory.getObject().get("test")).size(), 0); + } + + @Test + public void mapWithEmptyValue() { + this.factory.setResources(new ByteArrayResource("a: alpha\ntest:".getBytes())); + assertTrue(this.factory.getObject().containsKey("test")); + assertNull(this.factory.getObject().get("test")); + } + @Test public void testDuplicateKey() throws Exception { this.factory.setResources(new ByteArrayResource("mymap:\n foo: bar\nmymap:\n bar: foo".getBytes())); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBeanTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBeanTests.java index 6120d965de8..838dcb50b91 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBeanTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBeanTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -207,6 +207,15 @@ public class YamlPropertiesFactoryBeanTests { assertThat(properties.getProperty("spam"), equalTo("")); } + @Test + public void testLoadEmptyArrayValue() { + YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); + factory.setResources(new ByteArrayResource("a: alpha\ntest: []".getBytes())); + Properties properties = factory.getObject(); + assertThat(properties.getProperty("a"), equalTo("alpha")); + assertThat(properties.getProperty("test"), equalTo("")); + } + @Test public void testLoadArrayOfString() throws Exception { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();