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.
This commit is contained in:
Christoph Strobl
2015-08-03 15:30:51 +02:00
committed by Oliver Gierke
parent 8d0601550c
commit 82850d1605
2 changed files with 26 additions and 4 deletions

View File

@@ -108,7 +108,7 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
try {
if (persistentProperty.isEntity()) {
indexInformation.addAll(resolveIndexForClass(persistentProperty.getActualType(),
indexInformation.addAll(resolveIndexForClass(persistentProperty.getTypeInformation().getActualType(),
persistentProperty.getFieldName(), root.getCollection(), guard));
}
@@ -135,7 +135,7 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
* @return List of {@link IndexDefinitionHolder} representing indexes for given type and its referenced property
* types. Will never be {@code null}.
*/
private List<IndexDefinitionHolder> resolveIndexForClass(final Class<?> type, final String path,
private List<IndexDefinitionHolder> resolveIndexForClass(final TypeInformation<?> type, final String path,
final String collection, final CycleGuard guard) {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(type);
@@ -153,8 +153,8 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
if (persistentProperty.isEntity()) {
try {
indexInformation.addAll(resolveIndexForClass(persistentProperty.getActualType(), propertyDotPath,
collection, guard));
indexInformation.addAll(resolveIndexForClass(persistentProperty.getTypeInformation().getActualType(),
propertyDotPath, collection, guard));
} catch (CyclicPropertyReferenceException e) {
LOGGER.info(e.getMessage());
}

View File

@@ -853,6 +853,19 @@ public class MongoPersistentEntityIndexResolverUnitTests {
}
/**
* @see DATAMONGO-1263
*/
@Test
public void shouldConsiderGenericTypeArgumentsOfCollectionElements() {
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(EntityWithGenericTypeWrapperAsElement.class);
assertThat(indexDefinitions, hasSize(1));
assertThat((String) indexDefinitions.get(0).getIndexOptions().get("name"),
equalTo("listWithGeneircTypeElement.entity.property_index"));
}
@Document
static class MixedIndexRoot {
@@ -1028,6 +1041,15 @@ public class MongoPersistentEntityIndexResolverUnitTests {
NoCycleButIndenticallNamedPropertiesDeeplyNested propertyWithIndexedStructure;
}
static class GenericEntityWrapper<T> {
T entity;
}
@Document
static class EntityWithGenericTypeWrapperAsElement {
List<GenericEntityWrapper<DocumentWithNamedIndex>> listWithGeneircTypeElement;
}
}
private static List<IndexDefinitionHolder> prepareMappingContextAndResolveIndexForType(Class<?> type) {