From cc1aa713f97330e3d8158f089557b797d4d63c32 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 13 Aug 2015 12:37:30 +0200 Subject: [PATCH] DATACMNS-750 - Fixed part creation in query methods that have keywords in property paths. If a keyword was contained in a complex property path (like In in SomeInfo), the property path was invalidly cut of in the middle. We now simply use the length of the keyword to extract the actual property from the candidate. This should be faster anyway as it doesn't require another lookup of the pattern in the source candidate. Related pull request: #136. --- .../data/repository/query/parser/Part.java | 2 +- .../query/parser/PartTreeUnitTests.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/repository/query/parser/Part.java b/src/main/java/org/springframework/data/repository/query/parser/Part.java index 3aca50aa4..f90b468fb 100644 --- a/src/main/java/org/springframework/data/repository/query/parser/Part.java +++ b/src/main/java/org/springframework/data/repository/query/parser/Part.java @@ -298,7 +298,7 @@ public class Part { for (String keyword : keywords) { if (candidate.endsWith(keyword)) { - return candidate.substring(0, candidate.indexOf(keyword)); + return candidate.substring(0, candidate.length() - keyword.length()); } } diff --git a/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java b/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java index e08522763..f4f17dbaa 100644 --- a/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java +++ b/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java @@ -27,6 +27,7 @@ import java.util.Date; import java.util.Iterator; import java.util.List; +import org.hamcrest.Matchers; import org.junit.Test; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; @@ -670,6 +671,24 @@ public class PartTreeUnitTests { assertPart(tree, new Part[] { new Part("legalNameNotContaining", Organization.class) }); } + /** + * @see DATACMNS-750 + */ + @Test + public void doesNotFailOnPropertiesContainingAKeyword() { + + PartTree partTree = new PartTree("findBySomeInfoIn", Category.class); + + Iterable parts = partTree.getParts(); + + assertThat(parts, is(Matchers. iterableWithSize(1))); + + Part part = parts.iterator().next(); + + assertThat(part.getType(), is(Type.IN)); + assertThat(part.getProperty(), is(PropertyPath.from("someInfo", Category.class))); + } + private static void assertLimiting(String methodName, Class entityType, boolean limiting, Integer maxResults) { assertLimiting(methodName, entityType, limiting, maxResults, false); } @@ -791,6 +810,8 @@ public class PartTreeUnitTests { interface Category { Long getId(); + + String getSomeInfo(); } interface Order {