Skip to content

Change Collection to Iterable for event publishing #2927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* @author Christoph Strobl
* @author Yuki Yoshida
* @author Réda Housni Alaoui
* @author Yanming Zhou
* @since 1.13
* @soundtrack Henrik Freischlader Trio - Master Plan (Openness)
*/
Expand Down Expand Up @@ -186,13 +187,13 @@ public void publishEventsFrom(@Nullable Object object, ApplicationEventPublisher
return;
}

for (Object aggregateRoot : asCollection(object)) {
for (Object aggregateRoot : asIterable(object)) {

if (!type.isInstance(aggregateRoot)) {
continue;
}

for (Object event : asCollection(ReflectionUtils.invokeMethod(publishingMethod, aggregateRoot))) {
for (Object event : asIterable(ReflectionUtils.invokeMethod(publishingMethod, aggregateRoot))) {
publisher.publishEvent(event);
}

Expand Down Expand Up @@ -262,21 +263,21 @@ private static Method getClearingMethod(AnnotationDetectionMethodCallback<?> cle
}

/**
* Returns the given source object as collection, i.e. collections are returned as is, objects are turned into a
* Returns the given source object as iterable, i.e. iterables are returned as is, objects are turned into a
* one-element collection, {@literal null} will become an empty collection.
*
* @param source can be {@literal null}.
* @return
* @return iterable
*/
@SuppressWarnings("unchecked")
private static Collection<Object> asCollection(@Nullable Object source) {
private static Iterable<Object> asIterable(@Nullable Object source) {

if (source == null) {
return Collections.emptyList();
}

if (Collection.class.isInstance(source)) {
return (Collection<Object>) source;
if (Iterable.class.isInstance(source)) {
return (Iterable<Object>) source;
}

return Collections.singletonList(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import org.aopalliance.aop.Advice;
Expand All @@ -37,6 +38,8 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.AfterDomainEventPublication;
import org.springframework.data.domain.DomainEvents;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Window;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.support.EventPublishingRepositoryProxyPostProcessor.EventPublishingMethod;
Expand All @@ -49,6 +52,7 @@
* @author Mark Paluch
* @author Yuki Yoshida
* @author Réda Housni Alaoui
* @author Yanming Zhou
* @soundtrack Henrik Freischlader Trio - Nobody Else To Blame (Openness)
*/
@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -198,6 +202,21 @@ void publishesEventsForCallToSaveWithIterable() throws Throwable {
verify(publisher).publishEvent(any(SomeEvent.class));
}

@Test
void publishesEventsForCallToSaveWithIterableAndWindowAsParameter() throws Throwable {

var event = new SomeEvent();
var sample = MultipleEvents.of(Collections.singletonList(event));
Window<MultipleEvents> window = Window.from(List.of(sample), ScrollPosition::offset);
mockInvocation(invocation, SampleRepository.class.getMethod("saveAll", Iterable.class), window);

EventPublishingMethodInterceptor//
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
.invoke(invocation);

verify(publisher).publishEvent(any(SomeEvent.class));
}

@Test // DATACMNS-1663
void publishesEventsForCallToDeleteWithIterable() throws Throwable {

Expand Down