Skip to content

Commit

Permalink
Return a null InputStream from REST Client when response is 204
Browse files Browse the repository at this point in the history
This is similar to what RESTEasy Classic does

Fixes: #31231
  • Loading branch information
geoand committed Feb 20, 2023
1 parent afcb8c8 commit 2155da4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.quarkus.rest.client.reactive;

import static io.quarkus.rest.client.reactive.RestClientTestUtil.setUrlForClass;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.InputStream;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class NoContentResponseTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(Client.class, Resource.class)
.addAsResource(
new StringAsset(setUrlForClass(Client.class)),
"application.properties"));

@RestClient
Client client;

@Test
public void testGetStreamNoContent() {
assertNull(client.getStream());
}

@Test
public void testGetResponseNoContent() {
Response response = client.getResponse();
assertFalse(response.hasEntity());
assertNull(response.getEntity());
}

@Path("/test")
@RegisterRestClient
@Produces(MediaType.TEXT_PLAIN)
interface Client {
@GET
InputStream getStream();

@GET
Response getResponse();
}

@Path("/test")
static class Resource {
@GET
public Response get() {
return Response.noContent().build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.StatusType;

import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.client.api.WebClientApplicationException;
import org.jboss.resteasy.reactive.client.impl.ClientRequestContextImpl;
import org.jboss.resteasy.reactive.client.impl.ClientResponseContextImpl;
Expand Down Expand Up @@ -36,6 +37,12 @@ public void handle(RestClientRequestContext context) throws Exception {
// so we have to write it, but without filters/interceptors
if (isAbortedWith(requestContext)) {
propagateAbortedWithEntityToResponse(context);
} else {
StatusType effectiveResponseStatus = determineEffectiveResponseStatus(context, requestContext);
if ((effectiveResponseStatus.getStatusCode() == RestResponse.Status.NO_CONTENT.getStatusCode())
&& (requestContext != null && !requestContext.hasEntity())) {
context.setResponseEntityStream(null);
}
}
}

Expand Down

0 comments on commit 2155da4

Please sign in to comment.