Skip to content

Commit eca2108

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1087 - Fix index resolver detecting cycles for partial match.
We now check for presence of a dot path to verify that we’ve detected a cycle. Original pull request: #240.
1 parent dab6034 commit eca2108

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ boolean cycles(MongoPersistentProperty property, String path) {
540540
return false;
541541
}
542542

543-
return path.contains(this.path);
543+
return path.equals(this.path) || path.contains(this.path + ".") || path.contains("." + this.path);
544544
}
545545
}
546546
}

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,32 @@ public void shouldUseIndexNameOnRootLevel() {
812812
assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("property_index"));
813813
}
814814

815+
/**
816+
* @see DATAMONGO-1087
817+
*/
818+
@Test
819+
public void shouldAllowMultiplePropertiesOfSameTypeWithMatchingStartLettersOnRoot() {
820+
821+
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(MultiplePropertiesOfSameTypeWithMatchingStartLetters.class);
822+
823+
assertThat(indexDefinitions, hasSize(2));
824+
assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("name.component"));
825+
assertThat((String) indexDefinitions.get(1).getIndexOptions().get("name"), equalTo("nameLast.component"));
826+
}
827+
828+
/**
829+
* @see DATAMONGO-1087
830+
*/
831+
@Test
832+
public void shouldAllowMultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty() {
833+
834+
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(MultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty.class);
835+
836+
assertThat(indexDefinitions, hasSize(2));
837+
assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("component.nameLast"));
838+
assertThat((String) indexDefinitions.get(1).getIndexOptions().get("name"), equalTo("component.name"));
839+
}
840+
815841
@Document
816842
static class MixedIndexRoot {
817843

@@ -952,6 +978,30 @@ static class DocumentWithNestedTypeHavingNamedIndex {
952978
TypeWithNamedIndex propertyOfTypeHavingNamedIndex;
953979
}
954980

981+
@Document
982+
public class MultiplePropertiesOfSameTypeWithMatchingStartLetters {
983+
984+
public class NameComponent {
985+
986+
@Indexed String component;
987+
}
988+
989+
NameComponent name;
990+
NameComponent nameLast;
991+
}
992+
993+
@Document
994+
public class MultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty {
995+
996+
public class NameComponent {
997+
998+
@Indexed String nameLast;
999+
@Indexed String name;
1000+
}
1001+
1002+
NameComponent component;
1003+
}
1004+
9551005
}
9561006

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

0 commit comments

Comments
 (0)