Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent Optional capture in serializable predicate (#20360) (CP: 24.4) #20367

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -27,23 +27,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 @@ -1272,6 +1275,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