Skip to content

Commit

Permalink
Add support of CompletionStage
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien GREGOIRE authored and sgregoire committed Mar 6, 2023
1 parent ff161fd commit 9c95713
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;

public class Reader implements OpenApiReader {
Expand Down Expand Up @@ -1071,7 +1072,7 @@ protected Operation parseMethod(
Type returnType = method.getGenericReturnType();

if (annotatedMethod != null && annotatedMethod.getType() != null) {
returnType = annotatedMethod.getType();
returnType = extractTypeFromMethod(annotatedMethod);
}

final Class<?> subResource = getSubResourceWithJaxRsSubresourceLocatorSpecs(method);
Expand Down Expand Up @@ -1133,6 +1134,14 @@ protected Operation parseMethod(
return operation;
}

private Type extractTypeFromMethod(AnnotatedMethod annotatedMethod) {
if(CompletionStage.class.isAssignableFrom(annotatedMethod.getType().getRawClass())) {
// CompletionStage's 1st generic type is the real return type.
return annotatedMethod.getType().getBindings().getBoundType(0);
}
return annotatedMethod.getType();
}

protected Content resolveEmptyContent(Produces classProduces, Produces methodProduces) {
Content content = new Content();
MediaType mediaType = new MediaType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
Expand Down Expand Up @@ -717,6 +720,36 @@ public void test1(A a) {
}
}

@Test
public void testClassWithCompletableFuture() {
Reader reader = new Reader(new OpenAPI());
OpenAPI openAPI = reader.read(ClassWithCompletableFuture.class);
assertNotNull(openAPI);

assertEquals(
openAPI.getPaths()
.get("/myApi")
.getGet()
.getResponses()
.get("default")
.getContent()
.get("application/json")
.getSchema()
.get$ref(),
"#/components/schemas/Ret"
);
}

static class ClassWithCompletableFuture {
@Path("/myApi")
@Produces("application/json")
@Consumes("application/json")
@GET
public CompletableFuture<Ret> myApi(A a) {
return CompletableFuture.completedFuture(new Ret());
}
}

@Test(description = "test resource with array in response content")
public void test2497() {
Reader reader = new Reader(new OpenAPI());
Expand Down

0 comments on commit 9c95713

Please sign in to comment.