eclipse-ee4j / eclipse-ee4j/jersey

JSON-Jackson does not handle resource returning CompletionStage<X>

Open
#3,672 8 comments 3 reactions 0 assignees View on GitHub
Component: media
Dominant language
Java
Stars
730
Forks
382
PR merge metrics
No merged PRs in 30d

Description

It seems like `jersey-media-json-jackson` is not up to date with the latest JAX-RS 2.1 features.
I tried creating the following resource method:

@GET
@Produces({"application/json" })
@Path("async")
public CompletionStage asyncHello(@NotNull @PathParam(value = "lang") String lang,
@NotNull @Size(min = 2) @QueryParam("yo") String greet){
return CompletableFuture.supplyAsync(() -> new Hello(lang, greet);
}

This fails with stack trace

```
java.lang.IllegalArgumentException: Class org.kantega.reststop.helloworld.jaxrs.Hello not subtype of [simple type, class java.util.concurrent.CompletionStage]
at com.fasterxml.jackson.databind.type.TypeFactory.constructSpecializedType(TypeFactory.java:359)
at org.glassfish.jersey.jackson.internal.jackson.jaxrs.base.ProviderBase.writeTo(ProviderBase.java:624)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:266)
```

Returning a plain String with `text/plain` works as expected.

Just as a test I build my own version of Jersey with some additional code in `org.glassfish.jersey.jackson.internal.jackson.jaxrs.base.ProviderBase.writeTo(...)`:

TypeFactory typeFactory = writer.getTypeFactory();

String typeName = genericType.getTypeName();
if(typeName.contains(CompletionStage.class.getName())) {
Type[] actualTypeArguments = ((ParameterizedTypeImpl) genericType).getActualTypeArguments();
genericType = actualTypeArguments[0];
}

JavaType baseType = typeFactory.constructType(genericType);

The middle part unwrapping the CompletionStage is new.
This works.

So, have I missed something, or is the `CompletionStage` support not complete?

Contributor guide

Open the contributing guide

Research direction

Start with org.glassfish.jersey.jackson.internal.jackson.jaxrs.base.ProviderBase.writeTo(...) and reproduce the resource method returning CompletionStage with application/json. Compare the generic type handling with the plain String case; done means the Hello value is serialized successfully without the subtype exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.