Skip to content

Commit 76538ae

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1121 - Fix false positive when checking for potential cycles.
We now only check for cycles on entity types and explicitly exclude simple types. Original pull request: spring-projects#267.
1 parent 1181a21 commit 76538ae

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -391,8 +391,9 @@ void protect(MongoPersistentProperty property, String path) throws CyclicPropert
391391

392392
for (Path existingPath : paths) {
393393

394-
if (existingPath.cycles(property, path)) {
394+
if (existingPath.cycles(property, path) && property.isEntity()) {
395395
paths.add(new Path(property, path));
396+
396397
throw new CyclicPropertyReferenceException(property.getFieldName(), property.getOwner().getType(),
397398
existingPath.getPath());
398399
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -656,6 +656,21 @@ public void shouldAllowMultiplePropertiesOfSameTypeWithMatchingStartLettersOnNes
656656
assertThat((String) indexDefinitions.get(1).getIndexOptions().get("name"), equalTo("component.name"));
657657
}
658658

659+
/**
660+
* @see DATAMONGO-1121
661+
*/
662+
@Test
663+
public void shouldOnlyConsiderEntitiesAsPotentialCycleCandidates() {
664+
665+
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(OuterDocumentReferingToIndexedPropertyViaDifferentNonCyclingPaths.class);
666+
667+
assertThat(indexDefinitions, hasSize(2));
668+
assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("path1.foo"));
669+
assertThat((String) indexDefinitions.get(1).getIndexOptions().get("name"),
670+
equalTo("path2.propertyWithIndexedStructure.foo"));
671+
672+
}
673+
659674
@Document
660675
static class MixedIndexRoot {
661676

@@ -820,6 +835,17 @@ public class NameComponent {
820835
NameComponent component;
821836
}
822837

838+
@Document
839+
public static class OuterDocumentReferingToIndexedPropertyViaDifferentNonCyclingPaths {
840+
841+
NoCycleButIndenticallNamedPropertiesDeeplyNested path1;
842+
AlternatePathToNoCycleButIndenticallNamedPropertiesDeeplyNestedDocument path2;
843+
}
844+
845+
public static class AlternatePathToNoCycleButIndenticallNamedPropertiesDeeplyNestedDocument {
846+
NoCycleButIndenticallNamedPropertiesDeeplyNested propertyWithIndexedStructure;
847+
}
848+
823849
}
824850

825851
private static List<IndexDefinitionHolder> prepareMappingContextAndResolveIndexForType(Class<?> type) {

0 commit comments

Comments
 (0)