DATAMONGO-949 - CycleGuard should only match properties in word boundaries.

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.
This commit is contained in:
Christoph Strobl
2014-06-10 10:55:37 +02:00
committed by Oliver Gierke
parent 963a222616
commit 6115c9562b
2 changed files with 24 additions and 2 deletions

View File

@@ -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;

View File

@@ -446,7 +446,7 @@ public class MongoPersistentEntityIndexResolverUnitTests {
public void shouldNotRunIntoStackOverflow() {
List<IndexDefinitionHolder> 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<IndexDefinitionHolder> 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<SimilaritySibling> listOfSimilarilyNamedEntities = null;
}
static class SimilaritySibling {
@Field("similarity") private String similarThoughNotEqualNamedProperty;
}
}
private static List<IndexDefinitionHolder> prepareMappingContextAndResolveIndexForType(Class<?> type) {