From 4bd8a3cfc3a242cd3d433937ece7a4905efce4c8 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 19 Oct 2017 12:40:53 +0200 Subject: [PATCH] =?UTF-8?q?DATACMNS-1199=20-=20Added=20PropertyPath.nested?= =?UTF-8?q?(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows to obtain a nested property path based on a currently available one. --- .../data/mapping/PropertyPath.java | 15 +++++++++++++++ .../data/mapping/PropertyPathUnitTests.java | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/main/java/org/springframework/data/mapping/PropertyPath.java b/src/main/java/org/springframework/data/mapping/PropertyPath.java index 7596792ed..f5e6f1e1f 100644 --- a/src/main/java/org/springframework/data/mapping/PropertyPath.java +++ b/src/main/java/org/springframework/data/mapping/PropertyPath.java @@ -195,6 +195,21 @@ public class PropertyPath implements Streamable { return isCollection; } + /** + * Returns the {@link PropertyPath} for the path nested under the current property. + * + * @param path must not be {@literal null} or empty. + * @return will never be {@literal null}. + */ + public PropertyPath nested(String path) { + + Assert.hasText(path, "Path must not be null or empty!"); + + String lookup = toDotPath().concat(".").concat(path); + + return PropertyPath.from(lookup, owningType); + } + /* * (non-Javadoc) * @see java.lang.Iterable#iterator() diff --git a/src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java b/src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java index c9653e288..71853d5e0 100755 --- a/src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java @@ -355,6 +355,20 @@ public class PropertyPathUnitTests { assertThat(from("user.name", Bar.class).getLeafType()).isEqualTo(String.class); } + @Test // DATACMNS-1199 + public void createsNestedPropertyPath() { + assertThat(from("user", Bar.class).nested("name")).isEqualTo(from("user.name", Bar.class)); + } + + @Test // DATACMNS-1199 + public void rejectsNonExistantNestedPath() { + + assertThatExceptionOfType(PropertyReferenceException.class) // + .isThrownBy(() -> from("user", Bar.class).nested("nonexistant")) // + .withMessageContaining("nonexistant") // + .withMessageContaining("Bar.user"); + } + private class Foo { String userName;