Skip to content

Commit 4fa12eb

Browse files
authored
Merge pull request #160 from LoonyRules/ISSUE-158
ISSUE-158: Fix CompletableFuture jsonb generation
2 parents e369fb3 + 32e9879 commit 4fa12eb

File tree

3 files changed

+16
-25
lines changed

3 files changed

+16
-25
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ public static Map<String, UType> jsonTypes(ControllerReader reader) {
3333
methodReader -> {
3434
addJsonBodyType(methodReader, addToMap);
3535
if (!methodReader.isVoid()) {
36-
addToMap.accept(UType.parse(methodReader.returnType()));
36+
UType uType = UType.parse(methodReader.returnType());
37+
38+
if (uType.mainType().equals("java.util.concurrent.CompletableFuture")) {
39+
uType = uType.paramRaw();
40+
}
41+
42+
addToMap.accept(uType);
3743
}
3844
});
3945

@@ -50,12 +56,6 @@ private static void addJsonBodyType(MethodReader methodReader, Consumer<UType> a
5056
}
5157

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

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,6 @@ private void writeClassStart() {
8888
}
8989

9090
for (UType type : jsonTypes.values()) {
91-
// Support for CompletableFuture's.
92-
if (type.mainType().equals("java.util.concurrent.CompletableFuture")) {
93-
type = type.paramRaw();
94-
95-
if (this.jsonTypes.containsKey(type.full())) {
96-
// Already written before -- we can skip.
97-
continue;
98-
}
99-
}
100-
101-
// Everything else
10291
final var typeString = PrimitiveUtil.wrap(type.shortType()).replace(",", ", ");
10392
writer.append(" private final JsonType<%s> %sJsonType;", typeString, type.shortName()).eol();
10493
}
@@ -117,13 +106,7 @@ private void writeClassStart() {
117106
writer.append(" this.validator = validator;").eol();
118107
}
119108
if (useJsonB) {
120-
for (final UType type : jsonTypes.values()) {
121-
// Skip trying to assign a global variable value for any UType that is a Completable Future. Because the paramRaw() should
122-
// already be in this jsonTypes map anyway and write the assignment all by itself.
123-
if (type.mainType().equals("java.util.concurrent.CompletableFuture")) {
124-
continue;
125-
}
126-
109+
for (UType type : jsonTypes.values()) {
127110
JsonBUtil.writeJsonbType(type, writer);
128111
}
129112
}

tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/test/TestController.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import java.util.List;
44
import java.util.Map;
55
import java.util.Set;
6+
import java.util.concurrent.CompletableFuture;
67

78
import org.example.myapp.web.AppRoles;
9+
import org.example.myapp.web.HelloDto;
810
import org.example.myapp.web.Roles;
911

1012
import io.avaje.http.api.Controller;
@@ -122,4 +124,10 @@ void neo(
122124

123125
System.out.println("Ever have that feeling where you're not sure if you're awake or dreaming?");
124126
}
127+
128+
@Get("/async")
129+
CompletableFuture<HelloDto> getAllAsync() {
130+
return CompletableFuture.supplyAsync(() -> new HelloDto(12, "Jim", "asd"));
131+
}
132+
125133
}

0 commit comments

Comments
 (0)