Skip to content

Commit d56325a

Browse files
authored
fix: return empty Text component if renderer is updated with null (#8168)
Add the same behavior as when `createComponenet` is called and the component renderer returns `null` to the `updateComponent` method. That prevents a NPE in the `AbstractComponentDataGenerator#refreshData` method when it tries to call `getElement` in the updated component. Fixes #6825
1 parent 18b99f1 commit d56325a

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

vaadin-messages-flow-parent/vaadin-messages-flow/src/test/java/com/vaadin/flow/component/messages/tests/MessageListTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,21 @@ public class MessageListTest {
4040
private MessageList messageList;
4141
private MessageListItem item1;
4242
private MessageListItem item2;
43+
private UI ui;
4344

4445
@Before
4546
public void setup() {
4647
messageList = new MessageList();
4748
item1 = new MessageListItem();
49+
ui = new UI();
50+
UI.setCurrent(ui);
4851
item2 = new MessageListItem();
4952
}
5053

5154
@After
5255
public void tearDown() {
5356
UI.setCurrent(null);
57+
ui = null;
5458
}
5559

5660
@Test(expected = UnsupportedOperationException.class)
@@ -94,7 +98,6 @@ public void setItems_containsNullItem_throws() {
9498

9599
@Test
96100
public void setImageAsStreamResource_overridesImageUrl() {
97-
UI.setCurrent(new UI());
98101
item1.setUserImage("foo/bar");
99102
item1.setUserImageResource(new StreamResource("message-list-img",
100103
() -> getClass().getResourceAsStream("baz/qux")));
@@ -105,7 +108,6 @@ public void setImageAsStreamResource_overridesImageUrl() {
105108

106109
@Test
107110
public void setImageAsUrl_streamResourceBecomesNull() {
108-
UI.setCurrent(new UI());
109111
item1.setUserImageResource(new StreamResource("message-list-img",
110112
() -> getClass().getResourceAsStream("baz/qux")));
111113
item1.setUserImage("foo/bar");
@@ -114,7 +116,6 @@ public void setImageAsUrl_streamResourceBecomesNull() {
114116

115117
@Test
116118
public void setImageHandler_overridesImageUrl() {
117-
UI.setCurrent(new UI());
118119
item1.setUserImage("foo/bar");
119120
item1.setUserImageHandler(
120121
DownloadHandler.fromInputStream(data -> new DownloadResponse(
@@ -127,7 +128,6 @@ public void setImageHandler_overridesImageUrl() {
127128

128129
@Test
129130
public void setImageHandler_streamResourceBecomesNull() {
130-
UI.setCurrent(new UI());
131131
item1.setUserImageHandler(
132132
DownloadHandler.fromInputStream(data -> new DownloadResponse(
133133
getClass().getResourceAsStream("baz/qux"),

vaadin-renderer-flow-parent/vaadin-renderer-flow/src/main/java/com/vaadin/flow/data/renderer/ComponentDataGenerator.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,20 @@ public void generateData(T item, ObjectNode jsonObject) {
101101

102102
@Override
103103
protected Component createComponent(T item) {
104-
Component c = componentRenderer.createComponent(item);
105-
if (c == null) {
106-
c = new Text("");
107-
}
108-
return c;
104+
return ensureNonNullComponent(componentRenderer.createComponent(item));
109105
}
110106

111107
@Override
112108
protected Component updateComponent(Component currentComponent, T item) {
113-
return componentRenderer.updateComponent(currentComponent, item);
109+
return ensureNonNullComponent(
110+
componentRenderer.updateComponent(currentComponent, item));
111+
}
112+
113+
private Component ensureNonNullComponent(Component component) {
114+
if (component == null) {
115+
return new Text("");
116+
}
117+
return component;
114118
}
115119

116120
@Override

vaadin-renderer-flow-parent/vaadin-renderer-flow/src/test/java/com/vaadin/flow/data/renderer/ComponentRendererTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ public void nullValues() {
152152
Assert.assertNotNull(
153153
"Placeholder component should be generated for null values", c);
154154
Assert.assertEquals(0, c.getElement().getChildCount());
155+
156+
c = cdg.updateComponent(c, "foo");
157+
Assert.assertNotNull(
158+
"Placeholder component should be generated for null values", c);
159+
Assert.assertEquals(0, c.getElement().getChildCount());
155160
}
156161

157162
private void attachElement(UI ui, Element contentTemplate) {

0 commit comments

Comments
 (0)