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 6d361622e14 CAMEL-20446: camel-jbang - Regenerate --open-api on reload so you can… (#13245) 6d361622e14 is described below commit 6d361622e140bb2b68ae70ade35fc61d8a8b0779 Author: Claus Ibsen <claus.ib...@gmail.com> 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 8326f57cef9..a7ef2af46db 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 a86647b3c34..9911483c131 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc @@ -3338,6 +3338,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 4a3307fb184..ff127e5679c 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 ffa1d3ffacb..648d421cefe 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; @@ -64,6 +65,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; @@ -613,6 +615,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 06e77eb9ded..7c9f79c79c2 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -2684,6 +2684,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>