[Java][Play] Fix compilation issues when using the supportAsync option (#7864)

* fix async in java play generator

* add async operation option

* Remove the return null and replace with a return at the right place.

Co-authored-by: Jean-François Côté <jcote@stingray.com>
This commit is contained in:
William Cheng
2020-11-27 23:30:46 +08:00
committed by GitHub
parent 3f75691da2
commit 2801c0cb88
7 changed files with 132 additions and 47 deletions

View File

@@ -17,6 +17,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import openapitools.OpenAPIUtils;
import static play.mvc.Results.ok;
import play.api.libs.Files.TemporaryFile;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CompletableFuture;
@@ -28,20 +29,30 @@ public abstract class PetApiControllerImpInterface {
private ObjectMapper mapper = new ObjectMapper();
public CompletionStage<Result> addPetHttp(Http.Request request, Pet body) throws Exception {
return CompletableFuture.supplyAsync(() -> {
CompletableFuture<Result> result = CompletableFuture.supplyAsync(() -> {
try {
addPet(request, body);
return ok();
});
} catch (Exception e) {
throw new CompletionException(e);
}
return ok();
});
return result;
}
public abstract void addPet(Http.Request request, Pet body) throws Exception;
public CompletionStage<Result> deletePetHttp(Http.Request request, Long petId, String apiKey) throws Exception {
return CompletableFuture.supplyAsync(() -> {
CompletableFuture<Result> result = CompletableFuture.supplyAsync(() -> {
try {
deletePet(request, petId, apiKey);
return ok();
});
} catch (Exception e) {
throw new CompletionException(e);
}
return ok();
});
return result;
}
@@ -56,7 +67,7 @@ public abstract class PetApiControllerImpInterface {
}
return obj;
});
stage.thenApply(obj -> {
return stage.thenApply(obj -> {
JsonNode result = mapper.valueToTree(obj);
return ok(result);
});
@@ -74,7 +85,7 @@ stage.thenApply(obj -> {
}
return obj;
});
stage.thenApply(obj -> {
return stage.thenApply(obj -> {
JsonNode result = mapper.valueToTree(obj);
return ok(result);
});
@@ -90,7 +101,7 @@ stage.thenApply(obj -> {
}
return obj;
});
stage.thenApply(obj -> {
return stage.thenApply(obj -> {
JsonNode result = mapper.valueToTree(obj);
return ok(result);
});
@@ -100,20 +111,30 @@ stage.thenApply(obj -> {
public abstract CompletionStage<Pet> getPetById(Http.Request request, Long petId) throws Exception;
public CompletionStage<Result> updatePetHttp(Http.Request request, Pet body) throws Exception {
return CompletableFuture.supplyAsync(() -> {
CompletableFuture<Result> result = CompletableFuture.supplyAsync(() -> {
try {
updatePet(request, body);
return ok();
});
} catch (Exception e) {
throw new CompletionException(e);
}
return ok();
});
return result;
}
public abstract void updatePet(Http.Request request, Pet body) throws Exception;
public CompletionStage<Result> updatePetWithFormHttp(Http.Request request, Long petId, String name, String status) throws Exception {
return CompletableFuture.supplyAsync(() -> {
CompletableFuture<Result> result = CompletableFuture.supplyAsync(() -> {
try {
updatePetWithForm(request, petId, name, status);
return ok();
});
} catch (Exception e) {
throw new CompletionException(e);
}
return ok();
});
return result;
}
@@ -126,7 +147,7 @@ stage.thenApply(obj -> {
}
return obj;
});
stage.thenApply(obj -> {
return stage.thenApply(obj -> {
JsonNode result = mapper.valueToTree(obj);
return ok(result);
});

View File

@@ -16,6 +16,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import openapitools.OpenAPIUtils;
import static play.mvc.Results.ok;
import play.api.libs.Files.TemporaryFile;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CompletableFuture;
@@ -27,10 +28,15 @@ public abstract class StoreApiControllerImpInterface {
private ObjectMapper mapper = new ObjectMapper();
public CompletionStage<Result> deleteOrderHttp(Http.Request request, String orderId) throws Exception {
return CompletableFuture.supplyAsync(() -> {
CompletableFuture<Result> result = CompletableFuture.supplyAsync(() -> {
try {
deleteOrder(request, orderId);
return ok();
});
} catch (Exception e) {
throw new CompletionException(e);
}
return ok();
});
return result;
}
@@ -40,7 +46,7 @@ public abstract class StoreApiControllerImpInterface {
CompletionStage<Map<String, Integer>> stage = getInventory(request).thenApply(obj -> {
return obj;
});
stage.thenApply(obj -> {
return stage.thenApply(obj -> {
JsonNode result = mapper.valueToTree(obj);
return ok(result);
});
@@ -56,7 +62,7 @@ stage.thenApply(obj -> {
}
return obj;
});
stage.thenApply(obj -> {
return stage.thenApply(obj -> {
JsonNode result = mapper.valueToTree(obj);
return ok(result);
});
@@ -72,7 +78,7 @@ stage.thenApply(obj -> {
}
return obj;
});
stage.thenApply(obj -> {
return stage.thenApply(obj -> {
JsonNode result = mapper.valueToTree(obj);
return ok(result);
});

View File

@@ -16,6 +16,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import openapitools.OpenAPIUtils;
import static play.mvc.Results.ok;
import play.api.libs.Files.TemporaryFile;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CompletableFuture;
@@ -27,40 +28,60 @@ public abstract class UserApiControllerImpInterface {
private ObjectMapper mapper = new ObjectMapper();
public CompletionStage<Result> createUserHttp(Http.Request request, User body) throws Exception {
return CompletableFuture.supplyAsync(() -> {
CompletableFuture<Result> result = CompletableFuture.supplyAsync(() -> {
try {
createUser(request, body);
return ok();
});
} catch (Exception e) {
throw new CompletionException(e);
}
return ok();
});
return result;
}
public abstract void createUser(Http.Request request, User body) throws Exception;
public CompletionStage<Result> createUsersWithArrayInputHttp(Http.Request request, List<User> body) throws Exception {
return CompletableFuture.supplyAsync(() -> {
CompletableFuture<Result> result = CompletableFuture.supplyAsync(() -> {
try {
createUsersWithArrayInput(request, body);
return ok();
});
} catch (Exception e) {
throw new CompletionException(e);
}
return ok();
});
return result;
}
public abstract void createUsersWithArrayInput(Http.Request request, List<User> body) throws Exception;
public CompletionStage<Result> createUsersWithListInputHttp(Http.Request request, List<User> body) throws Exception {
return CompletableFuture.supplyAsync(() -> {
CompletableFuture<Result> result = CompletableFuture.supplyAsync(() -> {
try {
createUsersWithListInput(request, body);
return ok();
});
} catch (Exception e) {
throw new CompletionException(e);
}
return ok();
});
return result;
}
public abstract void createUsersWithListInput(Http.Request request, List<User> body) throws Exception;
public CompletionStage<Result> deleteUserHttp(Http.Request request, String username) throws Exception {
return CompletableFuture.supplyAsync(() -> {
CompletableFuture<Result> result = CompletableFuture.supplyAsync(() -> {
try {
deleteUser(request, username);
return ok();
});
} catch (Exception e) {
throw new CompletionException(e);
}
return ok();
});
return result;
}
@@ -73,7 +94,7 @@ public abstract class UserApiControllerImpInterface {
}
return obj;
});
stage.thenApply(obj -> {
return stage.thenApply(obj -> {
JsonNode result = mapper.valueToTree(obj);
return ok(result);
});
@@ -86,7 +107,7 @@ stage.thenApply(obj -> {
CompletionStage<String> stage = loginUser(request, username, password).thenApply(obj -> {
return obj;
});
stage.thenApply(obj -> {
return stage.thenApply(obj -> {
JsonNode result = mapper.valueToTree(obj);
return ok(result);
});
@@ -96,20 +117,30 @@ stage.thenApply(obj -> {
public abstract CompletionStage<String> loginUser(Http.Request request, @NotNull String username, @NotNull String password) throws Exception;
public CompletionStage<Result> logoutUserHttp(Http.Request request) throws Exception {
return CompletableFuture.supplyAsync(() -> {
CompletableFuture<Result> result = CompletableFuture.supplyAsync(() -> {
try {
logoutUser(request);
return ok();
});
} catch (Exception e) {
throw new CompletionException(e);
}
return ok();
});
return result;
}
public abstract void logoutUser(Http.Request request) throws Exception;
public CompletionStage<Result> updateUserHttp(Http.Request request, String username, User body) throws Exception {
return CompletableFuture.supplyAsync(() -> {
CompletableFuture<Result> result = CompletableFuture.supplyAsync(() -> {
try {
updateUser(request, username, body);
return ok();
});
} catch (Exception e) {
throw new CompletionException(e);
}
return ok();
});
return result;
}