Commit ebae76b1 authored by Phillip Webb's avatar Phillip Webb

Protect against null BindHandler.onStart result

Fixes gh-18129
parent 1851f711
...@@ -285,10 +285,11 @@ public class Binder { ...@@ -285,10 +285,11 @@ public class Binder {
boolean allowRecursiveBinding, boolean create) { boolean allowRecursiveBinding, boolean create) {
context.clearConfigurationProperty(); context.clearConfigurationProperty();
try { try {
target = handler.onStart(name, target, context); Bindable<T> replacementTarget = handler.onStart(name, target, context);
if (target == null) { if (replacementTarget == null) {
return handleBindResult(name, target, handler, context, null, create); return handleBindResult(name, target, handler, context, null, create);
} }
target = replacementTarget;
Object bound = bindObject(name, target, handler, context, allowRecursiveBinding); Object bound = bindObject(name, target, handler, context, allowRecursiveBinding);
return handleBindResult(name, target, handler, context, bound, create); return handleBindResult(name, target, handler, context, bound, create);
} }
......
...@@ -318,6 +318,20 @@ class BinderTests { ...@@ -318,6 +318,20 @@ class BinderTests {
assertThat(value).isInstanceOf(JavaBean.class); assertThat(value).isInstanceOf(JavaBean.class);
} }
@Test
void bindToJavaBeanWhenHandlerOnStartReturnsNullShouldReturnUnbound() { // gh-18129
this.sources.add(new MockConfigurationPropertySource("foo.value", "bar"));
BindResult<JavaBean> result = this.binder.bind("foo", Bindable.of(JavaBean.class), new BindHandler() {
@Override
public <T> Bindable<T> onStart(ConfigurationPropertyName name, Bindable<T> target, BindContext context) {
return null;
}
});
assertThat(result.isBound()).isFalse();
}
static class JavaBean { static class JavaBean {
private String value; private String value;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment