Skip to content

Commit

Permalink
Fix CompletableFuture responses without body (micronaut-projects#10953)
Browse files Browse the repository at this point in the history
  • Loading branch information
yawkat authored Jul 6, 2024
1 parent a90b37a commit a79c389
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
import io.micronaut.http.tck.AssertionUtils;
import io.micronaut.http.tck.BodyAssertion;
import io.micronaut.http.tck.HttpResponseAssertion;
import io.micronaut.http.tck.TestScenario;
import org.junit.jupiter.api.Test;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -168,6 +170,20 @@ void testEmptySingleResultPublisher() throws IOException {
.build()));
}

@Test
void testRedirectFuture() throws IOException {
TestScenario.builder()
.specName(SPEC_NAME)
.request(HttpRequest.GET("/response-body/redirect-future"))
.configuration(Map.of("micronaut.http.client.follow-redirects", false))
.assertion((server, request) -> AssertionUtils.assertDoesNotThrow(server, request,
HttpResponseAssertion.builder()
.status(HttpStatus.FOUND)
.header("Location", "https://example.com")
.build()))
.run();
}

@Controller("/response-body")
@Requires(property = "spec.name", value = SPEC_NAME)
static class BodyController {
Expand Down Expand Up @@ -232,6 +248,11 @@ CompletableFuture<HttpResponse<?>> emptyFuture() {
Publisher<HttpResponse<?>> emptySingleResult() {
return Mono.just(HttpResponse.ok());
}

@Get(uri = "/redirect-future")
CompletableFuture<HttpResponse<?>> redirectFuture() {
return CompletableFuture.completedFuture(HttpResponse.status(HttpStatus.FOUND).header("Location", "https://example.com"));
}
}

@Introspected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,8 @@ private CompletionStage<MutableHttpResponse<?>> fromCompletionStage(@NonNull Htt
mutableResponse = forStatus(routeInfo, null)
.body(asyncBody);
}
if (mutableResponse.body() == null) {
if (routeInfo.isVoid() || explicitResponse) {
if (mutableResponse.body() == null && !explicitResponse) {
if (routeInfo.isVoid()) {
return CompletableFuture.completedStage(voidResponse(routeInfo));
}
return CompletableFuture.completedStage(newNotFoundError(request));
Expand Down

0 comments on commit a79c389

Please sign in to comment.