Skip to content

Commit 1ea3bc5

Browse files
authored
fix: prevent Optional capture in serializable predicate (#20360)
Optional is not serializable, so capturing it into a SerializablePredicate will prevent serialization to succeed.
1 parent 0898486 commit 1ea3bc5

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

‎flow-data/src/main/java/com/vaadin/flow/data/provider/AbstractListDataView.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,10 @@ public Optional<T> getPreviousItem(T item) {
110110
@Override
111111
public AbstractListDataView<T> addFilter(SerializablePredicate<T> filter) {
112112
Objects.requireNonNull(filter, "Filter to add cannot be null");
113-
Optional<SerializablePredicate<T>> originalFilter = DataViewUtils
114-
.getComponentFilter(component);
115-
SerializablePredicate<T> newFilter = originalFilter.isPresent()
116-
? item -> originalFilter.get().test(item) && filter.test(item)
117-
: filter;
113+
SerializablePredicate<T> newFilter = DataViewUtils
114+
.<T> getComponentFilter(component)
115+
.map(originalFilter -> originalFilter.and(filter))
116+
.orElse(filter);
118117
return setFilter(newFilter);
119118
}
120119

‎flow-data/src/test/java/com/vaadin/flow/data/provider/AbstractListDataViewTest.java‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,26 @@
2828
import java.util.stream.Collectors;
2929
import java.util.stream.Stream;
3030

31+
import org.junit.Assert;
32+
import org.junit.Before;
33+
import org.junit.Rule;
34+
import org.junit.Test;
35+
import org.junit.rules.ExpectedException;
36+
import org.mockito.Mockito;
37+
38+
import com.vaadin.flow.component.Component;
3139
import com.vaadin.flow.component.ComponentEventListener;
3240
import com.vaadin.flow.component.Tag;
3341
import com.vaadin.flow.function.SerializableBiConsumer;
3442
import com.vaadin.flow.function.SerializableComparator;
3543
import com.vaadin.flow.function.SerializablePredicate;
44+
import com.vaadin.flow.function.SerializableSupplier;
3645
import com.vaadin.flow.function.ValueProvider;
3746
import com.vaadin.flow.shared.Registration;
3847
import com.vaadin.flow.tests.data.bean.Item;
39-
import org.junit.Assert;
40-
import org.junit.Before;
41-
import org.junit.Rule;
42-
import org.junit.Test;
43-
import org.junit.rules.ExpectedException;
4448

45-
import com.vaadin.flow.component.Component;
46-
import com.vaadin.flow.function.SerializableSupplier;
47-
import org.mockito.Mockito;
49+
import static com.vaadin.flow.tests.server.ClassesSerializableUtils.serializeAndDeserialize;
50+
import static org.junit.Assert.assertNotNull;
4851

4952
public class AbstractListDataViewTest {
5053

@@ -1299,6 +1302,18 @@ public void filterOrSortingChangedCallback_emptyCallbackProvided_throws() {
12991302
new ListDataViewImpl(() -> dataProvider, component, null);
13001303
}
13011304

1305+
@Test
1306+
public void addFilter_serialize_dataViewSerializable() throws Throwable {
1307+
DataViewUtils.setComponentFilter(component, term -> true);
1308+
ListDataProvider<String> dp = DataProvider.ofCollection(Set.of("A"));
1309+
ListDataViewImpl listDataView = new ListDataViewImpl(() -> dp,
1310+
component, (filter, sort) -> {
1311+
});
1312+
listDataView.addFilter(term -> true);
1313+
ListDataViewImpl out = serializeAndDeserialize(listDataView);
1314+
assertNotNull(out);
1315+
}
1316+
13021317
private static class ListDataViewImpl extends AbstractListDataView<String> {
13031318

13041319
private final AtomicInteger refreshCount = new AtomicInteger(0);

0 commit comments

Comments
 (0)