[ofbiz-plugins] branch trunk updated: Implemented: OpenAPI generation for resources configured using XML DSL(OFBIZ-11995)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ofbiz-plugins] branch trunk updated: Implemented: OpenAPI generation for resources configured using XML DSL(OFBIZ-11995)

grv-2
This is an automated email from the ASF dual-hosted git repository.

grv pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-plugins.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 85e76a3  Implemented: OpenAPI generation for resources configured using XML DSL(OFBIZ-11995)
85e76a3 is described below

commit 85e76a3997d3d7df32e166f154da484abff85434
Author: Girish Vasmatkar <[hidden email]>
AuthorDate: Wed Sep 16 13:10:46 2020 +0530

    Implemented: OpenAPI generation for resources configured using XML DSL(OFBIZ-11995)
---
 .../ofbiz/ws/rs/openapi/OFBizOpenApiReader.java    | 60 +++++++++++++++++++++-
 1 file changed, 59 insertions(+), 1 deletion(-)

diff --git a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/openapi/OFBizOpenApiReader.java b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/openapi/OFBizOpenApiReader.java
index 480aa2a..182347a 100644
--- a/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/openapi/OFBizOpenApiReader.java
+++ b/ofbiz-rest-impl/src/main/java/org/apache/ofbiz/ws/rs/openapi/OFBizOpenApiReader.java
@@ -21,6 +21,7 @@ package org.apache.ofbiz.ws.rs.openapi;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedHashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -34,7 +35,11 @@ import org.apache.ofbiz.service.GenericServiceException;
 import org.apache.ofbiz.service.LocalDispatcher;
 import org.apache.ofbiz.service.ModelService;
 import org.apache.ofbiz.webapp.WebAppUtil;
+import org.apache.ofbiz.ws.rs.core.OFBizApiConfig;
 import org.apache.ofbiz.ws.rs.listener.ApiContextListener;
+import org.apache.ofbiz.ws.rs.model.ModelApi;
+import org.apache.ofbiz.ws.rs.model.ModelOperation;
+import org.apache.ofbiz.ws.rs.model.ModelResource;
 import org.apache.ofbiz.ws.rs.util.OpenApiUtil;
 
 import io.swagger.v3.jaxrs2.Reader;
@@ -89,8 +94,61 @@ public final class OFBizOpenApiReader extends Reader implements OpenApiReader {
         return openApi;
     }
 
-    // TODO - Add method contents
     private void addApiResources() {
+        Map<String, ModelApi> apis = OFBizApiConfig.getModelApis();
+        SecurityRequirement security = new SecurityRequirement();
+        security.addList("jwtToken");
+        apis.forEach((k, v) -> {
+            List<ModelResource> resources = v.getResources();
+            resources.forEach(modelResource -> {
+                Tag resourceTab = new Tag().name(modelResource.getDisplayName()).description(modelResource.getDescription());
+                openApiTags.add(resourceTab);
+                String basePath = modelResource.getPath();
+                for (ModelOperation op : modelResource.getOperations()) {
+                    String uri = basePath + op.getPath();
+                    boolean pathExists = false;
+                    PathItem pathItemObject = paths.get(uri);
+                    if (UtilValidate.isEmpty(pathItemObject)) {
+                        pathItemObject = new PathItem();
+                    } else {
+                        pathExists = true;
+                    }
+                    String serviceName = op.getService();
+                    final Operation operation = new Operation().summary(op.getDescription())
+                            .description(op.getDescription()).addTagsItem(modelResource.getDisplayName())
+                            .operationId(serviceName).deprecated(false).addSecurityItem(security);
+                    String verb = op.getVerb().toUpperCase();
+                    ModelService service = null;
+                    try {
+                        service = context.getModelService(serviceName);
+                    } catch (GenericServiceException e) {
+                        e.printStackTrace();
+                    }
+                    if (verb.equalsIgnoreCase(HttpMethod.GET)) {
+                        final QueryParameter serviceInParam = (QueryParameter) new QueryParameter().required(true)
+                                .description("Operation Input Parameters in JSON").name("input");
+                        Schema<?> refSchema = new Schema<>();
+                        refSchema.$ref("#/components/schemas/" + "api.request." + service.getName());
+                        serviceInParam.schema(refSchema);
+                        operation.addParametersItem(serviceInParam);
+                    } else if (verb.matches(HttpMethod.POST + "|" + HttpMethod.PUT + "|" + HttpMethod.PATCH)) {
+                        RequestBody request = new RequestBody()
+                                .description("Request Body for operation " + op.getDescription())
+                                .content(new Content().addMediaType(javax.ws.rs.core.MediaType.APPLICATION_JSON,
+                                        new MediaType().schema(new Schema<>()
+                                                .$ref("#/components/schemas/" + "api.request." + service.getName()))));
+                        operation.setRequestBody(request);
+                    }
+                    addServiceOutSchema(service);
+                    addServiceInSchema(service);
+                    addServiceOperationApiResponses(service, operation);
+                    setPathItemOperation(pathItemObject, verb.toUpperCase(), operation);
+                    if (!pathExists) {
+                        paths.addPathItem(basePath + op.getPath(), pathItemObject);
+                    }
+                }
+            });
+        });
     }
 
     private void addExportableServices() {