diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java index a7d4e920e..7b07e113a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -466,8 +466,9 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver { for (Path existingPath : paths) { - if (existingPath.cycles(property, path)) { + if (existingPath.cycles(property, path) && property.isEntity()) { paths.add(new Path(property, path)); + throw new CyclicPropertyReferenceException(property.getFieldName(), property.getOwner().getType(), existingPath.getPath()); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java index e7966e817..527cd59cf 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -838,6 +838,21 @@ public class MongoPersistentEntityIndexResolverUnitTests { assertThat((String) indexDefinitions.get(1).getIndexOptions().get("name"), equalTo("component.name")); } + /** + * @see DATAMONGO-1121 + */ + @Test + public void shouldOnlyConsiderEntitiesAsPotentialCycleCandidates() { + + List indexDefinitions = prepareMappingContextAndResolveIndexForType(OuterDocumentReferingToIndexedPropertyViaDifferentNonCyclingPaths.class); + + assertThat(indexDefinitions, hasSize(2)); + assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"), equalTo("path1.foo")); + assertThat((String) indexDefinitions.get(1).getIndexOptions().get("name"), + equalTo("path2.propertyWithIndexedStructure.foo")); + + } + @Document static class MixedIndexRoot { @@ -1002,6 +1017,17 @@ public class MongoPersistentEntityIndexResolverUnitTests { NameComponent component; } + @Document + public static class OuterDocumentReferingToIndexedPropertyViaDifferentNonCyclingPaths { + + NoCycleButIndenticallNamedPropertiesDeeplyNested path1; + AlternatePathToNoCycleButIndenticallNamedPropertiesDeeplyNestedDocument path2; + } + + public static class AlternatePathToNoCycleButIndenticallNamedPropertiesDeeplyNestedDocument { + NoCycleButIndenticallNamedPropertiesDeeplyNested propertyWithIndexedStructure; + } + } private static List prepareMappingContextAndResolveIndexForType(Class type) {