Skip to content

Commit 9a1d443

Browse files
committed
Omit asymmetric Kotlin properties.
We now skip adding asymmetric Kotlin properties if the getter returns a different type than the setter (e.g. due to value boxing). Closes #2993
1 parent 3ff6d9e commit 9a1d443

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java

+7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException {
6868
Method getter = ReflectJvmMapping.getJavaGetter(property);
6969
Method setter = property instanceof KMutableProperty<?> kmp ? ReflectJvmMapping.getJavaSetter(kmp) : null;
7070

71+
if (getter != null && setter != null && setter.getParameterCount() == 1) {
72+
if (!getter.getReturnType().equals(setter.getParameters()[0].getType())) {
73+
// filter asymmetric getters/setters from being considered a Java Beans property
74+
continue;
75+
}
76+
}
77+
7178
pds.add(new PropertyDescriptor(property.getName(), getter, setter));
7279
}
7380
}

src/test/kotlin/org/springframework/data/util/KotlinBeanInfoFactoryUnitTests.kt

+18
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ class KotlinBeanInfoFactoryUnitTests {
9292
assertThat(pds).extracting("name").contains("myQueryLookupStrategyKey", "repositoryBaseClass")
9393
}
9494

95+
@Test // GH-2993
96+
internal fun skipsAsymmetricGettersAndSetters() {
97+
98+
val pds = BeanUtils.getPropertyDescriptors(MyEntity::class.java)
99+
100+
assertThat(pds).hasSize(1)
101+
assertThat(pds[0].writeMethod).isNull()
102+
assertThat(pds[0].readMethod).isNotNull()
103+
}
104+
95105
data class SimpleDataClass(val id: String, var name: String)
96106

97107
@JvmInline
@@ -123,4 +133,12 @@ class KotlinBeanInfoFactoryUnitTests {
123133
}
124134
}
125135

136+
interface Interval<T> {
137+
val end: T
138+
}
139+
140+
class MyEntity : Interval<Long> {
141+
override var end: Long = -1L
142+
protected set
143+
}
126144
}

0 commit comments

Comments
 (0)