This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.4.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.4.x by this push:
new ee6357387ce CAMEL-20446: camel-jbang - Regenerate --open-api on reload
so you can… (#13245)
ee6357387ce is described below
commit ee6357387ce91f96af19c388bb786ac741b70abd
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Feb 21 15:25:29 2024 +0100
CAMEL-20446: camel-jbang - Regenerate --open-api on reload so you can…
(#13245)
CAMEL-20446: camel-jbang - Regenerate --open-api on reload so you can do
this in --dev mode
---
bom/camel-bom/pom.xml | 5 ++
.../modules/ROOT/pages/camel-jbang.adoc | 3 +
dsl/camel-kamelet-main/pom.xml | 5 ++
.../java/org/apache/camel/main/KameletMain.java | 12 ++++
.../reload/OpenApiGeneratorReloadStrategy.java | 81 ++++++++++++++++++++++
parent/pom.xml | 5 ++
6 files changed, 111 insertions(+)
diff --git a/bom/camel-bom/pom.xml b/bom/camel-bom/pom.xml
index 424baae75b6..7e78163a8e8 100644
--- a/bom/camel-bom/pom.xml
+++ b/bom/camel-bom/pom.xml
@@ -1492,6 +1492,11 @@
<artifactId>camel-openapi-java</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-openapi-rest-dsl-generator</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-opensearch</artifactId>
diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
index edbb0a1eee7..7b5f85b70d5 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
@@ -3287,6 +3287,9 @@ The follow options related to _exporting_, can be
configured in `application.pro
|`camel.jbang.metrics`
| Metrics (Micrometer and Prometheus) at /q/metrics on local HTTP server (port
8080 by default) when running standalone Camel
+|`camel.jbang.open-api`
+| File name of open-api spec file (json or yaml) that are used when using
`--open-api` to generate routes from swagger/openapi API spec file.
+
|`camel.jbang.ignoreLoadingError`
| Whether to ignore route loading and compilation errors (use this with care!)
diff --git a/dsl/camel-kamelet-main/pom.xml b/dsl/camel-kamelet-main/pom.xml
index e9fa30c82ce..010bf75b2eb 100644
--- a/dsl/camel-kamelet-main/pom.xml
+++ b/dsl/camel-kamelet-main/pom.xml
@@ -122,6 +122,11 @@
<groupId>org.apache.camel</groupId>
<artifactId>camel-catalog-console</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-openapi-rest-dsl-generator</artifactId>
+ </dependency>
+
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-resourceresolver-github</artifactId>
diff --git
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
index 28f8a6825f4..1c4b46f6515 100644
---
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
+++
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
@@ -17,6 +17,7 @@
package org.apache.camel.main;
import java.io.File;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@@ -63,6 +64,7 @@ import
org.apache.camel.main.download.PromptPropertyPlaceholderSource;
import org.apache.camel.main.download.StubBeanRepository;
import org.apache.camel.main.download.TypeConverterLoaderDownloadListener;
import org.apache.camel.main.injection.AnnotationDependencyInjection;
+import org.apache.camel.main.reload.OpenApiGeneratorReloadStrategy;
import org.apache.camel.main.util.ClipboardReloadStrategy;
import org.apache.camel.main.util.ExtraClassesClassLoader;
import org.apache.camel.main.util.ExtraFilesClassLoader;
@@ -609,6 +611,16 @@ public class KameletMain extends MainCommandLineSupport {
PeriodTaskScheduler scheduler =
PluginHelper.getPeriodTaskScheduler(answer);
scheduler.schedulePeriodTask(reloader, 2000);
}
+
+ // reload with openapi
+ String openapi =
getInitialProperties().getProperty("camel.jbang.open-api");
+ String reload =
getInitialProperties().getProperty("camel.main.routesReloadDirectory");
+ if (openapi != null && (reload != null || sourceDir != null)) {
+ // add open-api reloader that generate output to
.camel-jbang/generated-openapi.yaml
+ File file = Paths.get(openapi).toFile();
+ OpenApiGeneratorReloadStrategy rs = new
OpenApiGeneratorReloadStrategy(file);
+ answer.addService(rs);
+ }
} catch (Exception e) {
throw RuntimeCamelException.wrapRuntimeException(e);
}
diff --git
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/reload/OpenApiGeneratorReloadStrategy.java
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/reload/OpenApiGeneratorReloadStrategy.java
new file mode 100644
index 00000000000..5ecbde23b98
--- /dev/null
+++
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/reload/OpenApiGeneratorReloadStrategy.java
@@ -0,0 +1,81 @@
+/*
+ * 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.main.reload;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
+import io.apicurio.datamodels.Library;
+import io.apicurio.datamodels.models.openapi.OpenApiDocument;
+import org.apache.camel.generator.openapi.RestDslGenerator;
+import org.apache.camel.support.FileWatcherResourceReloadStrategy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * When using --open-api in --dev mode then we need to watch the openapi spec
file if its changed, and then regenerate
+ * the route file, which is then reloaded as a regular route.
+ */
+public class OpenApiGeneratorReloadStrategy extends
FileWatcherResourceReloadStrategy {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(OpenApiGeneratorReloadStrategy.class);
+
+ // write to jbang generated file so it can be reloaded
+ private static final String OPENAPI_GENERATED_FILE =
".camel-jbang/generated-openapi.yaml";
+
+ private final File openapi;
+
+ public OpenApiGeneratorReloadStrategy(File openapi) {
+ String parent = openapi.getParent();
+ if (parent == null) {
+ parent = ".";
+ }
+ setFolder(parent);
+ // need to adjust file to be what file watcher uses when matching
+ Path dir = new File(parent).toPath();
+ this.openapi = dir.resolve(openapi.toPath()).toFile();
+ setFileFilter(this.openapi::equals);
+ setResourceReload((name, resource) -> {
+ if (!openapi.exists() && !openapi.isFile()) {
+ return;
+ }
+
+ LOG.info("Generating open-api rest-dsl from: {}", openapi);
+ try {
+ ObjectMapper mapper;
+ boolean yaml = openapi.getName().endsWith(".yaml") ||
openapi.getName().endsWith(".yml");
+ if (yaml) {
+ mapper = new YAMLMapper();
+ } else {
+ mapper = new ObjectMapper();
+ }
+ ObjectNode node = (ObjectNode) mapper.readTree(openapi);
+ OpenApiDocument document = (OpenApiDocument)
Library.readDocument(node);
+ String out =
RestDslGenerator.toYaml(document).generate(getCamelContext(), false);
+ Files.write(Paths.get(OPENAPI_GENERATED_FILE), out.getBytes());
+ } catch (Exception e) {
+ LOG.warn("Error generating open-api rest-dsl due: " +
e.getMessage(), e);
+ }
+ });
+ }
+
+}
diff --git a/parent/pom.xml b/parent/pom.xml
index 51e26dd644a..a683df3f7cc 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -2673,6 +2673,11 @@
</dependency>
<!-- camel misc -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-openapi-rest-dsl-generator</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>org.apache.camel.tests</groupId>
<artifactId>org.apache.camel.tests.mock-javamail_1.7</artifactId>