Skip to content

Commit

Permalink
fix: prevent Optional capture in serializable predicate (#20360) (#20368
Browse files Browse the repository at this point in the history
)

Optional is not serializable, so capturing it into a
SerializablePredicate will prevent serialization to succeed.

Co-authored-by: Marco Collovati <[email protected]>
  • Loading branch information
vaadin-bot and mcollovati authored Oct 29, 2024
1 parent fdb189b commit 0d9fba3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,10 @@ public Optional<T> getPreviousItem(T item) {
@Override
public AbstractListDataView<T> addFilter(SerializablePredicate<T> filter) {
Objects.requireNonNull(filter, "Filter to add cannot be null");
Optional<SerializablePredicate<T>> originalFilter = DataViewUtils
.getComponentFilter(component);
SerializablePredicate<T> newFilter = originalFilter.isPresent()
? item -> originalFilter.get().test(item) && filter.test(item)
: filter;
SerializablePredicate<T> newFilter = DataViewUtils
.<T> getComponentFilter(component)
.map(originalFilter -> originalFilter.and(filter))
.orElse(filter);
return setFilter(newFilter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,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 {

Expand Down Expand Up @@ -1230,6 +1233,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<String> 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<String> {

public ListDataViewImpl(
Expand Down

0 comments on commit 0d9fba3

Please sign in to comment.