diff --git a/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java b/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java index 8e17f2cffdd6..1f915bd68107 100644 --- a/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java @@ -375,7 +375,7 @@ private Object getPropertyHoldingValue(PropertyPath.Segment segment) { PropertyPath.Segment getterSegment = segment.withoutLastKey(); Object propValue; try { - propValue = getPropertyValue(getterSegment); + propValue = getPropertyValue(getterSegment, isAutoGrowNestedPaths()); } catch (NotReadablePropertyException ex) { throw new NotWritablePropertyException(getRootClass(), this.nestedPath + segment.toCanonicalName(), @@ -601,8 +601,14 @@ public boolean isWritableProperty(String propertyName) { return resolved.accessor().getPropertyValue(resolved.segment()); } - @SuppressWarnings({"rawtypes", "unchecked"}) protected @Nullable Object getPropertyValue(PropertyPath.Segment segment) throws BeansException { + return getPropertyValue(segment, false); + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + private @Nullable Object getPropertyValue(PropertyPath.Segment segment, boolean autoGrowMapValues) + throws BeansException { + String propertyName = segment.toCanonicalName(); String actualName = segment.name(); PropertyHandler ph = getLocalPropertyHandler(actualName); @@ -666,6 +672,13 @@ else if (value instanceof Map map) { TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(mapKeyType); Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType, typeDescriptor); value = map.get(convertedMapKey); + if (value == null && autoGrowMapValues) { + String indexedMapKeyName = indexedPropertyName + + PROPERTY_KEY_PREFIX + key + PROPERTY_KEY_SUFFIX; + TypeDescriptor mapValueType = ph.getMapValueType(i + 1); + value = newValue(mapValueType.getType(), mapValueType, indexedMapKeyName); + map.put(convertedMapKey, value); + } } else if (value instanceof Iterable iterable) { // Apply index to Iterator in case of a Set/Collection/Iterable. diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java index 1c869572ee15..e062eb0a58cb 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java @@ -21,7 +21,6 @@ import java.util.Map; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.util.AutoPopulatingList; @@ -390,12 +389,30 @@ void setPropertyValueAutoGrowNestedMapWithinMap() { assertThat(bean.getNestedMap().get("A").get("B")).isInstanceOf(Bean.class); } - @Test @Disabled // gh-32154 + @Test void setPropertyValueAutoGrowNestedNestedMapWithinMap() { wrapper.setPropertyValue("nestedNestedMap[A][B][C]", new Bean()); assertThat(bean.getNestedNestedMap().get("A").get("B").get("C")).isInstanceOf(Bean.class); } + @Test + void setPropertyValueAutoGrowNestedNestedMapPreservesExistingValues() { + Bean existing = new Bean(); + Bean added = new Bean(); + wrapper.setPropertyValue("nestedNestedMap[A][B][C]", existing); + wrapper.setPropertyValue("nestedNestedMap[A][B][D]", added); + assertThat(bean.getNestedNestedMap().get("A").get("B")) + .containsEntry("C", existing).containsEntry("D", added); + } + + @Test + void getPropertyValueDoesNotAutoGrowNestedMapValues() { + bean.setNestedNestedMap(Map.of("A", Map.of())); + assertThatExceptionOfType(NullValueInNestedPathException.class) + .isThrownBy(() -> wrapper.getPropertyValue("nestedNestedMap[A][B][C]")); + assertThat(bean.getNestedNestedMap().get("A")).isEmpty(); + } + @SuppressWarnings("rawtypes") public static class Bean {