Skip to content

Commit a4dec48

Browse files
author
Denis
authored
fix: allow to use null from clients side with primitives (#11948)
fixes #4062
1 parent 9d9f2ed commit a4dec48

3 files changed

Lines changed: 47 additions & 12 deletions

File tree

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ private void intMethod() {
9797
public static class MethodWithParameters extends ComponentWithMethod {
9898

9999
private int intArg;
100+
private boolean booleanArg;
100101
private String strArg;
101102
private boolean[] arrayArg;
102103
private Double[] doubleArg;
@@ -110,6 +111,11 @@ protected void intMethod(@EventData("foo") int i) {
110111
intArg = i;
111112
}
112113

114+
@EventHandler
115+
protected void booleanMethod(@EventData("foo") boolean value) {
116+
booleanArg = value;
117+
}
118+
113119
@EventHandler
114120
protected void method1(@EventData("foo") String str,
115121
@EventData("bar") boolean[] array) {
@@ -450,13 +456,22 @@ public void methodWithVarArg_arrayIsCorrectlyHandled() {
450456
component.varArg);
451457
}
452458

453-
@Test(expected = IllegalArgumentException.class)
454-
public void nullValueIsNotAcceptedForPrimitive() {
459+
@Test
460+
public void nullValueAreAcceptedForPrimitive() {
455461
JsonArray array = Json.createArray();
456462
array.set(0, Json.createNull());
457463
MethodWithParameters component = new MethodWithParameters();
464+
component.intArg = -1;
465+
component.booleanArg = true;
458466
PublishedServerEventHandlerRpcHandler.invokeMethod(component,
459-
component.getClass(), "method", array, -1);
467+
component.getClass(), "intMethod", array, -1);
468+
469+
Assert.assertEquals(0, component.intArg);
470+
471+
PublishedServerEventHandlerRpcHandler.invokeMethod(component,
472+
component.getClass(), "booleanMethod", array, -1);
473+
474+
Assert.assertFalse(component.booleanArg);
460475
}
461476

462477
@Test(expected = IllegalStateException.class)

‎flow-server/src/main/java/com/vaadin/flow/server/communication/rpc/PublishedServerEventHandlerRpcHandler.java‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.vaadin.flow.component.template.internal.DeprecatedPolymerPublishedEventHandler;
3636
import com.vaadin.flow.di.Lookup;
3737
import com.vaadin.flow.dom.DisabledUpdateMode;
38+
import com.vaadin.flow.internal.JsonCodec;
3839
import com.vaadin.flow.internal.ReflectTools;
3940
import com.vaadin.flow.internal.StateNode;
4041
import com.vaadin.flow.internal.nodefeature.ClientCallableHandlers;
@@ -285,15 +286,9 @@ private static Object decodeArg(Component instance, Method method,
285286
// come up with method to know that it's an id and should be gotten from
286287
// the model
287288
assert argValue != null;
289+
288290
if (type.isPrimitive() && argValue.getType() == JsonType.NULL) {
289-
String msg = String.format(
290-
"Null values are not allowed for primitive types but "
291-
+ "a 'null' value was received for parameter %d "
292-
+ "which refers to primitive type '%s' "
293-
+ "in the method '%s' defined in the class '%s'",
294-
index, type.getName(), method.getName(),
295-
method.getDeclaringClass().getName());
296-
throw new IllegalArgumentException(msg);
291+
return JsonCodec.decodeAs(argValue, type);
297292
} else if (type.isArray()) {
298293
return decodeArray(method, type, index, argValue);
299294
} else {

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.List;
1919

20+
import net.jcip.annotations.NotThreadSafe;
2021
import org.junit.After;
2122
import org.junit.Assert;
2223
import org.junit.Before;
@@ -31,6 +32,7 @@
3132
import com.vaadin.flow.component.UI;
3233
import com.vaadin.flow.component.internal.PendingJavaScriptInvocation;
3334
import com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation;
35+
import com.vaadin.flow.component.polymertemplate.EventHandler;
3436
import com.vaadin.flow.dom.DisabledUpdateMode;
3537
import com.vaadin.flow.server.MockServletServiceSessionSetup;
3638
import com.vaadin.flow.server.VaadinService;
@@ -42,7 +44,6 @@
4244
import elemental.json.JsonArray;
4345
import elemental.json.JsonObject;
4446
import elemental.json.JsonValue;
45-
import net.jcip.annotations.NotThreadSafe;
4647

4748
@NotThreadSafe
4849
public class PublishedServerEventHandlerRpcHandlerTest {
@@ -134,6 +135,7 @@ protected void varArgMethod(@EventData("foo") String... args) {
134135
public static class MethodWithParameters extends ComponentWithCompute {
135136

136137
private int intArg;
138+
private boolean booleanArg;
137139
private String strArg;
138140
private boolean[] arrayArg;
139141
private Double[] doubleArg;
@@ -147,6 +149,11 @@ protected void intMethod(@EventData("foo") int i) {
147149
intArg = i;
148150
}
149151

152+
@EventHandler
153+
protected void booleanMethod(@EventData("foo") boolean value) {
154+
booleanArg = value;
155+
}
156+
150157
@ClientCallable
151158
protected void method1(@EventData("foo") String str,
152159
@EventData("bar") boolean[] array) {
@@ -418,6 +425,24 @@ public void methodWithVarArg_arrayIsCorrectlyHandled() {
418425
component.varArg);
419426
}
420427

428+
@Test
429+
public void nullValueAreAcceptedForPrimitive() {
430+
JsonArray array = Json.createArray();
431+
array.set(0, Json.createNull());
432+
MethodWithParameters component = new MethodWithParameters();
433+
component.intArg = -1;
434+
component.booleanArg = true;
435+
PublishedServerEventHandlerRpcHandler.invokeMethod(component,
436+
component.getClass(), "intMethod", array, -1);
437+
438+
Assert.assertEquals(0, component.intArg);
439+
440+
PublishedServerEventHandlerRpcHandler.invokeMethod(component,
441+
component.getClass(), "booleanMethod", array, -1);
442+
443+
Assert.assertFalse(component.booleanArg);
444+
}
445+
421446
@Test(expected = IllegalStateException.class)
422447
public void noClientCallableMethodException() {
423448
ComponentWithNoClientCallableMethod component = new ComponentWithNoClientCallableMethod();

0 commit comments

Comments
 (0)