From ebae76b1b8145965875cd83e2506ab3e5c945c71 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 4 Sep 2019 14:02:26 -0700 Subject: [PATCH] Protect against null BindHandler.onStart result Fixes gh-18129 --- .../boot/context/properties/bind/Binder.java | 5 +++-- .../boot/context/properties/bind/BinderTests.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java index 22044710b0..de1328da43 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java @@ -285,10 +285,11 @@ public class Binder { boolean allowRecursiveBinding, boolean create) { context.clearConfigurationProperty(); try { - target = handler.onStart(name, target, context); - if (target == null) { + Bindable replacementTarget = handler.onStart(name, target, context); + if (replacementTarget == null) { return handleBindResult(name, target, handler, context, null, create); } + target = replacementTarget; Object bound = bindObject(name, target, handler, context, allowRecursiveBinding); return handleBindResult(name, target, handler, context, bound, create); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java index af637abf73..c2c03d114b 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java @@ -318,6 +318,20 @@ class BinderTests { assertThat(value).isInstanceOf(JavaBean.class); } + @Test + void bindToJavaBeanWhenHandlerOnStartReturnsNullShouldReturnUnbound() { // gh-18129 + this.sources.add(new MockConfigurationPropertySource("foo.value", "bar")); + BindResult result = this.binder.bind("foo", Bindable.of(JavaBean.class), new BindHandler() { + + @Override + public Bindable onStart(ConfigurationPropertyName name, Bindable target, BindContext context) { + return null; + } + + }); + assertThat(result.isBound()).isFalse(); + } + static class JavaBean { private String value;