diff options
4 files changed, 99 insertions, 6 deletions
diff --git a/server/src/main/java/com/vaadin/ui/ComboBox.java b/server/src/main/java/com/vaadin/ui/ComboBox.java index 54a3eea430..62e7ef7751 100644 --- a/server/src/main/java/com/vaadin/ui/ComboBox.java +++ b/server/src/main/java/com/vaadin/ui/ComboBox.java @@ -240,8 +240,11 @@ public class ComboBox<T> extends AbstractSingleSelect<T> registerRpc(new FocusAndBlurServerRpcDecorator(this, this::fireEvent)); addDataGenerator((T data, JsonObject jsonObject) -> { - jsonObject.put(DataCommunicatorConstants.NAME, - getItemCaptionGenerator().apply(data)); + String caption = getItemCaptionGenerator().apply(data); + if (caption == null) { + caption = ""; + } + jsonObject.put(DataCommunicatorConstants.NAME, caption); String style = itemStyleGenerator.apply(data); if (style != null) { jsonObject.put(ComboBoxConstants.STYLE, style); diff --git a/server/src/main/java/com/vaadin/ui/ItemCaptionGenerator.java b/server/src/main/java/com/vaadin/ui/ItemCaptionGenerator.java index d4117c195b..13de27cdd6 100644 --- a/server/src/main/java/com/vaadin/ui/ItemCaptionGenerator.java +++ b/server/src/main/java/com/vaadin/ui/ItemCaptionGenerator.java @@ -36,7 +36,8 @@ public interface ItemCaptionGenerator<T> * * @param item * the item to get caption for - * @return the caption of the item; not {@code null} + * @return the caption of the item; {@code null} will be shown as an empty + * string */ @Override String apply(T item); diff --git a/server/src/main/java/com/vaadin/ui/NativeSelect.java b/server/src/main/java/com/vaadin/ui/NativeSelect.java index 2c994b8384..4d08aacd12 100644 --- a/server/src/main/java/com/vaadin/ui/NativeSelect.java +++ b/server/src/main/java/com/vaadin/ui/NativeSelect.java @@ -52,9 +52,13 @@ public class NativeSelect<T> extends AbstractSingleSelect<T> */ public NativeSelect() { registerRpc(new FocusAndBlurServerRpcDecorator(this, this::fireEvent)); - addDataGenerator( - (item, json) -> json.put(DataCommunicatorConstants.DATA, - getItemCaptionGenerator().apply(item))); + addDataGenerator((item, json) -> { + String caption = getItemCaptionGenerator().apply(item); + if (caption == null) { + caption = ""; + } + json.put(DataCommunicatorConstants.DATA, caption); + }); setItemCaptionGenerator(String::valueOf); } diff --git a/server/src/test/java/com/vaadin/tests/server/component/ItemCaptionGeneratorTest.java b/server/src/test/java/com/vaadin/tests/server/component/ItemCaptionGeneratorTest.java new file mode 100644 index 0000000000..1f80e46476 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/server/component/ItemCaptionGeneratorTest.java @@ -0,0 +1,85 @@ +package com.vaadin.tests.server.component; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.provider.DataGenerator; +import com.vaadin.shared.data.DataCommunicatorConstants; +import com.vaadin.shared.ui.ListingJsonConstants; +import com.vaadin.ui.AbstractListing; +import com.vaadin.ui.CheckBoxGroup; +import com.vaadin.ui.ComboBox; +import com.vaadin.ui.ListSelect; +import com.vaadin.ui.NativeSelect; +import com.vaadin.ui.RadioButtonGroup; +import com.vaadin.ui.TwinColSelect; + +import elemental.json.JsonObject; + +public class ItemCaptionGeneratorTest { + + private static class TestDataGenerator implements DataGenerator<Object> { + JsonObject generated = null; + + @Override + public void generateData(Object item, JsonObject jsonObject) { + generated = jsonObject; + } + + } + + @Test + public void testItemCaptionGenerator_nullCaptionGiven_convertedToEmptyString() { + Collection<AbstractListing<Object>> listings = new ArrayList<>(); + + ComboBox<Object> comboBox = new ComboBox<>(); + comboBox.setData(DataCommunicatorConstants.NAME); + comboBox.setItemCaptionGenerator(item -> null); + listings.add(comboBox); + + CheckBoxGroup<Object> cbg = new CheckBoxGroup<>(); + cbg.setData(ListingJsonConstants.JSONKEY_ITEM_VALUE); + cbg.setItemCaptionGenerator(item -> null); + listings.add(cbg); + + ListSelect<Object> listSelect = new ListSelect<>(); + listSelect.setData(ListingJsonConstants.JSONKEY_ITEM_VALUE); + listSelect.setItemCaptionGenerator(item -> null); + listings.add(listSelect); + + NativeSelect<Object> nativeSelect = new NativeSelect<>(); + nativeSelect.setData(DataCommunicatorConstants.DATA); + nativeSelect.setItemCaptionGenerator(item -> null); + listings.add(nativeSelect); + + RadioButtonGroup<Object> rbg = new RadioButtonGroup<>(); + rbg.setData(ListingJsonConstants.JSONKEY_ITEM_VALUE); + rbg.setItemCaptionGenerator(item -> null); + listings.add(rbg); + + TwinColSelect<Object> tc = new TwinColSelect<>(); + tc.setData(ListingJsonConstants.JSONKEY_ITEM_VALUE); + tc.setItemCaptionGenerator(item -> null); + listings.add(tc); + + for (AbstractListing<Object> listing : listings) { + listing.setItems("Uno"); + TestDataGenerator dataGenerator = new TestDataGenerator(); + listing.getDataCommunicator().addDataGenerator(dataGenerator); + listing.getDataCommunicator().beforeClientResponse(true); + + Assert.assertEquals( + listing.getClass().getName() + + " does not convert null caption from generator to empty string", + "", + dataGenerator.generated.hasKey((String) listing.getData()) + ? dataGenerator.generated.getString( + (String) listing.getData()) + : null); + } + } + +} |