From ee7bebcbd05b494c86ad751117534a7652826f65 Mon Sep 17 00:00:00 2001 From: Marco Collovati Date: Mon, 28 Oct 2024 17:57:36 +0100 Subject: [PATCH] fix: prevent Optional capture in serializable predicate Optional is not serializable, so capturing it into a SerializablePredicate will prevent serialization to succeed. --- .../data/provider/AbstractListDataView.java | 9 +++--- .../provider/AbstractListDataViewTest.java | 31 ++++++++++++++----- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/flow-data/src/main/java/com/vaadin/flow/data/provider/AbstractListDataView.java b/flow-data/src/main/java/com/vaadin/flow/data/provider/AbstractListDataView.java index c7b362a6d5d..2b0a6105b7d 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/provider/AbstractListDataView.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/provider/AbstractListDataView.java @@ -110,11 +110,10 @@ public Optional getPreviousItem(T item) { @Override public AbstractListDataView addFilter(SerializablePredicate filter) { Objects.requireNonNull(filter, "Filter to add cannot be null"); - Optional> originalFilter = DataViewUtils - .getComponentFilter(component); - SerializablePredicate newFilter = originalFilter.isPresent() - ? item -> originalFilter.get().test(item) && filter.test(item) - : filter; + SerializablePredicate newFilter = DataViewUtils + . getComponentFilter(component) + .map(originalFilter -> originalFilter.and(filter)) + .orElse(filter); return setFilter(newFilter); } diff --git a/flow-data/src/test/java/com/vaadin/flow/data/provider/AbstractListDataViewTest.java b/flow-data/src/test/java/com/vaadin/flow/data/provider/AbstractListDataViewTest.java index a7be705f6a5..6da8009ce06 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/provider/AbstractListDataViewTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/provider/AbstractListDataViewTest.java @@ -28,23 +28,26 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mockito; + +import com.vaadin.flow.component.Component; import com.vaadin.flow.component.ComponentEventListener; import com.vaadin.flow.component.Tag; import com.vaadin.flow.function.SerializableBiConsumer; import com.vaadin.flow.function.SerializableComparator; import com.vaadin.flow.function.SerializablePredicate; +import com.vaadin.flow.function.SerializableSupplier; import com.vaadin.flow.function.ValueProvider; import com.vaadin.flow.shared.Registration; import com.vaadin.flow.tests.data.bean.Item; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import com.vaadin.flow.component.Component; -import com.vaadin.flow.function.SerializableSupplier; -import org.mockito.Mockito; +import static com.vaadin.flow.tests.server.ClassesSerializableUtils.serializeAndDeserialize; +import static org.junit.Assert.assertNotNull; public class AbstractListDataViewTest { @@ -1299,6 +1302,18 @@ public void filterOrSortingChangedCallback_emptyCallbackProvided_throws() { new ListDataViewImpl(() -> dataProvider, component, null); } + @Test + public void addFilter_serialize_dataViewSerializable() throws Throwable { + DataViewUtils.setComponentFilter(component, term -> true); + ListDataProvider dp = DataProvider.ofCollection(Set.of("A")); + ListDataViewImpl listDataView = new ListDataViewImpl(() -> dp, + component, (filter, sort) -> { + }); + listDataView.addFilter(term -> true); + ListDataViewImpl out = serializeAndDeserialize(listDataView); + assertNotNull(out); + } + private static class ListDataViewImpl extends AbstractListDataView { private final AtomicInteger refreshCount = new AtomicInteger(0);