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: Fix NPE if component renderer returns null #4361

Merged
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 @@ -18,6 +18,7 @@
import org.slf4j.LoggerFactory;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Text;
import com.vaadin.flow.data.provider.AbstractComponentDataGenerator;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.function.ValueProvider;
Expand Down Expand Up @@ -100,7 +101,11 @@ public void generateData(T item, JsonObject jsonObject) {

@Override
protected Component createComponent(T item) {
return componentRenderer.createComponent(item);
Component c = componentRenderer.createComponent(item);
if (c == null) {
c = new Text("");
}
return c;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.vaadin.flow.function.ValueProvider;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -132,6 +133,23 @@ public void componentRenderer_childAttachedBeforeParent() {

}

@Test
public void nullValues() {
ComponentRenderer<Component, String> renderer = new ComponentRenderer<>(
e -> {
return null;
});

ValueProvider<String, String> keyMapper = s -> "foo";
ComponentDataGenerator cdg = new ComponentDataGenerator<>(renderer,
keyMapper);

Component c = cdg.createComponent("foo");
Assert.assertNotNull(
"Placeholder component should be generated for null values", c);
Assert.assertEquals(0, c.getElement().getChildCount());
}

private void attachElement(UI ui, Element contentTemplate) {
ui.getElement().appendChild(contentTemplate);
}
Expand Down