Skip to content

Commit e4b0575

Browse files
Do not consider bridge methods when binding java bean properties
Fixes gh-28917
1 parent 34f9651 commit e4b0575

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ protected void addProperties(Method[] declaredMethods, Field[] declaredFields) {
165165
private boolean isCandidate(Method method) {
166166
int modifiers = method.getModifiers();
167167
return !Modifier.isPrivate(modifiers) && !Modifier.isProtected(modifiers) && !Modifier.isAbstract(modifiers)
168-
&& !Modifier.isStatic(modifiers) && !Object.class.equals(method.getDeclaringClass())
168+
&& !Modifier.isStatic(modifiers) && !method.isBridge()
169+
&& !Object.class.equals(method.getDeclaringClass())
169170
&& !Class.class.equals(method.getDeclaringClass()) && method.getName().indexOf('$') == -1;
170171
}
171172

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,18 @@ void bindToInstanceWhenNoNestedShouldLeaveNestedAsNull() {
318318
assertThat(bean.getValueBean()).isNull();
319319
}
320320

321+
@Test
322+
void bindToClassWithOverriddenPropertyShouldSetSubclassProperty() {
323+
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
324+
source.put("foo.value-bean.int-value", "123");
325+
source.put("foo.value-bean.sub-int-value", "456");
326+
this.sources.add(source);
327+
ExampleNestedSubclassBean bean = this.binder.bind("foo", Bindable.of(ExampleNestedSubclassBean.class)).get();
328+
assertThat(bean.getValueBean()).isNotNull();
329+
assertThat(bean.getValueBean().getIntValue()).isEqualTo(123);
330+
assertThat(bean.getValueBean().getSubIntValue()).isEqualTo(456);
331+
}
332+
321333
@Test
322334
void bindToClassWhenPropertiesMissingShouldReturnUnbound() {
323335
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
@@ -804,6 +816,35 @@ String getFoo() {
804816

805817
}
806818

819+
static class ExampleNestedSubclassBean extends ExampleNestedBean {
820+
821+
private ExampleValueSubclassBean valueBean;
822+
823+
@Override
824+
ExampleValueSubclassBean getValueBean() {
825+
return this.valueBean;
826+
}
827+
828+
void setValueBean(ExampleValueSubclassBean valueBean) {
829+
this.valueBean = valueBean;
830+
}
831+
832+
static class ExampleValueSubclassBean extends ExampleValueBean {
833+
834+
private int subIntValue;
835+
836+
int getSubIntValue() {
837+
return this.subIntValue;
838+
}
839+
840+
void setSubIntValue(int intValue) {
841+
this.subIntValue = intValue;
842+
}
843+
844+
}
845+
846+
}
847+
807848
static class ExampleWithNonDefaultConstructor {
808849

809850
private String value;

0 commit comments

Comments
 (0)