Skip to content

ISSUE-158: Fix CompletableFuture jsonb generation #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public static Map<String, UType> jsonTypes(ControllerReader reader) {
methodReader -> {
addJsonBodyType(methodReader, addToMap);
if (!methodReader.isVoid()) {
addToMap.accept(UType.parse(methodReader.returnType()));
UType uType = UType.parse(methodReader.returnType());

if (uType.mainType().equals("java.util.concurrent.CompletableFuture")) {
uType = uType.paramRaw();
}

addToMap.accept(uType);
}
});

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

public static void writeJsonbType(UType type, Append writer) {
// Support for CompletableFuture's.
if (type.mainType().equals("java.util.concurrent.CompletableFuture")) {
writeJsonbType(type.paramRaw(), writer);
return;
}

writer.append(" this.%sJsonType = jsonB.type(", type.shortName());
if (!type.isGeneric()) {
writer.append("%s.class)", Util.shortName(PrimitiveUtil.wrap(type.full())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,6 @@ private void writeClassStart() {
}

for (UType type : jsonTypes.values()) {
// Support for CompletableFuture's.
if (type.mainType().equals("java.util.concurrent.CompletableFuture")) {
type = type.paramRaw();

if (this.jsonTypes.containsKey(type.full())) {
// Already written before -- we can skip.
continue;
}
}

// Everything else
final var typeString = PrimitiveUtil.wrap(type.shortType()).replace(",", ", ");
writer.append(" private final JsonType<%s> %sJsonType;", typeString, type.shortName()).eol();
}
Expand All @@ -117,13 +106,7 @@ private void writeClassStart() {
writer.append(" this.validator = validator;").eol();
}
if (useJsonB) {
for (final UType type : jsonTypes.values()) {
// Skip trying to assign a global variable value for any UType that is a Completable Future. Because the paramRaw() should
// already be in this jsonTypes map anyway and write the assignment all by itself.
if (type.mainType().equals("java.util.concurrent.CompletableFuture")) {
continue;
}

for (UType type : jsonTypes.values()) {
JsonBUtil.writeJsonbType(type, writer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import org.example.myapp.web.AppRoles;
import org.example.myapp.web.HelloDto;
import org.example.myapp.web.Roles;

import io.avaje.http.api.Controller;
Expand Down Expand Up @@ -122,4 +124,10 @@ void neo(

System.out.println("Ever have that feeling where you're not sure if you're awake or dreaming?");
}

@Get("/async")
CompletableFuture<HelloDto> getAllAsync() {
return CompletableFuture.supplyAsync(() -> new HelloDto(12, "Jim", "asd"));
}

}