Support binding to immutable maps

Closes gh-13323
pull/13388/head
Madhura Bhave 7 years ago
parent 68cd27c47b
commit 8ce13c765b

@ -90,22 +90,32 @@ class MapBinder extends AggregateBinder<Map<Object, Object>> {
if (existingMap == null) {
return additional;
}
existingMap.putAll(additional);
return copyIfPossible(existingMap);
try {
existingMap.putAll(additional);
return copyIfPossible(existingMap);
}
catch (UnsupportedOperationException ex) {
Map<Object, Object> result = createNewMap(additional.getClass(), existingMap);
result.putAll(additional);
return result;
}
}
private Map<Object, Object> copyIfPossible(Map<Object, Object> map) {
try {
Map<Object, Object> result = CollectionFactory.createMap(map.getClass(),
map.size());
result.putAll(map);
return result;
return createNewMap(map.getClass(), map);
}
catch (Exception ex) {
return map;
}
}
private Map<Object, Object> createNewMap(Class<?> mapClass, Map<Object, Object> map) {
Map<Object, Object> result = CollectionFactory.createMap(mapClass, map.size());
result.putAll(map);
return result;
}
private class EntryBinder {
private final ConfigurationPropertyName root;

@ -615,6 +615,22 @@ public class MapBinderTests {
assertThat(result.getItems()).containsExactly(entry("a", "b"));
}
@Test
public void bindToImmutableMapShouldReturnPopulatedCollection() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.values.c", "d");
source.put("foo.values.e", "f");
this.sources.add(source);
Map<String, String> result = this.binder
.bind("foo.values",
STRING_STRING_MAP
.withExistingValue(Collections.singletonMap("a", "b")))
.get();
assertThat(result).hasSize(3);
assertThat(result.entrySet()).containsExactly(entry("a", "b"), entry("c", "d"),
entry("e", "f"));
}
private <K, V> Bindable<Map<K, V>> getMapBindable(Class<K> keyGeneric,
ResolvableType valueType) {
ResolvableType keyType = ResolvableType.forClass(keyGeneric);

Loading…
Cancel
Save