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

Fix CompletableFuture responses without body #10953

Merged
merged 1 commit into from
Jul 6, 2024
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
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
Loading