From a542fa9f82b75b0cc4547fc221d5be180b7d3af3 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Thu, 9 Feb 2023 11:30:31 +0100 Subject: [PATCH] Jersey Microprofile RestClient - NullPointerException when a optional FormParam is null #5254 Signed-off-by: Jorge Bescos Gascon --- .../jersey/microprofile/restclient/FormParamModel.java | 4 ++-- .../jersey/restclient/ApplicationResource.java | 9 ++++++++- .../jersey/restclient/ApplicationResourceImpl.java | 7 ++++++- .../jersey/restclient/RestClientModelTest.java | 10 +++++++++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/FormParamModel.java b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/FormParamModel.java index a8255b9db6..f33b592600 100644 --- a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/FormParamModel.java +++ b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/FormParamModel.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -45,7 +45,7 @@ Form handleParameter(Form form, Class annotationClass, Obj form.param(formParamName, v.toString()); } } else { - form.param(formParamName, resolvedValue.toString()); + form.param(formParamName, resolvedValue == null ? null : resolvedValue.toString()); } return form; } diff --git a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java index ede06ddfa9..a781d45d53 100644 --- a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java +++ b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -21,6 +21,7 @@ import javax.json.JsonValue; import javax.ws.rs.Consumes; +import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.POST; @@ -76,4 +77,10 @@ default String sayHi() { @Path("content1/{content1}/content0/{content0: [0-9]{4}}") @Produces(MediaType.TEXT_PLAIN) String regex0(@PathParam("content1") String context0, @PathParam("content0") String context1); + + @POST + @Path("formParam") + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + @Produces(MediaType.TEXT_PLAIN) + String formParam(@FormParam("param") String param); } diff --git a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java index bb58e0577d..d098f63f97 100644 --- a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java +++ b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -71,4 +71,9 @@ public String regex(String content) { public String regex0(String context0, String context1) { return context0 + "_" + context1; } + + @Override + public String formParam(String param) { + return param; + } } diff --git a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/RestClientModelTest.java b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/RestClientModelTest.java index 07189c690e..737d1099f2 100644 --- a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/RestClientModelTest.java +++ b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/RestClientModelTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -56,4 +56,12 @@ public void testGetIt() throws URISyntaxException { assertEquals("Hi", app.sayHi()); } + + @Test + public void testFormParam() throws URISyntaxException { + String response = RestClientBuilder.newBuilder() + .baseUri(new URI("http://localhost:9998")) + .build(ApplicationResource.class).formParam(null); + assertEquals("", response); + } }