Fix Invalid target for Validator error
Update `PropertiesConfigurationFactory` so that the validator is only set when it supports the target object. Fixes gh-8149
This commit is contained in:
parent
dc75f13754
commit
87b8ce61de
|
|
@ -253,7 +253,8 @@ public class PropertiesConfigurationFactory<T>
|
|||
RelaxedDataBinder dataBinder = (this.targetName != null
|
||||
? new RelaxedDataBinder(this.target, this.targetName)
|
||||
: new RelaxedDataBinder(this.target));
|
||||
if (this.validator != null) {
|
||||
if (this.validator != null
|
||||
&& this.validator.supports(dataBinder.getTarget().getClass())) {
|
||||
dataBinder.setValidator(this.validator);
|
||||
}
|
||||
if (this.conversionService != null) {
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.boot.context.properties;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
|
@ -47,6 +48,7 @@ import org.springframework.validation.Errors;
|
|||
import org.springframework.validation.ValidationUtils;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
|
@ -330,6 +332,17 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
|
|||
"Multiple PropertySourcesPlaceholderConfigurer beans registered");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertiesWithMap() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
|
||||
"test.map.foo=bar");
|
||||
this.context.register(PropertiesWithMap.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(PropertiesWithMap.class).getMap())
|
||||
.containsEntry("foo", "bar");
|
||||
}
|
||||
|
||||
private void assertBindingFailure(int errorCount) {
|
||||
try {
|
||||
this.context.refresh();
|
||||
|
|
@ -608,6 +621,28 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
|
|||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
@ConfigurationProperties(prefix = "test")
|
||||
public static class PropertiesWithMap {
|
||||
|
||||
@Bean
|
||||
public Validator validator() {
|
||||
return new LocalValidatorFactoryBean();
|
||||
}
|
||||
|
||||
private Map<String, String> map;
|
||||
|
||||
public Map<String, String> getMap() {
|
||||
return this.map;
|
||||
}
|
||||
|
||||
public void setMap(Map<String, String> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
public static class ConfigurationPropertiesWithFactoryBean {
|
||||
|
|
|
|||
Loading…
Reference in New Issue