From c8b64601db1d19c3039c0a7cd9472ec1b0824ac0 Mon Sep 17 00:00:00 2001 From: Yoann de Martino Date: Tue, 24 Nov 2020 10:39:46 +0100 Subject: [PATCH] DATAMONGO-2661 - Handle nullable types for KPropertyPath. Original pull request: #894. --- .../data/mongodb/core/query/KPropertyPath.kt | 5 +++-- .../data/mongodb/core/query/KPropertyPathTests.kt | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/KPropertyPath.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/KPropertyPath.kt index daa008af7..d88eae4e0 100644 --- a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/KPropertyPath.kt +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/KPropertyPath.kt @@ -27,7 +27,7 @@ import kotlin.reflect.KProperty1 * @since 2.2 */ class KPropertyPath( - internal val parent: KProperty, + internal val parent: KProperty, internal val child: KProperty1 ) : KProperty by child @@ -52,7 +52,8 @@ internal fun asString(property: KProperty<*>): String { * Book::author / Author::name isEqualTo "Herman Melville" * ``` * @author Tjeu Kayim + * @author Yoann de Martino * @since 2.2 */ -operator fun KProperty.div(other: KProperty1) = +operator fun KProperty.div(other: KProperty1) = KPropertyPath(this, other) diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/KPropertyPathTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/KPropertyPathTests.kt index 8a13140ba..985064183 100644 --- a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/KPropertyPathTests.kt +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/KPropertyPathTests.kt @@ -93,6 +93,15 @@ class KPropertyPathTests { assertThat(property).isEqualTo("entity.book.author.name") } + @Test + fun `Convert nullable KProperty to field name`() { + class Cat(val name: String) + class Owner(val cat: Cat?) + + val property = asString(Owner::cat / Cat::name) + assertThat(property).isEqualTo("cat.name") + } + class Book(val title: String, val author: Author) class Author(val name: String) }