Skip to content

Commit 87b8ce6

Browse files
committed
Fix Invalid target for Validator error
Update `PropertiesConfigurationFactory` so that the validator is only set when it supports the target object. Fixes gh-8149
1 parent dc75f13 commit 87b8ce6

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ private void doBindPropertiesToTarget() throws BindException {
253253
RelaxedDataBinder dataBinder = (this.targetName != null
254254
? new RelaxedDataBinder(this.target, this.targetName)
255255
: new RelaxedDataBinder(this.target));
256-
if (this.validator != null) {
256+
if (this.validator != null
257+
&& this.validator.supports(dataBinder.getTarget().getClass())) {
257258
dataBinder.setValidator(this.validator);
258259
}
259260
if (this.conversionService != null) {

spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.context.properties;
1818

1919
import java.util.List;
20+
import java.util.Map;
2021

2122
import javax.annotation.PostConstruct;
2223
import javax.validation.constraints.NotNull;
@@ -47,6 +48,7 @@
4748
import org.springframework.validation.ValidationUtils;
4849
import org.springframework.validation.Validator;
4950
import org.springframework.validation.annotation.Validated;
51+
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
5052

5153
import static org.assertj.core.api.Assertions.assertThat;
5254
import static org.junit.Assert.fail;
@@ -330,6 +332,17 @@ public void multiplePropertySourcesPlaceholderConfigurer() throws Exception {
330332
"Multiple PropertySourcesPlaceholderConfigurer beans registered");
331333
}
332334

335+
@Test
336+
public void propertiesWithMap() throws Exception {
337+
this.context = new AnnotationConfigApplicationContext();
338+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
339+
"test.map.foo=bar");
340+
this.context.register(PropertiesWithMap.class);
341+
this.context.refresh();
342+
assertThat(this.context.getBean(PropertiesWithMap.class).getMap())
343+
.containsEntry("foo", "bar");
344+
}
345+
333346
private void assertBindingFailure(int errorCount) {
334347
try {
335348
this.context.refresh();
@@ -608,6 +621,28 @@ public static PropertySourcesPlaceholderConfigurer configurer() {
608621

609622
}
610623

624+
@Configuration
625+
@EnableConfigurationProperties
626+
@ConfigurationProperties(prefix = "test")
627+
public static class PropertiesWithMap {
628+
629+
@Bean
630+
public Validator validator() {
631+
return new LocalValidatorFactoryBean();
632+
}
633+
634+
private Map<String, String> map;
635+
636+
public Map<String, String> getMap() {
637+
return this.map;
638+
}
639+
640+
public void setMap(Map<String, String> map) {
641+
this.map = map;
642+
}
643+
644+
}
645+
611646
@Configuration
612647
@EnableConfigurationProperties
613648
public static class ConfigurationPropertiesWithFactoryBean {

0 commit comments

Comments
 (0)