From b879ec8c0f1a5ce8ebc146ae41bd55e5b48d8ac1 Mon Sep 17 00:00:00 2001 From: Yoann de Martino Date: Wed, 5 Aug 2020 09:00:58 +0200 Subject: [PATCH] DATAMONGO-2596 - Introduce extension to render KProperty/KPropertyPath as property path. Original pull request: #880. --- .../data/mongodb/core/query/KPropertyPath.kt | 9 ++++++-- .../mongodb/core/query/KPropertyPathTests.kt | 21 +++++++++++++++++++ 2 files changed, 28 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 becfe9ca6..d83463f43 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 @@ -22,12 +22,17 @@ import kotlin.reflect.KProperty1 * Abstraction of a property path consisting of [KProperty]. * @author Tjeu Kayim * @author Mark Paluch + * @author Yoann de Martino * @since 2.2 */ class KPropertyPath( internal val parent: KProperty, internal val child: KProperty1 -) : KProperty by child +) : KProperty by child { + override fun toString(): String { + return asString(this) + } +} /** * Recursively construct field name for a nested property. @@ -45,7 +50,7 @@ internal fun asString(property: KProperty<*>): String { * Builds [KPropertyPath] from Property References. * Refer to a field in an embedded/nested document. * - * For example, referring to the field "book.author": + * For example, referring to the field "author.name": * ``` * Book::author / Author::name isEqualTo "Herman Melville" * ``` 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..cb41e7b9c 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 @@ -20,6 +20,7 @@ import org.junit.Test /** * @author Tjeu Kayim + * @author Yoann de Martino */ class KPropertyPathTests { @@ -60,6 +61,26 @@ class KPropertyPathTests { assertThat(property).isEqualTo("entity.book.author.name") } + @Test + fun `Convert nested KProperty to field name using toString()`() { + + val property = (Book::author / Author::name).toString() + + assertThat(property).isEqualTo("author.name") + } + + + @Test + fun `Convert triple nested KProperty to field name using toString()`() { + + class Entity(val book: Book) + class AnotherEntity(val entity: Entity) + + val property = (AnotherEntity::entity / Entity::book / Book::author / Author::name).toString() + + assertThat(property).isEqualTo("entity.book.author.name") + } + class Book(val title: String, val author: Author) class Author(val name: String) }