Skip to content

Commit 59f5c87

Browse files
author
Haydon Perrin
committed
Added CompletableFuture return type support for Javalin using Context#future(...).
1 parent 7412e64 commit 59f5c87

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/JsonBUtil.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ private static void addJsonBodyType(MethodReader methodReader, Consumer<UType> a
5050
}
5151

5252
public static void writeJsonbType(UType type, Append writer) {
53+
// Support for CompletableFuture's.
54+
if (type.isGeneric() && type.mainType().equals("java.util.concurrent.CompletableFuture")) {
55+
writeJsonbType(type.paramRaw(), writer);
56+
return;
57+
}
58+
5359
writer.append(" this.%sJsonType = jsonB.type(", type.shortName());
5460
if (!type.isGeneric()) {
5561
writer.append("%s.class)", Util.shortName(PrimitiveUtil.wrap(type.full())));

http-generator-javalin/src/main/java/io/avaje/http/generator/javalin/ControllerMethodWriter.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,43 @@ void write(boolean requestScoped) {
8585
}
8686

8787
private void writeContextReturn() {
88+
// Support for CompletableFuture's.
89+
final UType type = UType.parse(method.returnType());
90+
if (type.isGeneric() && type.mainType().equals("java.util.concurrent.CompletableFuture")) {
91+
final String returnVariableName = "futureResult";
92+
93+
writer.append(" ctx.future(() -> {").eol();
94+
writer.append(" return result.thenAccept(%s -> {", returnVariableName).eol();
95+
writer.append(" ");
96+
this.writeContextReturn(returnVariableName);
97+
writer.eol().append(" });").eol();
98+
writer.append(" });").eol();
99+
return;
100+
}
101+
102+
// Everything else
103+
this.writeContextReturn("result");
104+
}
105+
106+
private void writeContextReturn(final String returnVariableName) {
88107
final var produces = method.produces();
89108
if (produces == null || MediaType.APPLICATION_JSON.equalsIgnoreCase(produces)) {
90109
if (useJsonB) {
91-
final var uType = UType.parse(method.returnType());
92-
writer.append(" %sJsonType.toJson(result, ctx.contentType(\"application/json\").outputStream());", uType.shortName());
110+
var uType = UType.parse(method.returnType());
111+
if (uType.isGeneric() && uType.mainType().equals("java.util.concurrent.CompletableFuture")) {
112+
uType = uType.paramRaw();
113+
}
114+
115+
writer.append(" %sJsonType.toJson(%s, ctx.contentType(\"application/json\").outputStream());", uType.shortName(), returnVariableName);
93116
} else {
94-
writer.append(" ctx.json(result);");
117+
writer.append(" ctx.json(%s);", returnVariableName);
95118
}
96119
} else if (MediaType.TEXT_HTML.equalsIgnoreCase(produces)) {
97-
writer.append(" ctx.html(result);");
120+
writer.append(" ctx.html(%s);", returnVariableName);
98121
} else if (MediaType.TEXT_PLAIN.equalsIgnoreCase(produces)) {
99-
writer.append(" ctx.contentType(\"text/plain\").result(result);");
122+
writer.append(" ctx.contentType(\"text/plain\").result(%s);", returnVariableName);
100123
} else {
101-
writer.append(" ctx.contentType(\"%s\").result(result);", produces);
124+
writer.append(" ctx.contentType(\"%s\").result(%s);", produces, returnVariableName);
102125
}
103126
}
104127
}

http-generator-javalin/src/main/java/io/avaje/http/generator/javalin/ControllerWriter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ private void writeClassStart() {
8787
writer.append(" private final Validator validator;").eol();
8888
}
8989

90-
for (final UType type : jsonTypes.values()) {
90+
for (UType type : jsonTypes.values()) {
91+
// Support for CompletableFuture's.
92+
if (type.isGeneric() && type.mainType().equals("java.util.concurrent.CompletableFuture")) {
93+
type = type.paramRaw();
94+
}
95+
96+
// Everything else
9197
final var typeString = PrimitiveUtil.wrap(type.shortType()).replace(",", ", ");
9298
writer.append(" private final JsonType<%s> %sJsonType;", typeString, type.shortName()).eol();
9399
}

0 commit comments

Comments
 (0)