From bac2313fe6f7c6c4e098d5e59cc0155a825f2edf Mon Sep 17 00:00:00 2001 From: Ahmed El amraouiyine Date: Mon, 15 Jun 2026 22:25:31 +0100 Subject: [PATCH] Bind empty strings to empty maps Signed-off-by: Ahmed El amraouiyine --- .../context/properties/bind/MapBinder.java | 4 ++++ .../properties/bind/MapBinderTests.java | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/core/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/MapBinder.java b/core/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/MapBinder.java index f1e57fa32eee..a8aa438edf6a 100644 --- a/core/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/MapBinder.java +++ b/core/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/MapBinder.java @@ -33,6 +33,7 @@ import org.springframework.boot.context.properties.source.IterableConfigurationPropertySource; import org.springframework.core.CollectionFactory; import org.springframework.core.ResolvableType; +import org.springframework.util.ObjectUtils; /** * {@link AggregateBinder} for Maps. @@ -64,6 +65,9 @@ protected boolean isAllowRecursiveBinding(@Nullable ConfigurationPropertySource if (property != null) { getContext().setConfigurationProperty(property); Object result = getContext().getPlaceholdersResolver().resolvePlaceholders(property.getValue()); + if (ObjectUtils.isEmpty(result)) { + return createMap(target); + } return getContext().getConverter().convert(result, target); } } diff --git a/core/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java b/core/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java index 7b591d785a14..1cd946356043 100644 --- a/core/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java +++ b/core/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java @@ -274,6 +274,15 @@ void bindToMapWhenNoValueShouldReturnUnbound() { assertThat(result.isBound()).isFalse(); } + @Test + void bindToMapWhenEmptyStringShouldReturnEmptyMap() { + MockConfigurationPropertySource source = new MockConfigurationPropertySource(); + source.put("foo", ""); + this.sources.add(source); + Map result = this.binder.bind("foo", STRING_STRING_MAP).get(); + assertThat(result).isEmpty(); + } + @Test void bindToMapShouldConvertKey() { MockConfigurationPropertySource source = new MockConfigurationPropertySource(); @@ -510,6 +519,17 @@ void nestedMapsShouldNotBindToNull() { assertThat(foo2.getValue()).isEqualTo("three"); } + @Test + void nestedMapsWhenEmptyStringShouldReturnEmptyMap() { + MockConfigurationPropertySource source = new MockConfigurationPropertySource(); + source.put("foo.value", "one"); + source.put("foo.foos", ""); + this.sources.add(source); + BindResult foo = this.binder.bind("foo", NestableFoo.class); + assertThat(foo.get().getValue()).isEqualTo("one"); + assertThat(foo.get().getFoos()).isEmpty(); + } + @Test void bindToMapWithCustomConverter() { DefaultConversionService conversionService = new DefaultConversionService();