Skip to content

Commit

Permalink
fix: prevent Optional capture in serializable predicate
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.
  • Loading branch information
mcollovati committed Oct 28, 2024
1 parent f1c9673 commit ee7bebc
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 @@ -110,11 +110,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 @@ -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 {

Expand Down Expand Up @@ -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<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> {

private final AtomicInteger refreshCount = new AtomicInteger(0);
Expand Down

0 comments on commit ee7bebc

Please sign in to comment.