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 becfe9ca6..e0ea0c778 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 @@ -25,7 +25,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 @@ -50,7 +50,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 03d68f724..48e75febb 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 @@ -60,6 +60,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) }