Skip to content

Commit 9872f8c

Browse files
ydemartinomp911de
authored andcommitted
DATAMONGO-2661 - Handle nullable types for KPropertyPath.
Original pull request: #894.
1 parent 1b1ab2c commit 9872f8c

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
@@ -25,7 +25,7 @@ import kotlin.reflect.KProperty1
2525
* @since 2.2
2626
*/
2727
class KPropertyPath<T, U>(
28-
internal val parent: KProperty<U>,
28+
internal val parent: KProperty<U?>,
2929
internal val child: KProperty1<U, T>
3030
) : KProperty<T> by child
3131

@@ -50,7 +50,8 @@ internal fun asString(property: KProperty<*>): String {
5050
* Book::author / Author::name isEqualTo "Herman Melville"
5151
* ```
5252
* @author Tjeu Kayim
53+
* @author Yoann de Martino
5354
* @since 2.2
5455
*/
55-
operator fun <T, U> KProperty<T>.div(other: KProperty1<T, U>) =
56+
operator fun <T, U> KProperty<T?>.div(other: KProperty1<T, U>) =
5657
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
@@ -60,6 +60,15 @@ class KPropertyPathTests {
6060
assertThat(property).isEqualTo("entity.book.author.name")
6161
}
6262

63+
@Test
64+
fun `Convert nullable KProperty to field name`() {
65+
class Cat(val name: String)
66+
class Owner(val cat: Cat?)
67+
68+
val property = asString(Owner::cat / Cat::name)
69+
assertThat(property).isEqualTo("cat.name")
70+
}
71+
6372
class Book(val title: String, val author: Author)
6473
class Author(val name: String)
6574
}

0 commit comments

Comments
 (0)