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 e023d8b2e68 CAMEL-20839: camel-jbang - Run with openapi in sub folder 
does not work
e023d8b2e68 is described below

commit e023d8b2e68343a4458f5211a8a3f6b2b4e8e4dc
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Thu Jun 6 09:58:06 2024 +0200

    CAMEL-20839: camel-jbang - Run with openapi in sub folder does not work
---
 .../rest/openapi/RestOpenApiEndpoint.java          | 40 ++++++++++------------
 .../apache/camel/dsl/jbang/core/commands/Run.java  |  2 +-
 2 files changed, 19 insertions(+), 23 deletions(-)

diff --git 
a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiEndpoint.java
 
b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiEndpoint.java
index dee36ced6a1..019b5aa2987 100644
--- 
a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiEndpoint.java
+++ 
b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiEndpoint.java
@@ -16,11 +16,9 @@
  */
 package org.apache.camel.component.rest.openapi;
 
-import java.io.File;
+import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.nio.file.Files;
-import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -71,10 +69,9 @@ import org.apache.camel.spi.UriPath;
 import org.apache.camel.support.CamelContextHelper;
 import org.apache.camel.support.DefaultEndpoint;
 import org.apache.camel.support.ResourceHelper;
-import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.UnsafeUriCharactersEncoder;
-import org.apache.commons.io.FileUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -885,24 +882,25 @@ public final class RestOpenApiEndpoint extends 
DefaultEndpoint {
         final ParseOptions options = new ParseOptions();
         options.setResolveFully(true);
 
-        File tmpFileToDelete = null;
+        InputStream is = null;
         try {
+            String location = null;
+            String content = null;
             Resource resource = 
ResourceHelper.resolveMandatoryResource(camelContext, uri);
-            //if location can not be used in Swagger API (e.g. in case of 
"bean;")
-            // the content of the resource has to be copied into a tmp file 
for swagger API.
-            String locationToSearch;
-            if ("bean:".equals(ResourceHelper.getScheme(uri))) {
-                Path tmpFile = Files.createTempFile(null, null);
-                tmpFileToDelete = tmpFile.toFile();
-                tmpFileToDelete.deleteOnExit();
-                FileUtils.copyInputStreamToFile(resource.getInputStream(), 
tmpFileToDelete);
-                locationToSearch = tmpFile.toUri().toString();
+            if (resource.getScheme().startsWith("http")) {
+                location = resource.getURI().toString();
             } else {
-                locationToSearch = resource.getURI().toString();
+                is = resource.getInputStream();
+                if (is != null) {
+                    content = IOHelper.loadText(is);
+                }
+            }
+            SwaggerParseResult openApi = null;
+            if (location != null) {
+                openApi = openApiParser.readLocation(location, null, options);
+            } else if (content != null) {
+                openApi = openApiParser.readContents(content, null, options);
             }
-
-            final SwaggerParseResult openApi = 
openApiParser.readLocation(locationToSearch, null, options);
-
             if (openApi != null && openApi.getOpenAPI() != null) {
                 return openApi.getOpenAPI();
             }
@@ -910,9 +908,7 @@ public final class RestOpenApiEndpoint extends 
DefaultEndpoint {
             throw new IllegalArgumentException(
                     "The given OpenApi specification cannot be loaded from: " 
+ uri, e);
         } finally {
-            if (tmpFileToDelete != null) {
-                FileUtil.deleteFile(tmpFileToDelete);
-            }
+            IOHelper.close(is);
         }
 
         // In theory there should be a message in the parse result, but it has 
disappeared...
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 4232c4c80d9..a24d9b0b7d8 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
@@ -1494,7 +1494,7 @@ public class Run extends CamelCommand {
         String content = IOHelper.loadText(is);
         IOHelper.close(is);
 
-        String onlyName = FileUtil.stripPath(file.getName());
+        String onlyName = file.getPath();
         content = content.replaceFirst("\\{\\{ \\.Spec }}", onlyName);
 
         Files.writeString(Paths.get(OPENAPI_GENERATED_FILE), content);

Reply via email to