Skip to content

Commit b426bdd

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 3349454 commit b426bdd

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
@@ -465,7 +465,7 @@ boolean cycles(MongoPersistentProperty property, String path) {
465465
return false;
466466
}
467467

468-
return path.contains(this.path);
468+
return path.equals(this.path) || path.contains(this.path + ".") || path.contains("." + this.path);
469469
}
470470
}
471471
}

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
@@ -630,6 +630,32 @@ public void shouldUseIndexNameOnRootLevel() {
630630
assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("property_index"));
631631
}
632632

633+
/**
634+
* @see DATAMONGO-1087
635+
*/
636+
@Test
637+
public void shouldAllowMultiplePropertiesOfSameTypeWithMatchingStartLettersOnRoot() {
638+
639+
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(MultiplePropertiesOfSameTypeWithMatchingStartLetters.class);
640+
641+
assertThat(indexDefinitions, hasSize(2));
642+
assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("name.component"));
643+
assertThat((String) indexDefinitions.get(1).getIndexOptions().get("name"), equalTo("nameLast.component"));
644+
}
645+
646+
/**
647+
* @see DATAMONGO-1087
648+
*/
649+
@Test
650+
public void shouldAllowMultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty() {
651+
652+
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(MultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty.class);
653+
654+
assertThat(indexDefinitions, hasSize(2));
655+
assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("component.nameLast"));
656+
assertThat((String) indexDefinitions.get(1).getIndexOptions().get("name"), equalTo("component.name"));
657+
}
658+
633659
@Document
634660
static class MixedIndexRoot {
635661

@@ -770,6 +796,30 @@ static class DocumentWithNestedTypeHavingNamedIndex {
770796
TypeWithNamedIndex propertyOfTypeHavingNamedIndex;
771797
}
772798

799+
@Document
800+
public class MultiplePropertiesOfSameTypeWithMatchingStartLetters {
801+
802+
public class NameComponent {
803+
804+
@Indexed String component;
805+
}
806+
807+
NameComponent name;
808+
NameComponent nameLast;
809+
}
810+
811+
@Document
812+
public class MultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty {
813+
814+
public class NameComponent {
815+
816+
@Indexed String nameLast;
817+
@Indexed String name;
818+
}
819+
820+
NameComponent component;
821+
}
822+
773823
}
774824

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

0 commit comments

Comments
 (0)