You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ji...@apache.org on 2024/11/13 09:00:53 UTC

(camel-quarkus) 01/03: Fix #6702 to add custom openapi spec locations (#6769)

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

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

commit 12aec55e252ab040cac1cd804683cb057f204fc6
Author: Zheng Feng <zh...@gmail.com>
AuthorDate: Wed Nov 13 07:58:11 2024 +0800

    Fix #6702 to add custom openapi spec locations (#6769)
    
    * Fix #6702 to add custom openapi spec locations
    
    * Apply suggestions from code review
    
    Co-authored-by: James Netherton <ja...@users.noreply.github.com>
    
    * Regen
    
    ---------
    
    Co-authored-by: James Netherton <ja...@users.noreply.github.com>
---
 .../pages/reference/extensions/rest-openapi.adoc   | 12 +++++++--
 .../CamelQuarkusSwaggerCodegenProvider.java        | 27 ++++++++++++++++---
 .../runtime/RestOpenApiBuildTimeConfig.java        | 19 ++++++++++---
 .../component/rest/openapi/it/FruitResource.java   |  4 +--
 .../component/rest/openapi/it/model/Fruit.java     | 31 ----------------------
 .../src/main/resources/application.properties      |  1 +
 6 files changed, 53 insertions(+), 41 deletions(-)

diff --git a/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc b/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc
index fee26616da..bfcaeb004b 100644
--- a/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc
+++ b/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc
@@ -138,7 +138,9 @@ quarkus.native.resources.includes=contract.json
 
 |icon:lock[title=Fixed at build time] [[quarkus.camel.openapi.codegen.enabled]]`link:#quarkus.camel.openapi.codegen.enabled[quarkus.camel.openapi.codegen.enabled]`
 
-If `true`, Camel Quarkus OpenAPI code generation is run for .json files discovered from the `openapi` directory. When `false`, code generation for .json files is disabled.
+If `true`, Camel Quarkus OpenAPI code generation is run for .json and .yaml files discovered from the `openapi`
+directory. When
+`false`, code generation for .json and .yaml files is disabled.
 | `boolean`
 | `true`
 
@@ -150,7 +152,7 @@ The package to use for generated model classes.
 
 |icon:lock[title=Fixed at build time] [[quarkus.camel.openapi.codegen.models]]`link:#quarkus.camel.openapi.codegen.models[quarkus.camel.openapi.codegen.models]`
 
-A comma separated list of models to generate. All models is the default.
+A comma separated list of models to generate. The default is empty list for all models.
 | `string`
 | 
 
@@ -177,6 +179,12 @@ If `true`, use JsonIgnoreProperties(ignoreUnknown = true) annotation in the gene
 Additional properties to be used in the mustache templates.
 | `Map<String,String>`
 | 
+
+|icon:lock[title=Fixed at build time] [[quarkus.camel.openapi.codegen.locations]]`link:#quarkus.camel.openapi.codegen.locations[quarkus.camel.openapi.codegen.locations]`
+
+A comma separated list of OpenAPI spec locations.
+| `string`
+|
 |===
 
 [.configuration-legend]
diff --git a/extensions/rest-openapi/deployment/src/main/java/org/apache/camel/quarkus/component/rest/openapi/deployment/CamelQuarkusSwaggerCodegenProvider.java b/extensions/rest-openapi/deployment/src/main/java/org/apache/camel/quarkus/component/rest/openapi/deployment/CamelQuarkusSwaggerCodegenProvider.java
index 546af5455f..6f8aedf435 100644
--- a/extensions/rest-openapi/deployment/src/main/java/org/apache/camel/quarkus/component/rest/openapi/deployment/CamelQuarkusSwaggerCodegenProvider.java
+++ b/extensions/rest-openapi/deployment/src/main/java/org/apache/camel/quarkus/component/rest/openapi/deployment/CamelQuarkusSwaggerCodegenProvider.java
@@ -18,10 +18,12 @@
 package org.apache.camel.quarkus.component.rest.openapi.deployment;
 
 import java.io.IOException;
+import java.net.URI;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
 import java.util.stream.Stream;
 
 import io.quarkus.bootstrap.prebuild.CodeGenException;
@@ -65,7 +67,7 @@ public class CamelQuarkusSwaggerCodegenProvider implements CodeGenProvider {
         }
 
         try {
-            List<String> specFiles = new ArrayList<>();
+            Set<String> specFiles = new HashSet<>();
             if (Files.isDirectory(context.inputDir())) {
                 try (Stream<Path> protoFilesPaths = Files.walk(context.inputDir())) {
                     protoFilesPaths
@@ -78,6 +80,24 @@ public class CamelQuarkusSwaggerCodegenProvider implements CodeGenProvider {
                 }
             }
 
+            Optional<String> locations = config.getOptionalValue("quarkus.camel.openapi.codegen.locations", String.class);
+            if (locations.isPresent()) {
+                for (String location : locations.get().split(",")) {
+                    try {
+                        URI uri;
+                        if (location.indexOf("://") == -1) {
+                            uri = Thread.currentThread().getContextClassLoader().getResource(location).toURI();
+                        } else {
+                            uri = new URI(location);
+                        }
+                        Path path = Path.of(uri);
+                        specFiles.add(path.toAbsolutePath().toString());
+                    } catch (Exception e) {
+                        LOG.warnf(e, "Can not find location %s", location);
+                    }
+                }
+            }
+
             String packageName = config.getValue("quarkus.camel.openapi.codegen.model-package", String.class);
             String models = config.getOptionalValue("quarkus.camel.openapi.codegen.models", String.class).orElse("");
             boolean useBeanValidation = config.getValue("quarkus.camel.openapi.codegen.use-bean-validation", Boolean.class);
@@ -86,6 +106,7 @@ public class CamelQuarkusSwaggerCodegenProvider implements CodeGenProvider {
                     Boolean.class);
 
             for (String specFile : specFiles) {
+                LOG.infof("Generating models for %s", specFile);
                 CodegenConfigurator configurator = new CodegenConfigurator();
                 configurator.setLang("quarkus");
                 configurator.setLibrary("quarkus3");
diff --git a/extensions/rest-openapi/runtime/src/main/java/org/apache/camel/quarkus/rest/openapi/runtime/RestOpenApiBuildTimeConfig.java b/extensions/rest-openapi/runtime/src/main/java/org/apache/camel/quarkus/rest/openapi/runtime/RestOpenApiBuildTimeConfig.java
index fcebc9e0a5..d6c107aa81 100644
--- a/extensions/rest-openapi/runtime/src/main/java/org/apache/camel/quarkus/rest/openapi/runtime/RestOpenApiBuildTimeConfig.java
+++ b/extensions/rest-openapi/runtime/src/main/java/org/apache/camel/quarkus/rest/openapi/runtime/RestOpenApiBuildTimeConfig.java
@@ -35,8 +35,11 @@ public class RestOpenApiBuildTimeConfig {
     @ConfigGroup
     public static class CodeGenConfig {
         /**
-         * If {@code true}, Camel Quarkus OpenAPI code generation is run for .json files discovered from the {@code openapi}
-         * directory. When {@code false}, code generation for .json files is disabled.
+         * If `true`, Camel Quarkus OpenAPI code generation is run for .json and .yaml files discovered from the `openapi`
+         * directory. When
+         * `false`, code generation for .json and .yaml files is disabled.
+         *
+         * @asciidoclet
          */
         @ConfigItem(defaultValue = "true")
         public boolean enabled;
@@ -48,7 +51,9 @@ public class RestOpenApiBuildTimeConfig {
         public String modelPackage;
 
         /**
-         * A comma separated list of models to generate. All models is the default.
+         * A comma separated list of models to generate. The default is empty list for all models.
+         *
+         * @asciidoclet
          */
         @ConfigItem
         public Optional<String> models;
@@ -80,6 +85,14 @@ public class RestOpenApiBuildTimeConfig {
          */
         @ConfigItem
         public Map<String, String> additionalProperties;
+
+        /**
+         * A comma separated list of OpenAPI spec locations.
+         *
+         * @asciidoclet
+         */
+        @ConfigItem
+        public Optional<String> locations;
     }
 
 }
diff --git a/integration-tests/rest-openapi/src/main/java/org/apache/camel/quarkus/component/rest/openapi/it/FruitResource.java b/integration-tests/rest-openapi/src/main/java/org/apache/camel/quarkus/component/rest/openapi/it/FruitResource.java
index 73b66b0271..a178327d3c 100644
--- a/integration-tests/rest-openapi/src/main/java/org/apache/camel/quarkus/component/rest/openapi/it/FruitResource.java
+++ b/integration-tests/rest-openapi/src/main/java/org/apache/camel/quarkus/component/rest/openapi/it/FruitResource.java
@@ -37,8 +37,8 @@ public class FruitResource {
     private Set<Fruit> fruits = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>()));
 
     public FruitResource() {
-        fruits.add(new Fruit("Apple", "Winter fruit"));
-        fruits.add(new Fruit("Pineapple", "Tropical fruit"));
+        fruits.add(new Fruit().name("Apple").description("Winter fruit"));
+        fruits.add(new Fruit().name("Pineapple").description("Tropical fruit"));
     }
 
     @Operation(operationId = "list")
diff --git a/integration-tests/rest-openapi/src/main/java/org/apache/camel/quarkus/component/rest/openapi/it/model/Fruit.java b/integration-tests/rest-openapi/src/main/java/org/apache/camel/quarkus/component/rest/openapi/it/model/Fruit.java
deleted file mode 100644
index 8b21d8809d..0000000000
--- a/integration-tests/rest-openapi/src/main/java/org/apache/camel/quarkus/component/rest/openapi/it/model/Fruit.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * 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.quarkus.component.rest.openapi.it.model;
-
-public class Fruit {
-
-    public String name;
-    public String description;
-
-    public Fruit() {
-    }
-
-    public Fruit(String name, String description) {
-        this.name = name;
-        this.description = description;
-    }
-}
diff --git a/integration-tests/rest-openapi/src/main/resources/application.properties b/integration-tests/rest-openapi/src/main/resources/application.properties
index 01f7a2133a..be86366699 100644
--- a/integration-tests/rest-openapi/src/main/resources/application.properties
+++ b/integration-tests/rest-openapi/src/main/resources/application.properties
@@ -15,6 +15,7 @@
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
 quarkus.native.resources.includes=openapi.json,petstore.json,example.yaml
+quarkus.camel.openapi.codegen.locations=openapi.json
 quarkus.camel.openapi.codegen.model-package=org.apache.camel.quarkus.component.rest.openapi.it.model
 quarkus.camel.openapi.codegen.not-null-jackson=true
 quarkus.camel.openapi.codegen.ignore-unknown-properties=true