Skip to content

Commit 2a8ffd5

Browse files
ydemartinomp911de
authored andcommitted
DATAMONGO-2661 - Handle nullable types for KPropertyPath.
Original pull request: #894.
1 parent d54ed61 commit 2a8ffd5

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/KPropertyPath.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import kotlin.reflect.KProperty1
2727
* @since 2.2
2828
*/
2929
class KPropertyPath<T, U>(
30-
internal val parent: KProperty<U>,
30+
internal val parent: KProperty<U?>,
3131
internal val child: KProperty1<U, T>
3232
) : KProperty<T> by child
3333

@@ -52,7 +52,8 @@ internal fun asString(property: KProperty<*>): String {
5252
* Book::author / Author::name isEqualTo "Herman Melville"
5353
* ```
5454
* @author Tjeu Kayim
55+
* @author Yoann de Martino
5556
* @since 2.2
5657
*/
57-
operator fun <T, U> KProperty<T>.div(other: KProperty1<T, U>) =
58+
operator fun <T, U> KProperty<T?>.div(other: KProperty1<T, U>) =
5859
KPropertyPath(this, other)

spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/KPropertyPathTests.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ class KPropertyPathTests {
9393
assertThat(property).isEqualTo("entity.book.author.name")
9494
}
9595

96+
@Test
97+
fun `Convert nullable KProperty to field name`() {
98+
class Cat(val name: String)
99+
class Owner(val cat: Cat?)
100+
101+
val property = asString(Owner::cat / Cat::name)
102+
assertThat(property).isEqualTo("cat.name")
103+
}
104+
96105
class Book(val title: String, val author: Author)
97106
class Author(val name: String)
98107
}

0 commit comments

Comments
 (0)