Revert "Nested Map should not bind to null"
This reverts commit 5406567783.
This commit is contained in:
parent
5406567783
commit
e64090838c
|
|
@ -298,10 +298,11 @@ public class Binder {
|
||||||
}
|
}
|
||||||
BeanPropertyBinder propertyBinder = (propertyName, propertyTarget) -> bind(
|
BeanPropertyBinder propertyBinder = (propertyName, propertyTarget) -> bind(
|
||||||
name.append(propertyName), propertyTarget, handler, context);
|
name.append(propertyName), propertyTarget, handler, context);
|
||||||
if (context.isProcessingName(name)) {
|
Class<?> type = target.getType().resolve();
|
||||||
|
if (context.hasBoundBean(type)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return context.withName(name, () -> {
|
return context.withBean(type, () -> {
|
||||||
Stream<?> boundBeans = BEAN_BINDERS.stream()
|
Stream<?> boundBeans = BEAN_BINDERS.stream()
|
||||||
.map((b) -> b.bind(name, target, context, propertyBinder));
|
.map((b) -> b.bind(name, target, context, propertyBinder));
|
||||||
return boundBeans.filter(Objects::nonNull).findFirst().orElse(null);
|
return boundBeans.filter(Objects::nonNull).findFirst().orElse(null);
|
||||||
|
|
@ -352,7 +353,7 @@ public class Binder {
|
||||||
private final List<ConfigurationPropertySource> source = Arrays
|
private final List<ConfigurationPropertySource> source = Arrays
|
||||||
.asList((ConfigurationPropertySource) null);
|
.asList((ConfigurationPropertySource) null);
|
||||||
|
|
||||||
private final Deque<ConfigurationPropertyName> names = new ArrayDeque<>();
|
private final Deque<Class<?>> beans = new ArrayDeque<>();
|
||||||
|
|
||||||
private ConfigurationProperty configurationProperty;
|
private ConfigurationProperty configurationProperty;
|
||||||
|
|
||||||
|
|
@ -384,13 +385,13 @@ public class Binder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T withName(ConfigurationPropertyName name, Supplier<T> supplier) {
|
public <T> T withBean(Class<?> bean, Supplier<T> supplier) {
|
||||||
this.names.push(name);
|
this.beans.push(bean);
|
||||||
try {
|
try {
|
||||||
return withIncreasedDepth(supplier);
|
return withIncreasedDepth(supplier);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
this.names.pop();
|
this.beans.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -420,8 +421,8 @@ public class Binder {
|
||||||
return StreamSupport.stream(Binder.this.sources.spliterator(), false);
|
return StreamSupport.stream(Binder.this.sources.spliterator(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isProcessingName(ConfigurationPropertyName name) {
|
public boolean hasBoundBean(Class<?> bean) {
|
||||||
return this.names.contains(name);
|
return this.beans.contains(bean);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,7 @@ package org.springframework.boot.context.properties.bind;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.assertj.core.matcher.AssertionMatcher;
|
import org.assertj.core.matcher.AssertionMatcher;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
@ -239,19 +237,6 @@ public class BinderTests {
|
||||||
this.binder.bind("foo", target);
|
this.binder.bind("foo", target);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void nestedMapsShouldNotBindToNull() throws Exception {
|
|
||||||
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
|
|
||||||
source.put("foo.value", "one");
|
|
||||||
source.put("foo.foos.foo1.value", "two");
|
|
||||||
source.put("foo.foos.foo2.value", "three");
|
|
||||||
this.sources.add(source);
|
|
||||||
BindResult<Foo> foo = this.binder.bind("foo", Foo.class);
|
|
||||||
assertThat(foo.get().getValue()).isNotNull();
|
|
||||||
assertThat(foo.get().getFoos().get("foo1").getValue()).isEqualTo("two");
|
|
||||||
assertThat(foo.get().getFoos().get("foo2").getValue()).isEqualTo("three");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class JavaBean {
|
public static class JavaBean {
|
||||||
|
|
||||||
private String value;
|
private String value;
|
||||||
|
|
@ -278,24 +263,4 @@ public class BinderTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static class Foo {
|
|
||||||
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
private Map<String, Foo> foos = new LinkedHashMap<>();
|
|
||||||
|
|
||||||
public Map<String, Foo> getFoos() {
|
|
||||||
return this.foos;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getValue() {
|
|
||||||
return this.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue(String value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue