diff --git a/src/main/kotlin/org/springframework/data/core/KPropertyPath.kt b/src/main/kotlin/org/springframework/data/core/KPropertyPath.kt index 9d20d94353..83b67af3cf 100644 --- a/src/main/kotlin/org/springframework/data/core/KPropertyPath.kt +++ b/src/main/kotlin/org/springframework/data/core/KPropertyPath.kt @@ -17,6 +17,8 @@ package org.springframework.data.core +import org.springframework.data.mapping.KIterablePropertyPath as MappingKIterablePropertyPath +import org.springframework.data.mapping.KPropertyPath as MappingKPropertyPath import kotlin.reflect.KProperty import kotlin.reflect.KProperty1 @@ -101,6 +103,7 @@ internal class KIterablePropertyReference( * * @author Tjeu Kayim * @author Mikhail Polivakha + * @author hutiefang * @since 4.1 */ internal fun asString(property: KProperty<*>): String { @@ -109,8 +112,13 @@ internal fun asString(property: KProperty<*>): String { is KPropertyPath<*, *> -> "${asString(property.property)}.${property.leaf.name}" + is MappingKPropertyPath<*, *> -> + "${asString(property.parent)}.${property.child.name}" + + is MappingKIterablePropertyPath<*, *> -> + "${asString(property.parent)}.${property.child.name}" + else -> property.name } } - diff --git a/src/main/kotlin/org/springframework/data/mapping/KPropertyPath.kt b/src/main/kotlin/org/springframework/data/mapping/KPropertyPath.kt index c5d13b847b..6e6a6fb581 100644 --- a/src/main/kotlin/org/springframework/data/mapping/KPropertyPath.kt +++ b/src/main/kotlin/org/springframework/data/mapping/KPropertyPath.kt @@ -24,9 +24,10 @@ import kotlin.reflect.KProperty1 * @author Tjeu Kayim * @author Mark Paluch * @author Yoann de Martino + * @author hutiefang * @since 2.5 */ -private class KPropertyPath( +internal class KPropertyPath( val parent: KProperty, val child: KProperty1 ) : KProperty by child diff --git a/src/test/kotlin/org/springframework/data/mapping/KPropertyPathTests.kt b/src/test/kotlin/org/springframework/data/mapping/KPropertyPathTests.kt index 9b74d9afb2..c00114544f 100644 --- a/src/test/kotlin/org/springframework/data/mapping/KPropertyPathTests.kt +++ b/src/test/kotlin/org/springframework/data/mapping/KPropertyPathTests.kt @@ -17,6 +17,7 @@ package org.springframework.data.mapping import org.assertj.core.api.Assertions.assertThat import org.junit.Test +import org.springframework.data.core.toDotPath as coreToDotPath /** * Unit tests for [KPropertyPath] and its extensions. @@ -25,6 +26,7 @@ import org.junit.Test * @author Yoann de Martino * @author Mark Paluch * @author Mikhail Polivakha + * @author hutiefang */ class KPropertyPathTests { @@ -44,6 +46,14 @@ class KPropertyPathTests { assertThat(property).isEqualTo("author.name") } + @Test // GH-3503 + fun `Core toDotPath renders nested mapping KProperty`() { + + val property = (Book::author / Author::name).coreToDotPath() + + assertThat(property).isEqualTo("author.name") + } + @Test // GH-3010 fun `Convert from Iterable nested KProperty to field name`() { @@ -52,6 +62,22 @@ class KPropertyPathTests { assertThat(property).isEqualTo("books.title") } + @Test // GH-3503 + fun `Core toDotPath renders iterable mapping KProperty`() { + + val property = (Author::books / Book::title).coreToDotPath() + + assertThat(property).isEqualTo("books.title") + } + + @Test // GH-3503 + fun `Core toDotPath renders recursively nested mapping KProperty`() { + + val property = (Author::books / Book::author / Author::name).coreToDotPath() + + assertThat(property).isEqualTo("books.author.name") + } + @Test // GH-3010 fun `Convert from Iterable nested Iterable Property to field name`() {