Skip to content

Commit c2a483b

Browse files
Sebastien GREGOIREfrantuma
authored andcommitted
Add support of CompletionStage
1 parent ff161fd commit c2a483b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import java.util.Optional;
7070
import java.util.Set;
7171
import java.util.TreeSet;
72+
import java.util.concurrent.CompletionStage;
7273
import java.util.stream.Collectors;
7374

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

10731074
if (annotatedMethod != null && annotatedMethod.getType() != null) {
1074-
returnType = annotatedMethod.getType();
1075+
returnType = extractTypeFromMethod(annotatedMethod);
10751076
}
10761077

10771078
final Class<?> subResource = getSubResourceWithJaxRsSubresourceLocatorSpecs(method);
@@ -1133,6 +1134,14 @@ protected Operation parseMethod(
11331134
return operation;
11341135
}
11351136

1137+
private Type extractTypeFromMethod(AnnotatedMethod annotatedMethod) {
1138+
if(CompletionStage.class.isAssignableFrom(annotatedMethod.getType().getRawClass())) {
1139+
// CompletionStage's 1st generic type is the real return type.
1140+
return annotatedMethod.getType().getBindings().getBoundType(0);
1141+
}
1142+
return annotatedMethod.getType();
1143+
}
1144+
11361145
protected Content resolveEmptyContent(Produces classProduces, Produces methodProduces) {
11371146
Content content = new Content();
11381147
MediaType mediaType = new MediaType();

modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@
125125
import java.util.Map;
126126
import java.util.Optional;
127127
import java.util.Set;
128+
import java.util.concurrent.CompletableFuture;
129+
import java.util.concurrent.CompletionStage;
128130
import java.util.concurrent.CopyOnWriteArrayList;
131+
import java.util.stream.Collectors;
129132

130133
import static org.testng.Assert.assertEquals;
131134
import static org.testng.Assert.assertFalse;
@@ -717,6 +720,36 @@ public void test1(A a) {
717720
}
718721
}
719722

723+
@Test
724+
public void testClassWithCompletableFuture() {
725+
Reader reader = new Reader(new OpenAPI());
726+
OpenAPI openAPI = reader.read(ClassWithCompletableFuture.class);
727+
assertNotNull(openAPI);
728+
729+
assertEquals(
730+
openAPI.getPaths()
731+
.get("/myApi")
732+
.getGet()
733+
.getResponses()
734+
.get("default")
735+
.getContent()
736+
.get("application/json")
737+
.getSchema()
738+
.get$ref(),
739+
"#/components/schemas/Ret"
740+
);
741+
}
742+
743+
static class ClassWithCompletableFuture {
744+
@Path("/myApi")
745+
@Produces("application/json")
746+
@Consumes("application/json")
747+
@GET
748+
public CompletableFuture<Ret> myApi(A a) {
749+
return CompletableFuture.completedFuture(new Ret());
750+
}
751+
}
752+
720753
@Test(description = "test resource with array in response content")
721754
public void test2497() {
722755
Reader reader = new Reader(new OpenAPI());

0 commit comments

Comments
 (0)