Skip to content

Commit e2e1dbc

Browse files
committed
Change Collection to Iterable for event publishing
repository.saveAll(Window<?>) will be handled properly after this commit. Fixes https://github.com/spring-projects/spring-data-jpa/issues/3153
1 parent d5cd46c commit e2e1dbc

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* @author Christoph Strobl
4848
* @author Yuki Yoshida
4949
* @author Réda Housni Alaoui
50+
* @author Yanming Zhou
5051
* @since 1.13
5152
* @soundtrack Henrik Freischlader Trio - Master Plan (Openness)
5253
*/
@@ -186,13 +187,13 @@ public void publishEventsFrom(@Nullable Object object, ApplicationEventPublisher
186187
return;
187188
}
188189

189-
for (Object aggregateRoot : asCollection(object)) {
190+
for (Object aggregateRoot : asIterable(object)) {
190191

191192
if (!type.isInstance(aggregateRoot)) {
192193
continue;
193194
}
194195

195-
for (Object event : asCollection(ReflectionUtils.invokeMethod(publishingMethod, aggregateRoot))) {
196+
for (Object event : asIterable(ReflectionUtils.invokeMethod(publishingMethod, aggregateRoot))) {
196197
publisher.publishEvent(event);
197198
}
198199

@@ -262,21 +263,21 @@ private static Method getClearingMethod(AnnotationDetectionMethodCallback<?> cle
262263
}
263264

264265
/**
265-
* Returns the given source object as collection, i.e. collections are returned as is, objects are turned into a
266+
* Returns the given source object as iterable, i.e. iterables are returned as is, objects are turned into a
266267
* one-element collection, {@literal null} will become an empty collection.
267268
*
268269
* @param source can be {@literal null}.
269-
* @return
270+
* @return iterable
270271
*/
271272
@SuppressWarnings("unchecked")
272-
private static Collection<Object> asCollection(@Nullable Object source) {
273+
private static Iterable<Object> asIterable(@Nullable Object source) {
273274

274275
if (source == null) {
275276
return Collections.emptyList();
276277
}
277278

278-
if (Collection.class.isInstance(source)) {
279-
return (Collection<Object>) source;
279+
if (Iterable.class.isInstance(source)) {
280+
return (Iterable<Object>) source;
280281
}
281282

282283
return Collections.singletonList(source);

src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Arrays;
2424
import java.util.Collection;
2525
import java.util.Collections;
26+
import java.util.List;
2627
import java.util.UUID;
2728

2829
import org.aopalliance.aop.Advice;
@@ -37,6 +38,8 @@
3738
import org.springframework.context.ApplicationEventPublisher;
3839
import org.springframework.data.domain.AfterDomainEventPublication;
3940
import org.springframework.data.domain.DomainEvents;
41+
import org.springframework.data.domain.ScrollPosition;
42+
import org.springframework.data.domain.Window;
4043
import org.springframework.data.repository.CrudRepository;
4144
import org.springframework.data.repository.core.RepositoryInformation;
4245
import org.springframework.data.repository.core.support.EventPublishingRepositoryProxyPostProcessor.EventPublishingMethod;
@@ -49,6 +52,7 @@
4952
* @author Mark Paluch
5053
* @author Yuki Yoshida
5154
* @author Réda Housni Alaoui
55+
* @author Yanming Zhou
5256
* @soundtrack Henrik Freischlader Trio - Nobody Else To Blame (Openness)
5357
*/
5458
@ExtendWith(MockitoExtension.class)
@@ -198,6 +202,21 @@ void publishesEventsForCallToSaveWithIterable() throws Throwable {
198202
verify(publisher).publishEvent(any(SomeEvent.class));
199203
}
200204

205+
@Test
206+
void publishesEventsForCallToSaveWithIterableAndWindowAsParameter() throws Throwable {
207+
208+
var event = new SomeEvent();
209+
var sample = MultipleEvents.of(Collections.singletonList(event));
210+
Window<MultipleEvents> window = Window.from(List.of(sample), ScrollPosition::offset);
211+
mockInvocation(invocation, SampleRepository.class.getMethod("saveAll", Iterable.class), window);
212+
213+
EventPublishingMethodInterceptor//
214+
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
215+
.invoke(invocation);
216+
217+
verify(publisher).publishEvent(any(SomeEvent.class));
218+
}
219+
201220
@Test // DATACMNS-1663
202221
void publishesEventsForCallToDeleteWithIterable() throws Throwable {
203222

0 commit comments

Comments
 (0)