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

Jersey Microprofile RestClient - NullPointerException when a optional FormParam is null #5254

Closed
wagonli opened this issue Feb 7, 2023 · 1 comment

Comments

@wagonli
Copy link

wagonli commented Feb 7, 2023

Currently using Jersey 2.35 (does not seem fixed in current release 3.1.1) we're encountering an unexpected behavior while using Jersey's Microprofile RestClient implementation. We're trying to issue requests to an enpoint accepting nullable application/x-www-form-urlencoded parameters and we're getting a NullPointerException each time we're trying to pass a null (@FormParam) parameter.

The issue seems to be caused by org.glassfish.jersey.microprofile.restclient.FormParamModel class that resolve the parameter value as Object and tries to call resolvedValue.toString() without a null-check.

Here is our setup :

Given the following resource :

@Path("/resource")
public interface MyResource {
    @POST
    @Consumes(APPLICATION_FORM_URLENCODED)
    @Produces(APPLICATION_JSON)
    void endpoint(
            @FormParam("param")
            String param
    );

The resulting client throws an exception when called with null value for param :

RestClientBuilder.newBuilder()
                               .register(JACKSON_CONTEXT_RESOLVER)
                               .register(JACKSON_JSON_PROVIDER, MessageBodyReader.class, MessageBodyWriter.class)
                               .baseUri(URI.create(MY8SERVICE_URL))
                               .build(MyResource.class)
                               .endpoint(null);

Resulting exception :
java.lang.RuntimeException: java.lang.NullPointerException: Cannot invoke "Object.toString()" because "resolvedValue" is null

Caused by: java.lang.NullPointerException: Cannot invoke "Object.toString()" because "resolvedValue" is null
	at org.glassfish.jersey.microprofile.restclient.FormParamModel.handleParameter(FormParamModel.java:48)
	at org.glassfish.jersey.microprofile.restclient.FormParamModel.handleParameter(FormParamModel.java:31)
	at org.glassfish.jersey.microprofile.restclient.MethodModel.lambda$handleForm$4(MethodModel.java:213)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
	at org.glassfish.jersey.microprofile.restclient.MethodModel.handleForm(MethodModel.java:213)
	at org.glassfish.jersey.microprofile.restclient.MethodModel.invokeMethod(MethodModel.java:172)
	at org.glassfish.jersey.microprofile.restclient.InterceptorInvocationContext.proceed(InterceptorInvocationContext.java:110)
	at org.glassfish.jersey.microprofile.restclient.RestClientModel.invokeMethod(RestClientModel.java:69)
	at org.glassfish.jersey.microprofile.restclient.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:70)
	...
jbescos added a commit to jbescos/jersey that referenced this issue Feb 9, 2023
… FormParam is null eclipse-ee4j#5254

Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
jbescos added a commit to jbescos/jersey that referenced this issue Feb 9, 2023
… FormParam is null eclipse-ee4j#5254

Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
senivam pushed a commit that referenced this issue Feb 16, 2023
… FormParam is null #5254

Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
@wagonli
Copy link
Author

wagonli commented Apr 25, 2023

Issue has been fixed

@wagonli wagonli closed this as completed Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant