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 d83463f43..daa008af7 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 @@ -20,6 +20,7 @@ import kotlin.reflect.KProperty1 /** * Abstraction of a property path consisting of [KProperty]. + * * @author Tjeu Kayim * @author Mark Paluch * @author Yoann de Martino @@ -28,11 +29,7 @@ import kotlin.reflect.KProperty1 class KPropertyPath( internal val parent: KProperty, internal val child: KProperty1 -) : KProperty by child { - override fun toString(): String { - return asString(this) - } -} +) : KProperty by child /** * Recursively construct field name for a nested property. diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/KPropertyPathExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/KPropertyPathExtensions.kt new file mode 100644 index 000000000..10a23bce6 --- /dev/null +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/KPropertyPathExtensions.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core.query + +import kotlin.reflect.KProperty + +/** + * Extension for [KProperty] providing an `toPath` function to render a [KProperty] as property path. + * + * @author Mark Paluch + * @since 3.1 + */ +fun KProperty<*>.toPath(): String = asString(this) 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 cb41e7b9c..8a13140ba 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 @@ -19,8 +19,11 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.Test /** + * Unit tests for [KPropertyPath] and its extensions. + * * @author Tjeu Kayim * @author Yoann de Martino + * @author Mark Paluch */ class KPropertyPathTests { @@ -62,21 +65,30 @@ class KPropertyPathTests { } @Test - fun `Convert nested KProperty to field name using toString()`() { + fun `Convert simple KProperty to property path using toPath`() { - val property = (Book::author / Author::name).toString() + class AnotherEntity(val entity: String) + + val property = (AnotherEntity::entity).toPath() + + assertThat(property).isEqualTo("entity") + } + + @Test + fun `Convert nested KProperty to field name using toPath()`() { + + val property = (Book::author / Author::name).toPath() assertThat(property).isEqualTo("author.name") } - @Test - fun `Convert triple nested KProperty to field name using toString()`() { + fun `Convert triple nested KProperty to property path using toPath`() { class Entity(val book: Book) class AnotherEntity(val entity: Entity) - val property = (AnotherEntity::entity / Entity::book / Book::author / Author::name).toString() + val property = (AnotherEntity::entity / Entity::book / Book::author / Author::name).toPath() assertThat(property).isEqualTo("entity.book.author.name") } diff --git a/src/main/asciidoc/new-features.adoc b/src/main/asciidoc/new-features.adoc index 7727e2839..071d56f32 100644 --- a/src/main/asciidoc/new-features.adoc +++ b/src/main/asciidoc/new-features.adoc @@ -7,6 +7,7 @@ * <> enabled through `@EnableReactiveMongoAuditing`. `@EnableMongoAuditing` no longer registers `ReactiveAuditingEntityCallback`. * Reactive SpEL support in `@Query` and `@Aggregation` query methods. * Aggregation hints via `AggregationOptions.builder().hint(bson).build()`. +* Extension Function `KProperty.asPath()` to render property references into a property path representation. [[new-features.3.0]] == What's New in Spring Data MongoDB 3.0