Skip to content

Commit 05d2ca4

Browse files
committed
Allow bean binding if property binding fails
Update `Binder` so that if a property exists, but it cannot be converted to required type, bean binding is attempted. Prior to this commit, if a user happened to have an environment variable named `SERVER` the binder would fail when trying to directly convert its `String` value into a `ServerProperties` Fixes gh-10945
1 parent 93ae71c commit 05d2ca4

File tree

2 files changed

+25
-1
lines changed
  • spring-boot-project/spring-boot/src
    • main/java/org/springframework/boot/context/properties/bind
    • test/java/org/springframework/boot/context/properties/bind

2 files changed

+25
-1
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
3939
import org.springframework.boot.context.properties.source.ConfigurationPropertyState;
4040
import org.springframework.core.convert.ConversionService;
41+
import org.springframework.core.convert.ConverterNotFoundException;
4142
import org.springframework.core.env.Environment;
4243
import org.springframework.format.support.DefaultFormattingConversionService;
4344
import org.springframework.util.Assert;
@@ -244,7 +245,18 @@ private <T> Object bindObject(ConfigurationPropertyName name, Bindable<T> target
244245
return bindAggregate(name, target, handler, context, aggregateBinder);
245246
}
246247
if (property != null) {
247-
return bindProperty(name, target, handler, context, property);
248+
try {
249+
return bindProperty(name, target, handler, context, property);
250+
}
251+
catch (ConverterNotFoundException ex) {
252+
// We might still be able to bind it as a bean
253+
Object bean = bindBean(name, target, handler, context,
254+
allowRecursiveBinding);
255+
if (bean != null) {
256+
return bean;
257+
}
258+
throw ex;
259+
}
248260
}
249261
return bindBean(name, target, handler, context, allowRecursiveBinding);
250262
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ public void bindToJavaBeanWhenNonIterableShouldReturnPopulatedBean()
190190
assertThat(result.getValue()).isEqualTo("bar");
191191
}
192192

193+
@Test
194+
public void bindToJavaBeanWhenHasPropertyWithSameNameShouldStillBind()
195+
throws Exception {
196+
// gh-10945
197+
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
198+
source.put("foo", "boom");
199+
source.put("foo.value", "bar");
200+
this.sources.add(source);
201+
JavaBean result = this.binder.bind("foo", Bindable.of(JavaBean.class)).get();
202+
assertThat(result.getValue()).isEqualTo("bar");
203+
}
204+
193205
@Test
194206
public void bindToJavaBeanShouldTriggerOnSuccess() throws Exception {
195207
this.sources

0 commit comments

Comments
 (0)