Clear configurationProperty from context before bind
Fixes gh-9295
This commit is contained in:
		
							parent
							
								
									fed8c8a681
								
							
						
					
					
						commit
						2009da6169
					
				| 
						 | 
				
			
			@ -189,6 +189,7 @@ public class Binder {
 | 
			
		|||
 | 
			
		||||
	protected final <T> T bind(ConfigurationPropertyName name, Bindable<T> target,
 | 
			
		||||
			BindHandler handler, Context context) {
 | 
			
		||||
		context.clearConfigurationProperty();
 | 
			
		||||
		try {
 | 
			
		||||
			if (!handler.onStart(name, target, context)) {
 | 
			
		||||
				return null;
 | 
			
		||||
| 
						 | 
				
			
			@ -433,6 +434,10 @@ public class Binder {
 | 
			
		|||
			this.configurationProperty = configurationProperty;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		void clearConfigurationProperty() {
 | 
			
		||||
			this.configurationProperty = null;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		@Override
 | 
			
		||||
		public PlaceholdersResolver getPlaceholdersResolver() {
 | 
			
		||||
			return Binder.this.placeholdersResolver;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,6 +21,7 @@ import java.util.ArrayList;
 | 
			
		|||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import org.assertj.core.matcher.AssertionMatcher;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Rule;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
| 
						 | 
				
			
			@ -214,10 +215,31 @@ public class BinderTests {
 | 
			
		|||
		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 {
 | 
			
		||||
 | 
			
		||||
		private String value;
 | 
			
		||||
 | 
			
		||||
		private List<String> items = Collections.emptyList();
 | 
			
		||||
 | 
			
		||||
		public String getValue() {
 | 
			
		||||
			return this.value;
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -226,6 +248,9 @@ public class BinderTests {
 | 
			
		|||
			this.value = value;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public List<String> getItems() {
 | 
			
		||||
			return this.items;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public enum ExampleEnum {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue