Don't fail if aggregate merge can't get existing value
Fixes gh-13303
This commit is contained in:
@@ -55,10 +55,9 @@ class CollectionBinder extends IndexedElementsBinder<Collection<Object>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Collection<Object> merge(Supplier<?> existing,
|
||||
Collection<Object> additional) {
|
||||
Collection<Object> existingCollection = (Collection<Object>) existing.get();
|
||||
Collection<Object> existingCollection = getExistingIfPossible(existing);
|
||||
if (existingCollection == null) {
|
||||
return additional;
|
||||
}
|
||||
@@ -72,6 +71,16 @@ class CollectionBinder extends IndexedElementsBinder<Collection<Object>> {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Collection<Object> getExistingIfPossible(Supplier<?> existing) {
|
||||
try {
|
||||
return (Collection<Object>) existing.get();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Collection<Object> copyIfPossible(Collection<Object> collection) {
|
||||
try {
|
||||
return createNewCollection(collection);
|
||||
|
||||
@@ -83,10 +83,9 @@ class MapBinder extends AggregateBinder<Map<Object, Object>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Map<Object, Object> merge(Supplier<?> existing,
|
||||
Map<Object, Object> additional) {
|
||||
Map<Object, Object> existingMap = (Map<Object, Object>) existing.get();
|
||||
Map<Object, Object> existingMap = getExistingIfPossible(existing);
|
||||
if (existingMap == null) {
|
||||
return additional;
|
||||
}
|
||||
@@ -101,6 +100,16 @@ class MapBinder extends AggregateBinder<Map<Object, Object>> {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<Object, Object> getExistingIfPossible(Supplier<?> existing) {
|
||||
try {
|
||||
return (Map<Object, Object>) existing.get();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Map<Object, Object> copyIfPossible(Map<Object, Object> map) {
|
||||
try {
|
||||
return createNewMap(map.getClass(), map);
|
||||
|
||||
@@ -417,6 +417,16 @@ public class CollectionBinderTests {
|
||||
assertThat(bean.getBar()).containsExactly("hello");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindToBeanWithExceptionInGetterForExistingValue() {
|
||||
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
|
||||
source.put("foo.values", "a,b,c");
|
||||
this.sources.add(source);
|
||||
BeanWithGetterException result = this.binder
|
||||
.bind("foo", Bindable.of(BeanWithGetterException.class)).get();
|
||||
assertThat(result.getValues()).containsExactly("a", "b", "c");
|
||||
}
|
||||
|
||||
public static class ExampleCollectionBean {
|
||||
|
||||
private List<String> items = new ArrayList<>();
|
||||
@@ -521,4 +531,18 @@ public class CollectionBinderTests {
|
||||
|
||||
}
|
||||
|
||||
public static class BeanWithGetterException {
|
||||
|
||||
private List<String> values;
|
||||
|
||||
public void setValues(List<String> values) {
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
public List<String> getValues() {
|
||||
return Collections.unmodifiableList(this.values);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -631,6 +631,16 @@ public class MapBinderTests {
|
||||
entry("e", "f"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindToBeanWithExceptionInGetterForExistingValue() {
|
||||
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
|
||||
source.put("foo.values.a", "b");
|
||||
this.sources.add(source);
|
||||
BeanWithGetterException result = this.binder
|
||||
.bind("foo", Bindable.of(BeanWithGetterException.class)).get();
|
||||
assertThat(result.getValues()).containsExactly(entry("a", "b"));
|
||||
}
|
||||
|
||||
private <K, V> Bindable<Map<K, V>> getMapBindable(Class<K> keyGeneric,
|
||||
ResolvableType valueType) {
|
||||
ResolvableType keyType = ResolvableType.forClass(keyGeneric);
|
||||
@@ -736,4 +746,18 @@ public class MapBinderTests {
|
||||
|
||||
}
|
||||
|
||||
public static class BeanWithGetterException {
|
||||
|
||||
private Map<String, String> values;
|
||||
|
||||
public void setValues(Map<String, String> values) {
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
public Map<String, String> getValues() {
|
||||
return Collections.unmodifiableMap(this.values);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user