Clear configurationProperty from context before bind

Fixes gh-9295
This commit is contained in:
Madhura Bhave 2017-05-24 10:41:09 -07:00
parent fed8c8a681
commit 2009da6169
2 changed files with 30 additions and 0 deletions

View File

@ -189,6 +189,7 @@ public class Binder {
protected final <T> T bind(ConfigurationPropertyName name, Bindable<T> target, protected final <T> T bind(ConfigurationPropertyName name, Bindable<T> target,
BindHandler handler, Context context) { BindHandler handler, Context context) {
context.clearConfigurationProperty();
try { try {
if (!handler.onStart(name, target, context)) { if (!handler.onStart(name, target, context)) {
return null; return null;
@ -433,6 +434,10 @@ public class Binder {
this.configurationProperty = configurationProperty; this.configurationProperty = configurationProperty;
} }
void clearConfigurationProperty() {
this.configurationProperty = null;
}
@Override @Override
public PlaceholdersResolver getPlaceholdersResolver() { public PlaceholdersResolver getPlaceholdersResolver() {
return Binder.this.placeholdersResolver; return Binder.this.placeholdersResolver;

View File

@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.assertj.core.matcher.AssertionMatcher;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -214,10 +215,31 @@ public class BinderTests {
assertThat(result.toString()).isEqualTo("2014-04-01"); assertThat(result.toString()).isEqualTo("2014-04-01");
} }
@Test
public void bindExceptionWhenBeanBindingFailsShouldHaveNullConfigurationProperty() throws Exception {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.value", "hello");
source.put("foo.items", "bar,baz");
this.sources
.add(source);
Bindable<JavaBean> target = Bindable.of(JavaBean.class);
this.thrown.expect(BindException.class);
this.thrown.expect(new AssertionMatcher<BindException>() {
@Override
public void assertion(BindException ex) throws AssertionError {
assertThat(ex.getCause().getMessage()).isEqualTo("No setter found for property: items");
assertThat(ex.getProperty()).isNull();
}
});
this.binder.bind("foo", target);
}
public static class JavaBean { public static class JavaBean {
private String value; private String value;
private List<String> items = Collections.emptyList();
public String getValue() { public String getValue() {
return this.value; return this.value;
} }
@ -226,6 +248,9 @@ public class BinderTests {
this.value = value; this.value = value;
} }
public List<String> getItems() {
return this.items;
}
} }
public enum ExampleEnum { public enum ExampleEnum {