From 6043f6b74deb38689f7f1404e21175ccaf3fa6d8 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 10 Jun 2014 10:55:37 +0200 Subject: [PATCH] DATAMONGO-949 - CycleGuard should only match properties in word boundaries. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We modified the regular expression used for cycle detection to match on the exact property name within the inspected path using word boundaries. This fix prevents sub sequences of an existing property (like ‘sub’ would have matched ‘substr’) from being matched. Along the way we fixed the (false) assertion in one of the tests, as we create the +1 cycle reference index before actually breaking the operation. --- .../MongoPersistentEntityIndexResolver.java | 2 +- ...ersistentEntityIndexResolverUnitTests.java | 24 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java index 87a460343..09cf78a41 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java @@ -403,7 +403,7 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver { boolean cycles(MongoPersistentProperty property) { - Pattern pattern = Pattern.compile("\\p{Punct}?" + Pattern.quote(property.getFieldName()) + "(\\p{Punct}|\\w)?"); + Pattern pattern = Pattern.compile("\\b" + Pattern.quote(property.getFieldName()) + "\\b"); Matcher matcher = pattern.matcher(path); int count = 0; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java index f9e26afd0..691c172af 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java @@ -446,7 +446,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { public void shouldNotRunIntoStackOverflow() { List indexDefinitions = prepareMappingContextAndResolveIndexForType(CycleStartingInBetween.class); - assertThat(indexDefinitions, hasSize(1)); + assertThat(indexDefinitions, hasSize(2)); } /** @@ -488,6 +488,17 @@ public class MongoPersistentEntityIndexResolverUnitTests { assertThat(indexDefinitions, hasSize(3)); } + /** + * @see DATAMONGO-949 + */ + @Test + public void shouldNotDetectCycleInSimilarlyNamedProperties() { + + List indexDefinitions = prepareMappingContextAndResolveIndexForType(SimilarityHolingBean.class); + assertIndexPathAndCollection("norm", "similarityHolingBean", indexDefinitions.get(0)); + assertThat(indexDefinitions, hasSize(1)); + } + @Document static class MixedIndexRoot { @@ -554,6 +565,17 @@ public class MongoPersistentEntityIndexResolverUnitTests { @Indexed String foo; } + + @Document + static class SimilarityHolingBean { + + @Indexed @Field("norm") String normalProperty; + @Field("similarityL") private List listOfSimilarilyNamedEntities = null; + } + + static class SimilaritySibling { + @Field("similarity") private String similarThoughNotEqualNamedProperty; + } } private static List prepareMappingContextAndResolveIndexForType(Class type) {