You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ma...@apache.org on 2022/11/16 18:43:29 UTC

[camel-kameleon] branch main updated: OpenAPI to Camel REST generator

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

marat pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-kameleon.git


The following commit(s) were added to refs/heads/main by this push:
     new 23009b8  OpenAPI to Camel REST generator
23009b8 is described below

commit 23009b8e075ef4fb78a89234d28a2c3641156bd9
Author: Marat Gubaidullin <ma...@gmail.com>
AuthorDate: Wed Nov 16 13:43:22 2022 -0500

    OpenAPI to Camel REST generator
---
 pom.xml                                            |  6 +++
 .../org/apache/camel/kameleon/WarmUpService.java   |  6 +--
 .../kameleon/generator/GeneratorResource.java      | 16 +++++-
 ...orService.java => ProjectGeneratorService.java} |  2 +-
 .../generator/RestDslGeneratorService.java         | 57 ++++++++++++++++++++++
 5 files changed, 81 insertions(+), 6 deletions(-)

diff --git a/pom.xml b/pom.xml
index 25f453a..57eba5b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -140,6 +140,12 @@ under the License.
             <groupId>io.quarkus</groupId>
             <artifactId>quarkus-container-image-docker</artifactId>
         </dependency>
+        <!-- Code generator -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-openapi-rest-dsl-generator</artifactId>
+            <version>${version.camel-classic}</version>
+        </dependency>
     </dependencies>
     <build>
         <resources>
diff --git a/src/main/java/org/apache/camel/kameleon/WarmUpService.java b/src/main/java/org/apache/camel/kameleon/WarmUpService.java
index 5d63806..4bb98a9 100644
--- a/src/main/java/org/apache/camel/kameleon/WarmUpService.java
+++ b/src/main/java/org/apache/camel/kameleon/WarmUpService.java
@@ -24,7 +24,7 @@ import io.vertx.mutiny.core.eventbus.EventBus;
 
 import org.apache.camel.kameleon.component.ComponentResource;
 import org.apache.camel.kameleon.config.ConfigurationResource;
-import org.apache.camel.kameleon.generator.GeneratorService;
+import org.apache.camel.kameleon.generator.ProjectGeneratorService;
 import org.jboss.logging.Logger;
 
 import javax.enterprise.context.ApplicationScoped;
@@ -48,7 +48,7 @@ public class WarmUpService {
     ComponentResource componentResource;
 
     @Inject
-    GeneratorService generatorService;
+    ProjectGeneratorService projectGeneratorService;
 
     void onStart(@Observes StartupEvent ev) {
         LOGGER.info("Data warmup start...");
@@ -74,7 +74,7 @@ public class WarmUpService {
             JsonArray componentArray = componentResource.components(type, version);
             List<String> componentList = componentArray.stream().map(o -> o.toString()).collect(Collectors.toList());
             String components = componentList.stream().limit(5).collect(Collectors.joining(","));
-            generatorService.generate(type, version, "org.apache.camel.kameleon", "demo", "0.0.1", javaVersion, components);
+            projectGeneratorService.generate(type, version, "org.apache.camel.kameleon", "demo", "0.0.1", javaVersion, components);
             LOGGER.info("Data warmup done for " + type + " " + version);
         } catch (Exception e) {
             LOGGER.error(e);
diff --git a/src/main/java/org/apache/camel/kameleon/generator/GeneratorResource.java b/src/main/java/org/apache/camel/kameleon/generator/GeneratorResource.java
index 4c11a39..6cbc04a 100644
--- a/src/main/java/org/apache/camel/kameleon/generator/GeneratorResource.java
+++ b/src/main/java/org/apache/camel/kameleon/generator/GeneratorResource.java
@@ -25,7 +25,7 @@ import java.io.File;
 public class GeneratorResource {
 
     @Inject
-    GeneratorService generatorService;
+    ProjectGeneratorService projectGeneratorService;
 
     @GET
     @Produces("application/zip")
@@ -36,7 +36,7 @@ public class GeneratorResource {
                 @QueryParam("artifactId") String artifactId,
                 @QueryParam("version") String version,
                 @QueryParam("components") String components) throws Exception {
-        String fileName = generatorService.generate(camelType,camelVersion, groupId, artifactId, version, javaVersion, components);
+        String fileName = projectGeneratorService.generate(camelType,camelVersion, groupId, artifactId, version, javaVersion, components);
         File nf = new File(fileName);
         if (nf.exists()){
             return  Response.ok((Object) nf)
@@ -46,6 +46,18 @@ public class GeneratorResource {
         }
         return Response.serverError().build();
     }
+
+    @Inject
+    RestDslGeneratorService restDslGeneratorService;
+
+    @POST
+    @Consumes({"application/yaml", "application/json"})
+    @Produces("application/yaml")
+    @Path("/openapi")
+    public Response generateRestDsl(@QueryParam("filename") String filename, String openapi) throws Exception {
+        String yaml = restDslGeneratorService.generate(filename, openapi);
+        return Response.ok(yaml).build();
+    }
 }
 
 
diff --git a/src/main/java/org/apache/camel/kameleon/generator/GeneratorService.java b/src/main/java/org/apache/camel/kameleon/generator/ProjectGeneratorService.java
similarity index 99%
rename from src/main/java/org/apache/camel/kameleon/generator/GeneratorService.java
rename to src/main/java/org/apache/camel/kameleon/generator/ProjectGeneratorService.java
index 0640eda..f006692 100644
--- a/src/main/java/org/apache/camel/kameleon/generator/GeneratorService.java
+++ b/src/main/java/org/apache/camel/kameleon/generator/ProjectGeneratorService.java
@@ -42,7 +42,7 @@ import java.util.*;
 import java.util.stream.Collectors;
 
 @ApplicationScoped
-public class GeneratorService {
+public class ProjectGeneratorService {
 
     private static final String CAMEL_QUARKUS_CORE = "camel-quarkus-core";
 
diff --git a/src/main/java/org/apache/camel/kameleon/generator/RestDslGeneratorService.java b/src/main/java/org/apache/camel/kameleon/generator/RestDslGeneratorService.java
new file mode 100644
index 0000000..d4dc537
--- /dev/null
+++ b/src/main/java/org/apache/camel/kameleon/generator/RestDslGeneratorService.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.kameleon.generator;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.apicurio.datamodels.Library;
+import io.apicurio.datamodels.openapi.models.OasDocument;
+import org.apache.camel.CamelContext;
+import org.apache.camel.generator.openapi.RestDslGenerator;
+import org.apache.camel.impl.lw.LightweightCamelContext;
+import org.yaml.snakeyaml.Yaml;
+import org.yaml.snakeyaml.constructor.SafeConstructor;
+
+import javax.enterprise.context.ApplicationScoped;
+import java.io.FileNotFoundException;
+import java.util.Map;
+
+@ApplicationScoped
+public class RestDslGeneratorService {
+
+    public String generate(String filename, String openapi) throws Exception {
+        System.out.println(filename);
+        System.out.println(openapi);
+        final JsonNode node = filename.endsWith("json") ? readNodeFromJson(openapi) : readNodeFromYaml(openapi);
+        OasDocument document = (OasDocument) Library.readDocument(node);
+        try (CamelContext context = new LightweightCamelContext()) {
+            return RestDslGenerator.toYaml(document).generate(context, true);
+        }
+    }
+
+    private JsonNode readNodeFromJson(String openapi) throws Exception {
+        final ObjectMapper mapper = new ObjectMapper();
+        return mapper.readTree(openapi);
+    }
+
+    private JsonNode readNodeFromYaml(String openapi) throws FileNotFoundException {
+        final ObjectMapper mapper = new ObjectMapper();
+        Yaml loader = new Yaml(new SafeConstructor());
+        Map map = loader.load(openapi);
+        return mapper.convertValue(map, JsonNode.class);
+    }
+}