You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/03/31 15:24:56 UTC

[camel] branch main updated: Implement CAMEL-19232 (#9739)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 4f1801e8bbe Implement CAMEL-19232 (#9739)
4f1801e8bbe is described below

commit 4f1801e8bbef37402cde79e48dbd73ba69cf6abf
Author: Marat Gubaidullin <ma...@gmail.com>
AuthorDate: Fri Mar 31 11:24:48 2023 -0400

    Implement CAMEL-19232 (#9739)
---
 dsl/camel-jbang/camel-jbang-core/pom.xml           | 17 ++++++
 .../dsl/jbang/core/commands/CodeRestGenerator.java | 69 +++++++++++++++++++---
 parent/pom.xml                                     |  1 +
 3 files changed, 78 insertions(+), 9 deletions(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/pom.xml b/dsl/camel-jbang/camel-jbang-core/pom.xml
index 197179a3e9a..3993f78c25b 100644
--- a/dsl/camel-jbang/camel-jbang-core/pom.xml
+++ b/dsl/camel-jbang/camel-jbang-core/pom.xml
@@ -124,6 +124,21 @@
             <artifactId>camel-openapi-rest-dsl-generator</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.openapitools</groupId>
+            <artifactId>openapi-generator</artifactId>
+            <version>${openapi-generator}</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.slf4j</groupId>
+                    <artifactId>slf4j-api</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>org.slf4j</groupId>
+                    <artifactId>slf4j-simple</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
         <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-databind</artifactId>
@@ -131,4 +146,6 @@
         </dependency>
     </dependencies>
 
+
+
 </project>
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CodeRestGenerator.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CodeRestGenerator.java
index 99a67e2d919..8515b53d58f 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CodeRestGenerator.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CodeRestGenerator.java
@@ -16,8 +16,10 @@
  */
 package org.apache.camel.dsl.jbang.core.commands;
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.Map;
@@ -31,10 +33,16 @@ import org.apache.camel.generator.openapi.RestDslGenerator;
 import org.apache.camel.impl.lw.LightweightCamelContext;
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.core.config.Configurator;
+import org.openapitools.codegen.ClientOptInput;
+import org.openapitools.codegen.DefaultGenerator;
+import org.openapitools.codegen.config.CodegenConfigurator;
 import org.yaml.snakeyaml.Yaml;
 import org.yaml.snakeyaml.constructor.SafeConstructor;
 import picocli.CommandLine;
 
+import static org.openapitools.codegen.CodegenConstants.GENERATE_MODELS;
+import static org.openapitools.codegen.CodegenConstants.SERIALIZABLE_MODEL;
+
 @CommandLine.Command(name = "rest", description = "Generate REST DSL source code from OpenApi specification")
 public class CodeRestGenerator extends CamelCommand {
 
@@ -42,10 +50,17 @@ public class CodeRestGenerator extends CamelCommand {
     private String input;
     @CommandLine.Option(names = { "-o", "--output" }, description = "Output REST DSL file name")
     private String output;
-    @CommandLine.Option(names = { "-t", "--type" }, description = "REST DSL type (YAML or XML)", defaultValue = "yaml")
+    @CommandLine.Option(names = { "-t", "--type" }, description = "REST DSL type (YAML or XML)")
     private String type;
     @CommandLine.Option(names = { "-r", "--routes" }, description = "Generate routes (YAML)")
     private boolean generateRoutes;
+    @CommandLine.Option(names = { "-d", "--dto" }, description = "Data Objects")
+    private boolean generateDataObjects;
+    @CommandLine.Option(names = { "-run", "--runtime" }, description = "Runtime (quarkus, spring-boot)",
+            defaultValue = "quarkus")
+    private String runtime;
+    @CommandLine.Option(names = { "-p", "--package" }, description = "Package for generated models", defaultValue = "model")
+    private String packageName;
 
     public CodeRestGenerator(CamelJBangMain main) {
         super(main);
@@ -57,15 +72,23 @@ public class CodeRestGenerator extends CamelCommand {
         OasDocument document = (OasDocument) Library.readDocument(node);
         Configurator.setRootLevel(Level.OFF);
         try (CamelContext context = new LightweightCamelContext()) {
-            final String yaml = type.equalsIgnoreCase("yaml")
-                    ? RestDslGenerator.toYaml(document).generate(context, generateRoutes)
-                    : RestDslGenerator.toXml(document).generate(context);
-            if (output == null) {
-                System.out.println(yaml);
-            } else {
-                Files.write(Paths.get(output), yaml.getBytes());
+            String text = null;
+            if (type.equalsIgnoreCase("yaml")) {
+                text = RestDslGenerator.toYaml(document).generate(context, generateRoutes);
+            } else if (type.equalsIgnoreCase("xml")) {
+                text = RestDslGenerator.toXml(document).generate(context);
+            }
+            if (text != null) {
+                if (output == null) {
+                    System.out.println(text);
+                } else {
+                    Files.write(Paths.get(output), text.getBytes());
+                }
             }
         }
+        if (generateDataObjects) {
+            generateDto();
+        }
         return 0;
     }
 
@@ -80,4 +103,32 @@ public class CodeRestGenerator extends CamelCommand {
         Map map = loader.load(new FileInputStream(Paths.get(input).toFile()));
         return mapper.convertValue(map, JsonNode.class);
     }
-}
+
+    private void generateDto() throws IOException {
+        final String code = "code";
+        final String generatorName = "quarkus".equals(runtime) ? "jaxrs-spec" : "java-camel";
+        final String library = "quarkus".equals(runtime) ? "quarkus" : "spring-boot";
+        File output = Files.createTempDirectory("gendto").toFile();
+
+        final CodegenConfigurator configurator = new CodegenConfigurator()
+                .setGeneratorName(generatorName)
+                .setLibrary(library)
+                .setInputSpec(input)
+                .setModelPackage(packageName)
+                .setAdditionalProperties(
+                        Map.of(
+                                SERIALIZABLE_MODEL, "false",
+                                "useJakartaEe", "false",
+                                "useSwaggerAnnotations", "false",
+                                GENERATE_MODELS, "true",
+                                "generatePom", "false",
+                                "generateApis", "false",
+                                "sourceFolder", code))
+                .setOutputDir(output.getAbsolutePath());
+
+        final ClientOptInput clientOptInput = configurator.toClientOptInput();
+        new DefaultGenerator().opts(clientOptInput).generate();
+        File generated = new File(Paths.get(output.getAbsolutePath(), code, packageName).toUri());
+        generated.renameTo(new File(packageName));
+    }
+}
\ No newline at end of file
diff --git a/parent/pom.xml b/parent/pom.xml
index 9cdc1b8183d..966c47eaae3 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -393,6 +393,7 @@
         <olingo2-version>2.0.11</olingo2-version>
         <olingo4-version>4.8.0</olingo4-version>
         <ognl-version>3.3.4</ognl-version>
+        <openapi-generator>6.2.1</openapi-generator>
         <openjpa-version>3.2.2</openjpa-version>
         <openstack4j-version>3.10</openstack4j-version>
         <opentelemetry-version>1.23.1</opentelemetry-version>