Skip to content

Commit 42997ac

Browse files
committed
Fix constructor resolution for Kotlin classes using unsigned types.
We now leniently skip parameter name resolution for types using unsigned types. The issue is caused by Kotlin's DefaultConstructorMarker that doesn't report a parameter name. Closes #2215
1 parent 6970077 commit 42997ac

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private static <T, P extends PersistentProperty<P>> PreferredConstructor<T, P> b
212212

213213
for (int i = 0; i < parameterTypes.size(); i++) {
214214

215-
String name = parameterNames == null ? null : parameterNames[i];
215+
String name = parameterNames == null || parameterNames.length <= i ? null : parameterNames[i];
216216
TypeInformation<?> type = parameterTypes.get(i);
217217
Annotation[] annotations = parameterAnnotations[i];
218218

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

+32-4
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,27 @@ class PreferredConstructorDiscovererUnitTests {
7979
@Suppress("UNCHECKED_CAST")
8080
fun `should not resolve constructor for synthetic Kotlin class`() {
8181

82-
val c = Class.forName("org.springframework.data.mapping.model.TypeCreatingSyntheticClassKt") as Class<Any>
82+
val c =
83+
Class.forName("org.springframework.data.mapping.model.TypeCreatingSyntheticClassKt") as Class<Any>
8384

84-
val constructor = PreferredConstructorDiscoverer.discover<Any, SamplePersistentProperty>(c)
85+
val constructor =
86+
PreferredConstructorDiscoverer.discover<Any, SamplePersistentProperty>(c)
8587

8688
assertThat(constructor).isNull()
8789
}
8890

91+
@Test // DATACMNS-1800, gh-2215
92+
@ExperimentalUnsignedTypes
93+
fun `should discover constructor for class using unsigned types`() {
94+
95+
val constructor =
96+
PreferredConstructorDiscoverer.discover<UnsignedTypesEntity, SamplePersistentProperty>(
97+
UnsignedTypesEntity::class.java
98+
)
99+
100+
assertThat(constructor).isNotNull()
101+
}
102+
89103
data class Simple(val firstname: String)
90104

91105
class TwoConstructorsWithoutDefault {
@@ -111,9 +125,23 @@ class PreferredConstructorDiscovererUnitTests {
111125

112126
class DefaultConstructor(val firstname: String = "foo")
113127

114-
class TwoDefaultConstructorsAnnotated(val firstname: String = "foo", val lastname: String = "bar") {
128+
class TwoDefaultConstructorsAnnotated(
129+
val firstname: String = "foo",
130+
val lastname: String = "bar"
131+
) {
115132

116133
@PersistenceConstructor
117-
constructor(firstname: String = "foo", lastname: String = "bar", age: Int) : this(firstname, lastname)
134+
constructor(firstname: String = "foo", lastname: String = "bar", age: Int) : this(
135+
firstname,
136+
lastname
137+
)
118138
}
139+
140+
@ExperimentalUnsignedTypes
141+
data class UnsignedTypesEntity(
142+
val id: String,
143+
val a: UInt = 5u,
144+
val b: Int = 5,
145+
val c: Double = 1.5
146+
)
119147
}

0 commit comments

Comments
 (0)