Skip to content

Commit 9b9778c

Browse files
committed
Fix to SchemaDocBuilder to support CompletableFuture response
1 parent 1badc22 commit 9b9778c

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

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

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class SchemaDocBuilder {
4949
private final KnownTypes knownTypes;
5050
private final TypeMirror iterableType;
5151
private final TypeMirror mapType;
52+
private final TypeMirror completableFutureType;
5253

5354
private final Map<String, Schema> schemas = new TreeMap<>();
5455

@@ -58,6 +59,7 @@ class SchemaDocBuilder {
5859
this.knownTypes = new KnownTypes();
5960
this.iterableType = types.erasure(elements.getTypeElement("java.lang.Iterable").asType());
6061
this.mapType = types.erasure(elements.getTypeElement("java.util.Map").asType());
62+
this.completableFutureType = types.erasure(elements.getTypeElement("java.util.concurrent.CompletableFuture").asType());
6163
}
6264

6365
Map<String, Schema> getSchemas() {
@@ -82,7 +84,6 @@ void addFormParam(Operation operation, String varName, Schema schema) {
8284
}
8385

8486
private Schema requestFormParamSchema(RequestBody body) {
85-
8687
final Content content = body.getContent();
8788
MediaType mediaType = content.get(APP_FORM);
8889

@@ -103,7 +104,6 @@ private Schema requestFormParamSchema(RequestBody body) {
103104
* Add as request body.
104105
*/
105106
void addRequestBody(Operation operation, Schema schema, boolean asForm, String description) {
106-
107107
RequestBody body = requestBody(operation);
108108
body.setDescription(description);
109109

@@ -126,15 +126,14 @@ private RequestBody requestBody(Operation operation) {
126126
return body;
127127
}
128128

129+
private static TypeMirror typeArgument(TypeMirror type) {
130+
List<? extends TypeMirror> typeArguments = ((DeclaredType) type).getTypeArguments();
131+
return typeArguments.get(0);
132+
}
133+
129134
Schema<?> toSchema(TypeMirror type) {
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();
135+
if (types.isAssignable(type, completableFutureType)) {
136+
type = typeArgument(type);
138137
}
139138
Schema<?> schema = knownTypes.createSchema(typeDef(type));
140139
if (schema != null) {
@@ -143,20 +142,16 @@ Schema<?> toSchema(TypeMirror type) {
143142
if (types.isAssignable(type, mapType)) {
144143
return buildMapSchema(type);
145144
}
146-
147145
if (type.getKind() == TypeKind.ARRAY) {
148146
return buildArraySchema(type);
149147
}
150-
151148
if (types.isAssignable(type, iterableType)) {
152149
return buildIterableSchema(type);
153150
}
154-
155151
return buildObjectSchema(type);
156152
}
157153

158154
private Schema<?> buildObjectSchema(TypeMirror type) {
159-
160155
String objectSchemaKey = getObjectSchemaName(type);
161156

162157
Schema objectSchema = schemas.get(objectSchemaKey);
@@ -173,7 +168,6 @@ private Schema<?> buildObjectSchema(TypeMirror type) {
173168
}
174169

175170
private Schema<?> buildIterableSchema(TypeMirror type) {
176-
177171
Schema<?> itemSchema = new ObjectSchema().format("unknownIterableType");
178172

179173
if (type.getKind() == TypeKind.DECLARED) {
@@ -189,7 +183,6 @@ private Schema<?> buildIterableSchema(TypeMirror type) {
189183
}
190184

191185
private Schema<?> buildArraySchema(TypeMirror type) {
192-
193186
ArrayType arrayType = (ArrayType) type;
194187
Schema<?> itemSchema = toSchema(arrayType.getComponentType());
195188

@@ -199,7 +192,6 @@ private Schema<?> buildArraySchema(TypeMirror type) {
199192
}
200193

201194
private Schema<?> buildMapSchema(TypeMirror type) {
202-
203195
Schema<?> valueSchema = new ObjectSchema().format("unknownMapValueType");
204196

205197
if (type.getKind() == TypeKind.DECLARED) {
@@ -216,7 +208,6 @@ private Schema<?> buildMapSchema(TypeMirror type) {
216208
}
217209

218210
private String getObjectSchemaName(TypeMirror type) {
219-
220211
var canonicalName = Util.trimAnnotations(type.toString());
221212
final var pos = canonicalName.lastIndexOf('.');
222213
if (pos > -1) {
@@ -246,7 +237,6 @@ private void setFormatFromValidation(Element element, Schema<?> propSchema) {
246237
}
247238

248239
private void setLengthMinMax(Element element, Schema<?> propSchema) {
249-
250240
SizePrism.getOptionalOn(element)
251241
.ifPresent(
252242
size -> {
@@ -279,7 +269,6 @@ private boolean isNotNullable(Element element) {
279269
* Gather all the fields (properties) for the given bean element.
280270
*/
281271
private List<VariableElement> allFields(Element element) {
282-
283272
List<VariableElement> list = new ArrayList<>();
284273
gatherProperties(list, element);
285274
return list;
@@ -289,7 +278,6 @@ private List<VariableElement> allFields(Element element) {
289278
* Recursively gather all the fields (properties) for the given bean element.
290279
*/
291280
private void gatherProperties(List<VariableElement> fields, Element element) {
292-
293281
if (element == null) {
294282
return;
295283
}
@@ -314,7 +302,6 @@ private boolean ignoreField(VariableElement field) {
314302
}
315303

316304
private boolean isHiddenField(VariableElement field) {
317-
318305
if (HiddenPrism.getOptionalOn(field).isPresent()) {
319306
return true;
320307
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,10 @@
337337
"content" : {
338338
"application/json" : {
339339
"schema" : {
340-
"$ref" : "#/components/schemas/HelloDto"
340+
"items" : {
341+
"$ref" : "#/components/schemas/HelloDto"
342+
},
343+
"type" : "array"
341344
}
342345
}
343346
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,10 @@
327327
"content" : {
328328
"application/json" : {
329329
"schema" : {
330-
"$ref" : "#/components/schemas/HelloDto"
330+
"items" : {
331+
"$ref" : "#/components/schemas/HelloDto"
332+
},
333+
"type" : "array"
331334
}
332335
}
333336
}

0 commit comments

Comments
 (0)