Skip to content

Commit c3d533d

Browse files
committed
DATAMONGO-411 - Double check type of PersistentEntity for index creation.
The Spring container does not check nested generic types of the type parameter of ApplicationEvent<T>. As T is parameterized in our case as well (PersistentEntity<…, …>) we can code an event listener against that fully parameterized type but might run into ClassCastExceptions as we might get other implementations handed into the method at runtime. We now do an instanceof check to safely invoke checkForIndexes(…) only in case we get the correct event type.
1 parent e4b9be6 commit c3d533d

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.slf4j.Logger;
2424
import org.slf4j.LoggerFactory;
2525
import org.springframework.context.ApplicationListener;
26+
import org.springframework.data.mapping.PersistentEntity;
2627
import org.springframework.data.mapping.PropertyHandler;
2728
import org.springframework.data.mapping.event.MappingContextEvent;
2829
import org.springframework.data.mongodb.MongoDbFactory;
@@ -76,7 +77,13 @@ public MongoPersistentEntityIndexCreator(MongoMappingContext mappingContext, Mon
7677
*/
7778
public void onApplicationEvent(
7879
MappingContextEvent<MongoPersistentEntity<MongoPersistentProperty>, MongoPersistentProperty> event) {
79-
checkForIndexes(event.getPersistentEntity());
80+
81+
PersistentEntity<?, ?> entity = event.getPersistentEntity();
82+
83+
// Double check type as Spring infrastructure does not consider nested generics
84+
if (entity instanceof MongoPersistentEntity) {
85+
checkForIndexes(event.getPersistentEntity());
86+
}
8087
}
8188

8289
protected void checkForIndexes(final MongoPersistentEntity<?> entity) {

0 commit comments

Comments
 (0)