Skip to content

Commit 82850d1

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1263 - Index resolver considers generic type argument of collection elements.
We now consider the potential generic type argument of collection elements. Prior to this change an index within List<GenericWrapper<ConcreteWithIndex>> would not have been resolved. Original pull request: #312.
1 parent 8d06015 commit 82850d1

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
108108

109109
try {
110110
if (persistentProperty.isEntity()) {
111-
indexInformation.addAll(resolveIndexForClass(persistentProperty.getActualType(),
111+
indexInformation.addAll(resolveIndexForClass(persistentProperty.getTypeInformation().getActualType(),
112112
persistentProperty.getFieldName(), root.getCollection(), guard));
113113
}
114114

@@ -135,7 +135,7 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
135135
* @return List of {@link IndexDefinitionHolder} representing indexes for given type and its referenced property
136136
* types. Will never be {@code null}.
137137
*/
138-
private List<IndexDefinitionHolder> resolveIndexForClass(final Class<?> type, final String path,
138+
private List<IndexDefinitionHolder> resolveIndexForClass(final TypeInformation<?> type, final String path,
139139
final String collection, final CycleGuard guard) {
140140

141141
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(type);
@@ -153,8 +153,8 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
153153

154154
if (persistentProperty.isEntity()) {
155155
try {
156-
indexInformation.addAll(resolveIndexForClass(persistentProperty.getActualType(), propertyDotPath,
157-
collection, guard));
156+
indexInformation.addAll(resolveIndexForClass(persistentProperty.getTypeInformation().getActualType(),
157+
propertyDotPath, collection, guard));
158158
} catch (CyclicPropertyReferenceException e) {
159159
LOGGER.info(e.getMessage());
160160
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,19 @@ public void shouldOnlyConsiderEntitiesAsPotentialCycleCandidates() {
853853

854854
}
855855

856+
/**
857+
* @see DATAMONGO-1263
858+
*/
859+
@Test
860+
public void shouldConsiderGenericTypeArgumentsOfCollectionElements() {
861+
862+
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(EntityWithGenericTypeWrapperAsElement.class);
863+
864+
assertThat(indexDefinitions, hasSize(1));
865+
assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"),
866+
equalTo("listWithGeneircTypeElement.entity.property_index"));
867+
}
868+
856869
@Document
857870
static class MixedIndexRoot {
858871

@@ -1028,6 +1041,15 @@ public static class AlternatePathToNoCycleButIndenticallNamedPropertiesDeeplyNes
10281041
NoCycleButIndenticallNamedPropertiesDeeplyNested propertyWithIndexedStructure;
10291042
}
10301043

1044+
static class GenericEntityWrapper<T> {
1045+
T entity;
1046+
}
1047+
1048+
@Document
1049+
static class EntityWithGenericTypeWrapperAsElement {
1050+
List<GenericEntityWrapper<DocumentWithNamedIndex>> listWithGeneircTypeElement;
1051+
}
1052+
10311053
}
10321054

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

0 commit comments

Comments
 (0)