Skip to content

Commit a99950d

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 0bf4ee2 commit a99950d

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

833+
/**
834+
* @see DATAMONGO-1087
835+
*/
836+
@Test
837+
public void shouldAllowMultiplePropertiesOfSameTypeWithMatchingStartLettersOnRoot() {
838+
839+
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(MultiplePropertiesOfSameTypeWithMatchingStartLetters.class);
840+
841+
assertThat(indexDefinitions, hasSize(2));
842+
assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("name.component"));
843+
assertThat((String) indexDefinitions.get(1).getIndexOptions().get("name"), equalTo("nameLast.component"));
844+
}
845+
846+
/**
847+
* @see DATAMONGO-1087
848+
*/
849+
@Test
850+
public void shouldAllowMultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty() {
851+
852+
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(MultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty.class);
853+
854+
assertThat(indexDefinitions, hasSize(2));
855+
assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("component.nameLast"));
856+
assertThat((String) indexDefinitions.get(1).getIndexOptions().get("name"), equalTo("component.name"));
857+
}
858+
833859
@Document
834860
static class MixedIndexRoot {
835861

@@ -970,6 +996,30 @@ static class DocumentWithNestedTypeHavingNamedIndex {
970996
TypeWithNamedIndex propertyOfTypeHavingNamedIndex;
971997
}
972998

999+
@Document
1000+
public class MultiplePropertiesOfSameTypeWithMatchingStartLetters {
1001+
1002+
public class NameComponent {
1003+
1004+
@Indexed String component;
1005+
}
1006+
1007+
NameComponent name;
1008+
NameComponent nameLast;
1009+
}
1010+
1011+
@Document
1012+
public class MultiplePropertiesOfSameTypeWithMatchingStartLettersOnNestedProperty {
1013+
1014+
public class NameComponent {
1015+
1016+
@Indexed String nameLast;
1017+
@Indexed String name;
1018+
}
1019+
1020+
NameComponent component;
1021+
}
1022+
9731023
}
9741024

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

0 commit comments

Comments
 (0)