Skip to content

Commit eb1ad95

Browse files
edwardmpmp911de
authored andcommitted
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>
1 parent 22eab8a commit eb1ad95

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import java.time.LocalDate
2020

2121
/**
2222
* @author Mark Paluch
23+
* @author Edward Poot
2324
*/
2425
@JvmInline
2526
value class MyValueClass(val id: String)
@@ -46,6 +47,10 @@ data class WithMyValueClass(val id: MyValueClass) {
4647
// ---------
4748
}
4849

50+
data class WithMyValueClassPrivateConstructor private constructor(val id: MyValueClass)
51+
52+
data class WithMyValueClassPrivateConstructorAndDefaultValue private constructor(val id: MyValueClass = MyValueClass("id"))
53+
4954
@JvmInline
5055
value class MyNullableValueClass(val id: String? = "id")
5156

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import kotlin.reflect.KClass
3333
*
3434
* @author Mark Paluch
3535
* @author Sebastien Deleuze
36+
* @author Edward Poot
3637
*/
3738
@Suppress("UNCHECKED_CAST")
3839
class KotlinClassGeneratingEntityInstantiatorUnitTests {
@@ -192,6 +193,24 @@ class KotlinClassGeneratingEntityInstantiatorUnitTests {
192193
assertThat(instance.id.id).isEqualTo("hello")
193194
}
194195

196+
@Test // GH-3389
197+
fun `should use private default constructor for types using value class`() {
198+
199+
every { provider.getParameterValue<String>(any()) } returns "hello"
200+
val instance = construct(WithMyValueClassPrivateConstructor::class)
201+
202+
assertThat(instance.id.id).isEqualTo("hello")
203+
}
204+
205+
@Test // GH-3389
206+
fun `should use private default constructor for types using value class with default value`() {
207+
208+
every { provider.getParameterValue<String>(any()) } returns "hello"
209+
val instance = construct(WithMyValueClassPrivateConstructorAndDefaultValue::class)
210+
211+
assertThat(instance.id.id).isEqualTo("hello")
212+
}
213+
195214
@Test
196215
fun `should use default constructor for types using nullable value class`() {
197216

0 commit comments

Comments
 (0)