From 8830955be60dd7f6ad47e238170422eafcbc2d8b Mon Sep 17 00:00:00 2001 From: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com> Date: Wed, 4 May 2022 12:01:12 +0200 Subject: [PATCH] fix: Use incremental numeric id as a key for Tree Grid items (#3046) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Use incremental numeric id as a key for Tree Grid items Using Object::toString as an item's key for server-client communication makes item's data visible on the client side, so generated numeric id is used instead. * Use serializable map * Use index and depth as an item ID * fix data provider getId implementation * use custom unique key provider in tests Co-authored-by: Soroosh Taefi Co-authored-by: Sascha Ißbrücker (cherry picked from commit 8fe08611e304779df01ecd6306d478a9c0edced0) --- .../flow/component/treegrid/TreeGrid.java | 11 +++- .../flow/component/grid/TreeGridTest.java | 62 +++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 vaadin-grid-flow-parent/vaadin-grid-flow/src/test/java/com/vaadin/flow/component/grid/TreeGridTest.java diff --git a/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java b/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java index 3761f4cf901..718890cbf79 100644 --- a/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java +++ b/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java @@ -18,10 +18,13 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; import java.util.Optional; +import java.util.concurrent.atomic.AtomicLong; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -143,8 +146,12 @@ public UpdateQueueData getUpdateQueueData() { } } - private final ValueProvider defaultUniqueKeyProvider = item -> getDataProvider() - .getId(item).toString(); + private final AtomicLong uniqueKeyCounter = new AtomicLong(0); + private final Map objectUniqueKeyMap = new HashMap<>(); + + ValueProvider defaultUniqueKeyProvider = item -> String.valueOf( + objectUniqueKeyMap.computeIfAbsent(getDataProvider().getId(item), + key -> uniqueKeyCounter.getAndIncrement())); private Registration dataProviderRegistration; diff --git a/vaadin-grid-flow-parent/vaadin-grid-flow/src/test/java/com/vaadin/flow/component/grid/TreeGridTest.java b/vaadin-grid-flow-parent/vaadin-grid-flow/src/test/java/com/vaadin/flow/component/grid/TreeGridTest.java new file mode 100644 index 00000000000..7cede715734 --- /dev/null +++ b/vaadin-grid-flow-parent/vaadin-grid-flow/src/test/java/com/vaadin/flow/component/grid/TreeGridTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2000-2022 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.flow.component.grid; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.flow.component.treegrid.TreeGrid; +import com.vaadin.flow.function.ValueProvider; + +public class TreeGridTest { + + @Test + public void defaultUniqueKeyProvider_usesIncrementalLongId() { + Item item1 = new Item("sensitive data 1"); + Item item2 = new Item("sensitive data 2"); + + UniqueKeyTreeGrid grid = new UniqueKeyTreeGrid(); + String key1 = grid.getUniqueKeyProvider().apply(item1); + Assert.assertEquals("0", key1); + + String key2 = grid.getUniqueKeyProvider().apply(item2); + Assert.assertEquals("1", key2); + + key1 = grid.getUniqueKeyProvider().apply(item1); + Assert.assertEquals("0", key1); + } + + private static class UniqueKeyTreeGrid extends TreeGrid { + @Override + public ValueProvider getUniqueKeyProvider() { + return super.getUniqueKeyProvider(); + } + } + + private static class Item { + private final String sensitiveData; + + public Item(String sensitiveData) { + this.sensitiveData = sensitiveData; + } + + public String getSensitiveData() { + return sensitiveData; + } + } + +}