#1601 - Properly create Property instances in PropertyUtils.

We now hand the already known property name (from the PropertyDescriptor) to the Property instance we create when inspecting classes for metadata. This avoids ambiguities stemming from the fact that Property assumes Java Bean style properties but Spring's BeanUtils also supporting Java Records style accessors. In special naming contexts like the one used in the test case, this can lead to properties considered "the same" as their accessor methods imply their name is the same.
This commit is contained in:
Oliver Drotbohm
2021-09-09 13:00:32 +02:00
parent 7b2079d83f
commit 9528d378dd
2 changed files with 22 additions and 1 deletions

View File

@@ -231,7 +231,7 @@ public class PropertyUtils {
return type == null //
? Stream.empty() //
: getPropertyDescriptors(type) //
.map(it -> new AnnotatedProperty(new Property(type, it.getReadMethod(), it.getWriteMethod())))
.map(it -> new AnnotatedProperty(new Property(type, it.getReadMethod(), it.getWriteMethod(), it.getName())))
.map(it -> JSR_303_PRESENT ? new Jsr303AwarePropertyMetadata(it) : new DefaultPropertyMetadata(it));
}

View File

@@ -164,6 +164,13 @@ class PropertyUtilsTest {
assertThat(metadata.getPropertyMetadata("firstname")).isPresent();
}
@Test // #1402
void detectesPropertiesWithRecordStyleAccessorsCorrectly() {
assertThatNoException()
.isThrownBy(() -> PropertyUtils.getExposedProperties(TypeWithRecordStyleAccessors.class));
}
@Data
@AllArgsConstructor
@JsonIgnoreProperties({ "ignoreThisProperty" })
@@ -236,4 +243,18 @@ class PropertyUtilsTest {
this.firstname = firstname;
}
}
// #1402
static class TypeWithRecordStyleAccessors {
private Boolean isActive;
public Boolean isActive() {
return isActive;
}
public void setActive(Boolean active) {
isActive = active;
}
}
}