Skip to content

Commit 1badc22

Browse files
committed
Fix generated openapi schema for CompletableFuture response body
Note: Not detecting openapi array vs object there though
1 parent 4ab9bad commit 1badc22

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ build/
1111
*/bin/
1212
*.editorconfig
1313
*.Module
14+
dependency-reduced-pom.xml

http-generator-core/src/main/java/io/avaje/http/generator/core/openapi/SchemaDocBuilder.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.lang.model.util.Types;
2323

2424
import io.avaje.http.generator.core.HiddenPrism;
25+
import io.avaje.http.generator.core.UType;
2526
import io.avaje.http.generator.core.Util;
2627
import io.avaje.prism.GeneratePrism;
2728
import io.swagger.v3.oas.models.Operation;
@@ -43,6 +44,7 @@ class SchemaDocBuilder {
4344
private static final String APP_FORM = "application/x-www-form-urlencoded";
4445
private static final String APP_JSON = "application/json";
4546

47+
private final Elements elements;
4648
private final Types types;
4749
private final KnownTypes knownTypes;
4850
private final TypeMirror iterableType;
@@ -52,6 +54,7 @@ class SchemaDocBuilder {
5254

5355
SchemaDocBuilder(Types types, Elements elements) {
5456
this.types = types;
57+
this.elements = elements;
5558
this.knownTypes = new KnownTypes();
5659
this.iterableType = types.erasure(elements.getTypeElement("java.lang.Iterable").asType());
5760
this.mapType = types.erasure(elements.getTypeElement("java.util.Map").asType());
@@ -112,7 +115,6 @@ void addRequestBody(Operation operation, Schema schema, boolean asForm, String d
112115
}
113116

114117
private RequestBody requestBody(Operation operation) {
115-
116118
RequestBody body = operation.getRequestBody();
117119
if (body == null) {
118120
body = new RequestBody();
@@ -125,7 +127,15 @@ private RequestBody requestBody(Operation operation) {
125127
}
126128

127129
Schema<?> toSchema(TypeMirror type) {
128-
130+
UType uType = UType.parse(type);
131+
if (uType.mainType().equals("java.util.concurrent.CompletableFuture")) {
132+
UType bodyType = uType.paramRaw();
133+
if (bodyType.isGeneric()) { // Container type like List, Set etc
134+
bodyType = bodyType.paramRaw();
135+
}
136+
TypeElement typeElement = elements.getTypeElement(bodyType.full());
137+
type = typeElement.asType();
138+
}
129139
Schema<?> schema = knownTypes.createSchema(typeDef(type));
130140
if (schema != null) {
131141
return schema;

http-generator-core/src/test/java/io/avaje/http/generator/core/UtilTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ void parse_CompletableFutureListBean() {
122122

123123
assertThat(type.importTypes()).containsExactly("java.util.concurrent.CompletableFuture", "java.util.List", "org.example.Repo");
124124
assertThat(type.shortType()).isEqualTo("CompletableFuture<List<Repo>>");
125+
126+
assertThat(type.mainType()).isEqualTo("java.util.concurrent.CompletableFuture");
127+
UType param = type.paramRaw();
128+
assertThat(param.isGeneric()).isTrue();
129+
assertThat(param.full()).isEqualTo("java.util.List<org.example.Repo>");
130+
UType uType = param.paramRaw();
131+
assertThat(uType.full()).isEqualTo("org.example.Repo");
125132
}
126133

127134
@Test

tests/test-javalin-jsonb/src/main/resources/public/openapi.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,27 @@
324324
}
325325
}
326326
},
327+
"/hello/async" : {
328+
"get" : {
329+
"tags" : [
330+
331+
],
332+
"summary" : "",
333+
"description" : "",
334+
"responses" : {
335+
"200" : {
336+
"description" : "",
337+
"content" : {
338+
"application/json" : {
339+
"schema" : {
340+
"$ref" : "#/components/schemas/HelloDto"
341+
}
342+
}
343+
}
344+
}
345+
}
346+
}
347+
},
327348
"/hello/findbyname/{name}" : {
328349
"get" : {
329350
"tags" : [

tests/test-javalin/src/main/resources/public/openapi.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,27 @@
314314
}
315315
}
316316
},
317+
"/hello/async" : {
318+
"get" : {
319+
"tags" : [
320+
321+
],
322+
"summary" : "",
323+
"description" : "",
324+
"responses" : {
325+
"200" : {
326+
"description" : "",
327+
"content" : {
328+
"application/json" : {
329+
"schema" : {
330+
"$ref" : "#/components/schemas/HelloDto"
331+
}
332+
}
333+
}
334+
}
335+
}
336+
}
337+
},
317338
"/hello/findbyname/{name}" : {
318339
"get" : {
319340
"tags" : [

0 commit comments

Comments
 (0)