Skip to content

Commit

Permalink
fix: Use incremental numeric id as a key for Tree Grid items (#3046)
Browse files Browse the repository at this point in the history
* 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 <taefi.soroosh@gmail.com>
Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>

(cherry picked from commit 8fe0861)
  • Loading branch information
mshabarov authored and sissbruecker committed May 4, 2022
1 parent 498d3a4 commit 8830955
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -143,8 +146,12 @@ public UpdateQueueData getUpdateQueueData() {
}
}

private final ValueProvider<T, String> defaultUniqueKeyProvider = item -> getDataProvider()
.getId(item).toString();
private final AtomicLong uniqueKeyCounter = new AtomicLong(0);
private final Map<Object, Long> objectUniqueKeyMap = new HashMap<>();

ValueProvider<T, String> defaultUniqueKeyProvider = item -> String.valueOf(
objectUniqueKeyMap.computeIfAbsent(getDataProvider().getId(item),
key -> uniqueKeyCounter.getAndIncrement()));

private Registration dataProviderRegistration;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Item> {
@Override
public ValueProvider<Item, String> getUniqueKeyProvider() {
return super.getUniqueKeyProvider();
}
}

private static class Item {
private final String sensitiveData;

public Item(String sensitiveData) {
this.sensitiveData = sensitiveData;
}

public String getSensitiveData() {
return sensitiveData;
}
}

}

0 comments on commit 8830955

Please sign in to comment.