Browse Source

Add test cases for Kotlin private constructor instantiation edge cases.

Add two test cases to demonstrate a bug in Kotlin constructor resolution when dealing with private constructors.

Closes #3389
Original pull request: #3390

Signed-off-by: Edward Poot <edwardmp@gmail.com>
3.5.x
Edward Poot 2 months ago committed by Mark Paluch
parent
commit
eb1ad95f2b
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 5
      src/test/kotlin/org/springframework/data/mapping/model/InlineClasses.kt
  2. 19
      src/test/kotlin/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiatorUnitTests.kt

5
src/test/kotlin/org/springframework/data/mapping/model/InlineClasses.kt

@ -20,6 +20,7 @@ import java.time.LocalDate @@ -20,6 +20,7 @@ import java.time.LocalDate
/**
* @author Mark Paluch
* @author Edward Poot
*/
@JvmInline
value class MyValueClass(val id: String)
@ -46,6 +47,10 @@ data class WithMyValueClass(val id: MyValueClass) { @@ -46,6 +47,10 @@ data class WithMyValueClass(val id: MyValueClass) {
// ---------
}
data class WithMyValueClassPrivateConstructor private constructor(val id: MyValueClass)
data class WithMyValueClassPrivateConstructorAndDefaultValue private constructor(val id: MyValueClass = MyValueClass("id"))
@JvmInline
value class MyNullableValueClass(val id: String? = "id")

19
src/test/kotlin/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiatorUnitTests.kt

@ -33,6 +33,7 @@ import kotlin.reflect.KClass @@ -33,6 +33,7 @@ import kotlin.reflect.KClass
*
* @author Mark Paluch
* @author Sebastien Deleuze
* @author Edward Poot
*/
@Suppress("UNCHECKED_CAST")
class KotlinClassGeneratingEntityInstantiatorUnitTests {
@ -192,6 +193,24 @@ class KotlinClassGeneratingEntityInstantiatorUnitTests { @@ -192,6 +193,24 @@ class KotlinClassGeneratingEntityInstantiatorUnitTests {
assertThat(instance.id.id).isEqualTo("hello")
}
@Test // GH-3389
fun `should use private default constructor for types using value class`() {
every { provider.getParameterValue<String>(any()) } returns "hello"
val instance = construct(WithMyValueClassPrivateConstructor::class)
assertThat(instance.id.id).isEqualTo("hello")
}
@Test // GH-3389
fun `should use private default constructor for types using value class with default value`() {
every { provider.getParameterValue<String>(any()) } returns "hello"
val instance = construct(WithMyValueClassPrivateConstructorAndDefaultValue::class)
assertThat(instance.id.id).isEqualTo("hello")
}
@Test
fun `should use default constructor for types using nullable value class`() {

Loading…
Cancel
Save