Browse Source

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
pull/2996/head
Mark Paluch 2 years ago
parent
commit
1548ab6551
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 7
      src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java
  2. 18
      src/test/kotlin/org/springframework/data/util/KotlinBeanInfoFactoryUnitTests.kt

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

@ -68,6 +68,13 @@ public class KotlinBeanInfoFactory implements BeanInfoFactory, Ordered { @@ -68,6 +68,13 @@ public class KotlinBeanInfoFactory implements BeanInfoFactory, Ordered {
Method getter = ReflectJvmMapping.getJavaGetter(property);
Method setter = property instanceof KMutableProperty<?> kmp ? ReflectJvmMapping.getJavaSetter(kmp) : null;
if (getter != null && setter != null && setter.getParameterCount() == 1) {
if (!getter.getReturnType().equals(setter.getParameters()[0].getType())) {
// filter asymmetric getters/setters from being considered a Java Beans property
continue;
}
}
pds.add(new PropertyDescriptor(property.getName(), getter, setter));
}
}

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

@ -92,6 +92,16 @@ class KotlinBeanInfoFactoryUnitTests { @@ -92,6 +92,16 @@ class KotlinBeanInfoFactoryUnitTests {
assertThat(pds).extracting("name").contains("myQueryLookupStrategyKey", "repositoryBaseClass")
}
@Test // GH-2993
internal fun skipsAsymmetricGettersAndSetters() {
val pds = BeanUtils.getPropertyDescriptors(MyEntity::class.java)
assertThat(pds).hasSize(1)
assertThat(pds[0].writeMethod).isNull()
assertThat(pds[0].readMethod).isNotNull()
}
data class SimpleDataClass(val id: String, var name: String)
@JvmInline
@ -123,4 +133,12 @@ class KotlinBeanInfoFactoryUnitTests { @@ -123,4 +133,12 @@ class KotlinBeanInfoFactoryUnitTests {
}
}
interface Interval<T> {
val end: T
}
class MyEntity : Interval<Long> {
override var end: Long = -1L
protected set
}
}

Loading…
Cancel
Save