Browse Source

Fix Kotlin copy method assignability check.

We now resolve only the raw class when checking if a primary constructor argument is assignable to method parameters of the synthetic copy method.
Previously we used ResolvableType's assignability check which considered generic type arguments. As the type resolution between the KType and copy method type is not symmetric, the check only succeeded in cases where both types could be resolved to the same type/assignable type. Using projections or Any caused asymmetric resolution and therefor the assignability check returned non-assignable.

Closes #2336.
pull/2339/head
Mark Paluch 5 years ago
parent
commit
1eeef23fd9
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 3
      src/main/java/org/springframework/data/mapping/model/KotlinCopyMethod.java
  2. 7
      src/test/java/org/springframework/data/mapping/model/KotlinCopyMethodUnitTests.java
  3. 9
      src/test/kotlin/org/springframework/data/mapping/model/DataClasses.kt

3
src/main/java/org/springframework/data/mapping/model/KotlinCopyMethod.java

@ -278,7 +278,8 @@ class KotlinCopyMethod { @@ -278,7 +278,8 @@ class KotlinCopyMethod {
Type parameterType = ReflectJvmMapping.getJavaType(source);
return ResolvableType.forClass(target).isAssignableFrom(ResolvableType.forType(parameterType));
Class<?> rawClass = ResolvableType.forType(parameterType).getRawClass();
return rawClass == null || target.isAssignableFrom(rawClass);
}
/**

7
src/test/java/org/springframework/data/mapping/model/KotlinCopyMethodUnitTests.java

@ -73,11 +73,10 @@ class KotlinCopyMethodUnitTests { @@ -73,11 +73,10 @@ class KotlinCopyMethodUnitTests {
.isTrue();
}
@Test // #2324
@Test // #2324, #2336
void shouldDetermineCopyMethodForParametrizedType() {
Optional<KotlinCopyMethod> copyMethod = KotlinCopyMethod.findCopyMethod(ImmutableKotlinPerson.class);
assertThat(copyMethod).isPresent();
assertThat(KotlinCopyMethod.findCopyMethod(ImmutableKotlinPerson.class)).isPresent();
assertThat(KotlinCopyMethod.findCopyMethod(DataClassWithParametrizedCollections.class)).isPresent();
}
}

9
src/test/kotlin/org/springframework/data/mapping/model/DataClasses.kt

@ -65,3 +65,12 @@ data class ImmutableKotlinPerson( @@ -65,3 +65,12 @@ data class ImmutableKotlinPerson(
@Id val name: String,
val wasOnboardedBy: List<ImmutableKotlinPerson>
)
data class DataClassWithParametrizedCollections<T>(
val id: String? = null,
val flags: Map<out String, Any>,
val stringStringFlags: Map<in String, String>,
val parametrized: List<T>,
val anyList: List<*>
)

Loading…
Cancel
Save