Skip to content

Commit

Permalink
Merge pull request #24467 from michalszynkiewicz/uni-cs-multipart-res…
Browse files Browse the repository at this point in the history
…ponses

Support Uni and CompletionStage results for multipart responses in REST Client Reactive
  • Loading branch information
gsmet authored Mar 22, 2022
2 parents c00a130 + f5fa2c3 commit 7429026
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1014,12 +1014,18 @@ private void addResponseTypeIfMultipart(Set<ClassInfo> multipartResponseTypes, M

private ClassInfo returnTypeAsClass(MethodInfo jandexMethod, IndexView index) {
Type result = jandexMethod.returnType();
if (result.kind() == CLASS) {
if (result.kind() == PARAMETERIZED_TYPE) {
if (result.name().equals(COMPLETION_STAGE) || result.name().equals(UNI)) {
Type firstArgument = result.asParameterizedType().arguments().get(0);
if (firstArgument.kind() == CLASS) {
return index.getClassByName(firstArgument.asClassType().name());
}
}
} else if (result.kind() == CLASS) {
return index.getClassByName(result.asClassType().name());
} else {
throw new IllegalArgumentException("multipart responses can only be mapped to non-generic classes, " +
"got " + result + " of type: " + result.kind());
}
throw new IllegalArgumentException("multipart responses can only be mapped to non-generic classes, " +
"got " + result + " of type: " + result.kind());
}

private void handleSubResourceMethod(List<JaxrsClientReactiveEnricherBuildItem> enrichers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
Expand All @@ -28,6 +33,7 @@

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.smallrye.mutiny.Uni;

public class MultipartResponseTest {

Expand All @@ -52,6 +58,34 @@ void shouldParseMultipartResponse() {
assertThat(data.numberz).containsSequence(2008, 2011, 2014);
}

@Test
void shouldParseUniMultipartResponse() {
Client client = RestClientBuilder.newBuilder().baseUri(baseUri).build(Client.class);
MultipartData data = client.uniGetFile().await().atMost(Duration.ofSeconds(10));
assertThat(data.file).exists();
verifyWooHooFile(data.file, 10000);
assertThat(data.name).isEqualTo("foo");
assertThat(data.panda.weight).isEqualTo("huge");
assertThat(data.panda.height).isEqualTo("medium");
assertThat(data.panda.mood).isEqualTo("happy");
assertThat(data.number).isEqualTo(1984);
assertThat(data.numberz).containsSequence(2008, 2011, 2014);
}

@Test
void shouldParseCompletionStageMultipartResponse() throws ExecutionException, InterruptedException, TimeoutException {
Client client = RestClientBuilder.newBuilder().baseUri(baseUri).build(Client.class);
MultipartData data = client.csGetFile().toCompletableFuture().get(10, TimeUnit.SECONDS);
assertThat(data.file).exists();
verifyWooHooFile(data.file, 10000);
assertThat(data.name).isEqualTo("foo");
assertThat(data.panda.weight).isEqualTo("huge");
assertThat(data.panda.height).isEqualTo("medium");
assertThat(data.panda.mood).isEqualTo("happy");
assertThat(data.number).isEqualTo(1984);
assertThat(data.numberz).containsSequence(2008, 2011, 2014);
}

@Test
void shouldParseMultipartResponseWithSmallFile() {
Client client = RestClientBuilder.newBuilder().baseUri(baseUri).build(Client.class);
Expand Down Expand Up @@ -143,6 +177,14 @@ public interface Client {
@Produces(MediaType.MULTIPART_FORM_DATA)
@Path("/error")
MultipartData error();

@GET
@Produces(MediaType.MULTIPART_FORM_DATA)
Uni<MultipartData> uniGetFile();

@GET
@Produces(MediaType.MULTIPART_FORM_DATA)
CompletionStage<MultipartData> csGetFile();
}

@Path("/give-me-file")
Expand Down

0 comments on commit 7429026

Please sign in to comment.