Skip to content

Commit e640908

Browse files
committed
Revert "Nested Map should not bind to null"
This reverts commit 5406567.
1 parent 5406567 commit e640908

File tree

2 files changed

+9
-43
lines changed
  • spring-boot/src
    • main/java/org/springframework/boot/context/properties/bind
    • test/java/org/springframework/boot/context/properties/bind

2 files changed

+9
-43
lines changed

spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,11 @@ private Object bindBean(ConfigurationPropertyName name, Bindable<?> target,
298298
}
299299
BeanPropertyBinder propertyBinder = (propertyName, propertyTarget) -> bind(
300300
name.append(propertyName), propertyTarget, handler, context);
301-
if (context.isProcessingName(name)) {
301+
Class<?> type = target.getType().resolve();
302+
if (context.hasBoundBean(type)) {
302303
return null;
303304
}
304-
return context.withName(name, () -> {
305+
return context.withBean(type, () -> {
305306
Stream<?> boundBeans = BEAN_BINDERS.stream()
306307
.map((b) -> b.bind(name, target, context, propertyBinder));
307308
return boundBeans.filter(Objects::nonNull).findFirst().orElse(null);
@@ -352,7 +353,7 @@ final class Context implements BindContext {
352353
private final List<ConfigurationPropertySource> source = Arrays
353354
.asList((ConfigurationPropertySource) null);
354355

355-
private final Deque<ConfigurationPropertyName> names = new ArrayDeque<>();
356+
private final Deque<Class<?>> beans = new ArrayDeque<>();
356357

357358
private ConfigurationProperty configurationProperty;
358359

@@ -384,13 +385,13 @@ public <T> T withSource(ConfigurationPropertySource source,
384385
}
385386
}
386387

387-
public <T> T withName(ConfigurationPropertyName name, Supplier<T> supplier) {
388-
this.names.push(name);
388+
public <T> T withBean(Class<?> bean, Supplier<T> supplier) {
389+
this.beans.push(bean);
389390
try {
390391
return withIncreasedDepth(supplier);
391392
}
392393
finally {
393-
this.names.pop();
394+
this.beans.pop();
394395
}
395396
}
396397

@@ -420,8 +421,8 @@ public Stream<ConfigurationPropertySource> streamSources() {
420421
return StreamSupport.stream(Binder.this.sources.spliterator(), false);
421422
}
422423

423-
public boolean isProcessingName(ConfigurationPropertyName name) {
424-
return this.names.contains(name);
424+
public boolean hasBoundBean(Class<?> bean) {
425+
return this.beans.contains(bean);
425426
}
426427

427428
@Override

spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
import java.time.LocalDate;
2020
import java.util.ArrayList;
2121
import java.util.Collections;
22-
import java.util.LinkedHashMap;
2322
import java.util.List;
24-
import java.util.Map;
2523

2624
import org.assertj.core.matcher.AssertionMatcher;
2725
import org.junit.Before;
@@ -239,19 +237,6 @@ public void assertion(BindException ex) throws AssertionError {
239237
this.binder.bind("foo", target);
240238
}
241239

242-
@Test
243-
public void nestedMapsShouldNotBindToNull() throws Exception {
244-
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
245-
source.put("foo.value", "one");
246-
source.put("foo.foos.foo1.value", "two");
247-
source.put("foo.foos.foo2.value", "three");
248-
this.sources.add(source);
249-
BindResult<Foo> foo = this.binder.bind("foo", Foo.class);
250-
assertThat(foo.get().getValue()).isNotNull();
251-
assertThat(foo.get().getFoos().get("foo1").getValue()).isEqualTo("two");
252-
assertThat(foo.get().getFoos().get("foo2").getValue()).isEqualTo("three");
253-
}
254-
255240
public static class JavaBean {
256241

257242
private String value;
@@ -278,24 +263,4 @@ public enum ExampleEnum {
278263

279264
}
280265

281-
static class Foo {
282-
283-
private String value;
284-
285-
private Map<String, Foo> foos = new LinkedHashMap<>();
286-
287-
public Map<String, Foo> getFoos() {
288-
return this.foos;
289-
}
290-
291-
public String getValue() {
292-
return this.value;
293-
}
294-
295-
public void setValue(String value) {
296-
this.value = value;
297-
}
298-
299-
}
300-
301266
}

0 commit comments

Comments
 (0)