Skip to content

Commit e411831

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1823 - Emit ApplicationEvents using projecting find methods.
We now again emit application events when using finder methods that apply projection. Original pull request: spring-projects#517.
1 parent eb51828 commit e411831

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2813,10 +2813,15 @@ public T doWith(@Nullable Document object) {
28132813

28142814
Class<?> typeToRead = targetType.isInterface() || targetType.isAssignableFrom(entityType) ? entityType
28152815
: targetType;
2816+
2817+
if (null != object) {
2818+
maybeEmitEvent(new AfterLoadEvent<T>(object, targetType, collectionName));
2819+
}
2820+
28162821
Object source = reader.read(typeToRead, object);
28172822
Object result = targetType.isInterface() ? projectionFactory.createProjection(targetType, source) : source;
28182823

2819-
if (result == null) {
2824+
if (result != null) {
28202825
maybeEmitEvent(new AfterConvertEvent<>(object, result, collectionName));
28212826
}
28222827

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTests.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import static org.hamcrest.core.Is.*;
2020
import static org.hamcrest.core.IsEqual.*;
2121
import static org.junit.Assert.*;
22-
import static org.springframework.data.mongodb.core.DocumentTestUtils.assertTypeHint;
22+
import static org.springframework.data.mongodb.core.DocumentTestUtils.*;
2323
import static org.springframework.data.mongodb.core.query.Criteria.*;
2424
import static org.springframework.data.mongodb.core.query.Query.*;
2525

26+
import lombok.Data;
27+
2628
import java.net.UnknownHostException;
2729
import java.util.Arrays;
2830
import java.util.LinkedHashMap;
@@ -46,8 +48,6 @@
4648
import com.mongodb.MongoClient;
4749
import com.mongodb.WriteConcern;
4850

49-
import lombok.Data;
50-
5151
/**
5252
* Integration test for Mapping Events.
5353
*
@@ -390,6 +390,24 @@ public void publishesAfterLoadAndAfterConvertEventsForLazyLoadingMapOfDBRef() th
390390
is(equalTo(RELATED_COLLECTION_NAME)));
391391
}
392392

393+
@Test // DATAMONGO-1823
394+
public void publishesAfterConvertEventForFindQueriesUsingProjections() {
395+
396+
PersonPojoStringId entity = new PersonPojoStringId("1", "Text");
397+
template.insert(entity);
398+
399+
template.query(PersonPojoStringId.class).matching(query(where("id").is(entity.getId()))).all();
400+
401+
assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(1));
402+
assertThat(simpleMappingEventListener.onAfterLoadEvents.get(0).getCollectionName(), is(COLLECTION_NAME));
403+
404+
assertThat(simpleMappingEventListener.onBeforeConvertEvents.size(), is(1));
405+
assertThat(simpleMappingEventListener.onBeforeConvertEvents.get(0).getCollectionName(), is(COLLECTION_NAME));
406+
407+
assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(1));
408+
assertThat(simpleMappingEventListener.onAfterConvertEvents.get(0).getCollectionName(), is(COLLECTION_NAME));
409+
}
410+
393411
private void comparePersonAndDocument(PersonPojoStringId p, PersonPojoStringId p2, org.bson.Document document) {
394412

395413
assertEquals(p.getId(), p2.getId());

0 commit comments

Comments
 (0)