From 5a2e4f2b75a1e5c2b483d163b4ed683075da8c8a Mon Sep 17 00:00:00 2001 From: mikera Date: Thu, 8 Aug 2024 18:34:07 +0100 Subject: [PATCH] Updates for REST API server --- .../java/convex/cli/local/LocalStart.java | 8 ++++---- convex-restapi/pom.xml | 2 +- .../main/java/convex/restapi/RESTServer.java | 20 ++++++++++++++++--- .../java/convex/restapi/api/ChainAPI.java | 13 ++++++++---- .../restapi/model/CreateAccountRequest.java | 8 ++++++++ 5 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 convex-restapi/src/main/java/convex/restapi/model/CreateAccountRequest.java diff --git a/convex-cli/src/main/java/convex/cli/local/LocalStart.java b/convex-cli/src/main/java/convex/cli/local/LocalStart.java index be19794e5..c54348742 100644 --- a/convex-cli/src/main/java/convex/cli/local/LocalStart.java +++ b/convex-cli/src/main/java/convex/cli/local/LocalStart.java @@ -87,11 +87,11 @@ private List getPeerKeyPairs(int n) { } int left=n-keyPairList.size(); if (left>0) { - log.warn("Insufficient key pairs specified. Additional "+left+" keypair(s) will be generated"); + informWarning("Insufficient key pairs specified. Additional "+left+" keypair(s) will be generated"); for (int i=0; i 1.1.1 - 6.1.6 + 6.2.0 diff --git a/convex-restapi/src/main/java/convex/restapi/RESTServer.java b/convex-restapi/src/main/java/convex/restapi/RESTServer.java index 86fb09e97..f1a4b1289 100644 --- a/convex-restapi/src/main/java/convex/restapi/RESTServer.java +++ b/convex-restapi/src/main/java/convex/restapi/RESTServer.java @@ -20,6 +20,8 @@ import io.javalin.community.ssl.SslPlugin; import io.javalin.config.JavalinConfig; import io.javalin.http.staticfiles.Location; +import io.javalin.openapi.JsonSchemaLoader; +import io.javalin.openapi.JsonSchemaResource; import io.javalin.openapi.plugin.OpenApiPlugin; import io.javalin.openapi.plugin.redoc.ReDocPlugin; import io.javalin.openapi.plugin.swagger.SwaggerPlugin; @@ -98,9 +100,13 @@ protected SslPlugin getSSLPlugin(HashMap config) { } protected void addOpenApiPlugins(JavalinConfig config) { + String docsPath="openapi-plugin/openapi-default.json"; + config.registerPlugin(new OpenApiPlugin(pluginConfig -> { - pluginConfig.withDefinitionConfiguration((version, definition) -> { + pluginConfig + .withDocumentationPath(docsPath) + .withDefinitionConfiguration((version, definition) -> { definition.withOpenApiInfo(info -> { info.setTitle("Convex REST API"); info.setVersion("0.1.1"); @@ -108,8 +114,16 @@ protected void addOpenApiPlugins(JavalinConfig config) { }); })); - config.registerPlugin(new SwaggerPlugin()); - config.registerPlugin(new ReDocPlugin()); + config.registerPlugin(new SwaggerPlugin(swaggerConfiguration->{ + swaggerConfiguration.setDocumentationPath(docsPath); + })); + config.registerPlugin(new ReDocPlugin(reDocConfiguration -> { + reDocConfiguration.setDocumentationPath(docsPath); + })); + + for (JsonSchemaResource generatedJsonSchema : new JsonSchemaLoader().loadGeneratedSchemes()) { + System.out.println(generatedJsonSchema.getName()); + } } protected ChainAPI chainAPI; diff --git a/convex-restapi/src/main/java/convex/restapi/api/ChainAPI.java b/convex-restapi/src/main/java/convex/restapi/api/ChainAPI.java index 841a6ca90..fe65af041 100644 --- a/convex-restapi/src/main/java/convex/restapi/api/ChainAPI.java +++ b/convex-restapi/src/main/java/convex/restapi/api/ChainAPI.java @@ -38,6 +38,7 @@ import convex.core.util.Utils; import convex.java.JSON; import convex.restapi.RESTServer; +import convex.restapi.model.CreateAccountRequest; import io.javalin.Javalin; import io.javalin.http.BadRequestResponse; import io.javalin.http.Context; @@ -112,11 +113,15 @@ public void getData(Context ctx) { operationId = "createAccount", summary="Create a new Convex account", requestBody = @OpenApiRequestBody( - description = "Complex bodies", + description = "Create Account request, must provide an accountKey for the new Account", content= {@OpenApiContent( - from = String.class, - // type = "application/json", - example = "{}")})) + from = CreateAccountRequest.class, + type = "application/json", + exampleObjects = { + @OpenApiExampleProperty(name="accountKey", value = "d82e78594610f708ad47f666bbacbab1711760652cb88bf7515ed6c3ae84a08d") + } + //example = "{\"accountKey\": \"d82e78594610f708ad47f666bbacbab1711760652cb88bf7515ed6c3ae84a08d\"}")} + )})) public void createAccount(Context ctx) { Map req=getJSONBody(ctx); Object key = req.get("accountKey"); diff --git a/convex-restapi/src/main/java/convex/restapi/model/CreateAccountRequest.java b/convex-restapi/src/main/java/convex/restapi/model/CreateAccountRequest.java new file mode 100644 index 000000000..eb0dfe246 --- /dev/null +++ b/convex-restapi/src/main/java/convex/restapi/model/CreateAccountRequest.java @@ -0,0 +1,8 @@ +package convex.restapi.model; + +import io.javalin.openapi.OpenApiByFields; + +@OpenApiByFields +public class CreateAccountRequest { + public String accountKey; +}