Skip to content

Commit

Permalink
Treat Window as Collection for event publishing
Browse files Browse the repository at this point in the history
repository.saveAll(Window<?>) will be handled properly after this commit.

Fixes https://github.com/spring-projects/spring-data-jpa/issues/3153
  • Loading branch information
quaff committed Sep 15, 2023
1 parent e901923 commit 5fc7e4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.AfterDomainEventPublication;
import org.springframework.data.domain.DomainEvents;
import org.springframework.data.domain.Window;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.util.AnnotationDetectionMethodCallback;
Expand All @@ -47,6 +48,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 @@ -268,15 +270,19 @@ private static Method getClearingMethod(AnnotationDetectionMethodCallback<?> cle
* @param source can be {@literal null}.
* @return
*/
@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "rawtypes"})
private static Collection<Object> asCollection(@Nullable Object source) {

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

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

if (source instanceof Window window) {
return window.toList();
}

return Collections.singletonList(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,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 @@ -50,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 @@ -199,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

0 comments on commit 5fc7e4f

Please sign in to comment.