Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down