Skip to content

Commit 4bd8a3c

Browse files
committed
DATACMNS-1199 - Added PropertyPath.nested(…).
This allows to obtain a nested property path based on a currently available one.
1 parent b3e1601 commit 4bd8a3c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/main/java/org/springframework/data/mapping/PropertyPath.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ public boolean isCollection() {
195195
return isCollection;
196196
}
197197

198+
/**
199+
* Returns the {@link PropertyPath} for the path nested under the current property.
200+
*
201+
* @param path must not be {@literal null} or empty.
202+
* @return will never be {@literal null}.
203+
*/
204+
public PropertyPath nested(String path) {
205+
206+
Assert.hasText(path, "Path must not be null or empty!");
207+
208+
String lookup = toDotPath().concat(".").concat(path);
209+
210+
return PropertyPath.from(lookup, owningType);
211+
}
212+
198213
/*
199214
* (non-Javadoc)
200215
* @see java.lang.Iterable#iterator()

src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,20 @@ public void exposesLeafPropertyType() {
355355
assertThat(from("user.name", Bar.class).getLeafType()).isEqualTo(String.class);
356356
}
357357

358+
@Test // DATACMNS-1199
359+
public void createsNestedPropertyPath() {
360+
assertThat(from("user", Bar.class).nested("name")).isEqualTo(from("user.name", Bar.class));
361+
}
362+
363+
@Test // DATACMNS-1199
364+
public void rejectsNonExistantNestedPath() {
365+
366+
assertThatExceptionOfType(PropertyReferenceException.class) //
367+
.isThrownBy(() -> from("user", Bar.class).nested("nonexistant")) //
368+
.withMessageContaining("nonexistant") //
369+
.withMessageContaining("Bar.user");
370+
}
371+
358372
private class Foo {
359373

360374
String userName;

0 commit comments

Comments
 (0)