Skip to content

Commit 1b0462a

Browse files
authored
fix: Null for primitive should not fail decode (#22403)
Decoding null to primitive should not fail as dom event data may be missing parts sent from the client.
1 parent 8594711 commit 1b0462a

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

flow-polymer-template/src/test/java/com/vaadin/flow/server/communication/rpc/PolymerPublishedServerEventHandlerRpcHandlerTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,21 +452,19 @@ public void methodWithVarArg_arrayIsCorrectlyHandled() {
452452
}
453453

454454
@Test
455-
public void nullValueShouldFailForPrimitive() {
455+
public void nullValueShouldNotFailForPrimitive() {
456456
ArrayNode array = JacksonUtils.createArrayNode();
457457
array.add(JacksonUtils.nullNode());
458458
MethodWithParameters component = new MethodWithParameters();
459459
component.intArg = -1;
460460
component.booleanArg = true;
461461

462462
// Passing null to a primitive parameter should throw an exception
463-
Assert.assertThrows(IllegalArgumentException.class, () -> {
464-
PublishedServerEventHandlerRpcHandler.invokeMethod(component,
465-
component.getClass(), "intMethod", array, -1);
466-
});
463+
PublishedServerEventHandlerRpcHandler.invokeMethod(component,
464+
component.getClass(), "intMethod", array, -1);
467465

468466
// Verify the field was not modified
469-
Assert.assertEquals(-1, component.intArg);
467+
Assert.assertEquals(0, component.intArg);
470468
}
471469

472470
@Test(expected = IllegalStateException.class)

flow-server/src/main/java/com/vaadin/flow/internal/JacksonUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import tools.jackson.core.util.DefaultPrettyPrinter;
3939
import tools.jackson.core.util.Separators;
4040
import tools.jackson.core.util.Separators.Spacing;
41+
import tools.jackson.databind.DeserializationFeature;
4142
import tools.jackson.databind.JsonNode;
4243
import tools.jackson.databind.ObjectMapper;
4344
import tools.jackson.databind.json.JsonMapper;
@@ -71,7 +72,9 @@ public final class JacksonUtils {
7172
private static final String CANNOT_CONVERT_NULL_TO_OBJECT = "Cannot convert null to Java object";
7273

7374
private static final ObjectMapper objectMapper = JsonMapper.builder()
74-
.enable(JsonReadFeature.ALLOW_SINGLE_QUOTES).build();
75+
.enable(JsonReadFeature.ALLOW_SINGLE_QUOTES)
76+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
77+
.build();
7578

7679
public static ObjectMapper getMapper() {
7780
return objectMapper;

flow-server/src/test/java/com/vaadin/flow/component/ComponentEventBusTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,18 @@ public void mappedDomEvent_fire_noListeners() {
177177
}
178178

179179
@Test
180-
public void mappedDomEvent_fire_missingData_shouldFail() {
180+
public void mappedDomEvent_fire_missingData() {
181181
TestComponent c = new TestComponent();
182182
EventTracker<MappedToDomEvent> eventListener = new EventTracker<>();
183183
c.addListener(MappedToDomEvent.class, eventListener);
184184

185-
// Missing primitive boolean data should cause event creation to fail
186-
Assert.assertThrows(IllegalArgumentException.class, () -> {
187-
fireDomEvent(c, "dom-event", createData("event.someData", 2));
188-
});
185+
fireDomEvent(c, "dom-event", createData("event.someData", 2));
189186

190-
// Event should not have been called due to the failure
191-
eventListener.assertEventNotCalled();
187+
eventListener.assertEventCalled(c, true);
188+
Assert.assertEquals(2, eventListener.getEvent().getSomeData());
189+
Assert.assertNull(eventListener.getEvent().getMoreData());
190+
Assert.assertFalse(eventListener.getEvent().getPrimitiveBoolean());
191+
Assert.assertNull(eventListener.getEvent().getObjectBoolean());
192192
}
193193

194194
@Test

flow-server/src/test/java/com/vaadin/flow/internal/JacksonCodecTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,5 @@ public void testDecodeAsForPrimitiveTypes() {
434434
Assert.assertEquals(Double.valueOf(3.14), JacksonCodec
435435
.decodeAs(objectMapper.valueToTree(3.14), Double.class));
436436
}
437+
437438
}

flow-server/src/test/java/com/vaadin/flow/server/communication/rpc/PublishedServerEventHandlerRpcHandlerTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -495,21 +495,19 @@ public void methodWithVarArg_arrayIsCorrectlyHandled() {
495495
}
496496

497497
@Test
498-
public void nullValueShouldFailForPrimitive() {
498+
public void nullValueShouldReturnZeroForPrimitive() {
499499
ArrayNode array = JacksonUtils.createArrayNode();
500500
array.add(JacksonUtils.nullNode());
501501
MethodWithParameters component = new MethodWithParameters();
502502
component.intArg = -1;
503503
component.booleanArg = true;
504504

505505
// Passing null to a primitive parameter should throw an exception
506-
Assert.assertThrows(IllegalArgumentException.class, () -> {
507-
PublishedServerEventHandlerRpcHandler.invokeMethod(component,
508-
component.getClass(), "intMethod", array, -1);
509-
});
506+
PublishedServerEventHandlerRpcHandler.invokeMethod(component,
507+
component.getClass(), "intMethod", array, -1);
510508

511509
// Verify the field was not modified
512-
Assert.assertEquals(-1, component.intArg);
510+
Assert.assertEquals(0, component.intArg);
513511
}
514512

515513
@Test(expected = IllegalStateException.class)

0 commit comments

Comments
 (0)