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

Support CompletionStage<Response> #5105

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2022 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
Expand Down Expand Up @@ -85,6 +85,7 @@ public class ResourceMethodInvoker implements Endpoint, ResourceInfo {
private final Type invocableResponseType;
private final boolean canUseInvocableResponseType;
private final boolean isCompletionStageResponseType;
private final boolean isCompletionStageResponseResponseType; // CompletionStage<Response>
private final Type completionStageResponseType;
private final ResourceMethodDispatcher dispatcher;
private final Method resourceMethod;
Expand Down Expand Up @@ -313,6 +314,8 @@ protected void configure() {
&& CompletionStage.class.isAssignableFrom((Class<?>) ((ParameterizedType) invocableResponseType).getRawType());
this.completionStageResponseType =
isCompletionStageResponseType ? ((ParameterizedType) invocableResponseType).getActualTypeArguments()[0] : null;
this.isCompletionStageResponseResponseType = Class.class.isInstance(completionStageResponseType)
&& Response.class.isAssignableFrom((Class<?>) completionStageResponseType);
}

private <T> void addNameBoundProviders(
Expand Down Expand Up @@ -465,7 +468,7 @@ private Response invoke(final RequestProcessingContext context, final Object res
if (canUseInvocableResponseType
&& response.hasEntity()
&& !(response.getEntityType() instanceof ParameterizedType)) {
response.setEntityType(unwrapInvocableResponseType(context.request()));
response.setEntityType(unwrapInvocableResponseType(context.request(), response.getEntityType()));
}

return response;
Expand All @@ -484,10 +487,10 @@ private Response invoke(final RequestProcessingContext context, final Object res
return jaxrsResponse;
}

private Type unwrapInvocableResponseType(ContainerRequest request) {
private Type unwrapInvocableResponseType(ContainerRequest request, Type entityType) {
if (isCompletionStageResponseType
&& request.resolveProperty(ServerProperties.UNWRAP_COMPLETION_STAGE_IN_WRITER_ENABLE, Boolean.FALSE)) {
return completionStageResponseType;
return isCompletionStageResponseResponseType ? entityType : completionStageResponseType;
}
return invocableResponseType;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
<artifactId>jersey-media-json-processing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022 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
Expand All @@ -18,6 +18,7 @@

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.ServerRuntime;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

Expand Down Expand Up @@ -64,7 +65,8 @@ public class CompletionStageTest extends JerseyTest {

@Override
protected Application configure() {
return new ResourceConfig(CompletionStageResource.class, DataBeanWriter.class);
return new ResourceConfig(CompletionStageResource.class, DataBeanWriter.class)
.property(ServerProperties.UNWRAP_COMPLETION_STAGE_IN_WRITER_ENABLE, Boolean.TRUE);
}

@Test
Expand Down Expand Up @@ -104,6 +106,14 @@ public void testGetCompletedAsync() {
assertThat(response.readEntity(String.class), is(ENTITY));
}

@Test
public void testGetCompletedAsyncResponse() {
Response response = target("cs/completedAsyncResponse").request().get();

assertThat(response.getStatus(), is(200));
assertThat(response.readEntity(List.class).get(0), is(ENTITY));
}

@Test
public void testGetException400Async() {
Response response = target("cs/exception400Async").request().get();
Expand Down Expand Up @@ -213,6 +223,14 @@ public CompletionStage<String> getCompletedAsync() {
return cs;
}

@GET
@Path("/completedAsyncResponse")
public CompletionStage<Response> getCompletedAsyncResponse() {
CompletableFuture<Response> cs = new CompletableFuture<>();
delaySubmit(() -> cs.complete(Response.ok().entity(Collections.singletonList(ENTITY)).build()));
return cs;
}

@GET
@Path("/exception400Async")
public CompletionStage<String> getException400Async() {
Expand Down