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/06/14 06:01:51 UTC

[camel] branch camel-3.20.x updated: CAMEL-19453: camel-jbang - Run with --open-api to support yaml spec files

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

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


The following commit(s) were added to refs/heads/camel-3.20.x by this push:
     new c34f8eda3a1 CAMEL-19453: camel-jbang - Run with --open-api to support yaml spec files
c34f8eda3a1 is described below

commit c34f8eda3a169558b88081515106c7587f7ade09
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jun 14 08:01:10 2023 +0200

    CAMEL-19453: camel-jbang - Run with --open-api to support yaml spec files
---
 .../org/apache/camel/dsl/jbang/core/commands/Run.java | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 8654f83eba6..29b5209ffa3 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -22,6 +22,7 @@ import java.awt.datatransfer.DataFlavor;
 import java.awt.datatransfer.UnsupportedFlavorException;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -46,6 +47,7 @@ import java.util.stream.Collectors;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
 import io.apicurio.datamodels.Library;
 import io.apicurio.datamodels.openapi.models.OasDocument;
 import org.apache.camel.CamelContext;
@@ -202,7 +204,7 @@ public class Run extends CamelCommand {
     @Option(names = { "--modeline" }, defaultValue = "true", description = "Enables Camel-K style modeline")
     boolean modeline = true;
 
-    @Option(names = { "--open-api" }, description = "Adds an OpenAPI spec from the given file")
+    @Option(names = { "--open-api" }, description = "Adds an OpenAPI spec from the given file (json or yaml file)")
     String openapi;
 
     @Option(names = { "--code" }, description = "Run the given string as Java DSL route")
@@ -944,8 +946,19 @@ public class Run extends CamelCommand {
     }
 
     private void generateOpenApi() throws Exception {
-        final ObjectMapper mapper = new ObjectMapper();
-        final JsonNode node = mapper.readTree(Paths.get(openapi).toFile());
+        File file = Paths.get(openapi).toFile();
+        if (!file.exists() && !file.isFile()) {
+            throw new FileNotFoundException("Cannot find file: " + file);
+        }
+
+        ObjectMapper mapper;
+        boolean yaml = file.getName().endsWith(".yaml") || file.getName().endsWith(".yml");
+        if (yaml) {
+            mapper = new YAMLMapper();
+        } else {
+            mapper = new ObjectMapper();
+        }
+        JsonNode node = mapper.readTree(file);
         OasDocument document = (OasDocument) Library.readDocument(node);
         RuntimeUtil.setRootLoggingLevel("off");
         try (CamelContext context = new LightweightCamelContext()) {