From 56fda4e9b936db23f675cb40850a6121480fad9c Mon Sep 17 00:00:00 2001 From: Zheng Feng Date: Tue, 12 Nov 2024 13:48:15 +0800 Subject: [PATCH 1/3] Fix #6702 to add custom openapi spec locations --- .../reference/extensions/rest-openapi.adoc | 13 ++++++-- .../CamelQuarkusSwaggerCodegenProvider.java | 27 ++++++++++++++-- .../runtime/RestOpenApiBuildTimeConfig.java | 15 +++++++-- .../rest/openapi/it/FruitResource.java | 4 +-- .../rest/openapi/it/model/Fruit.java | 31 ------------------- .../src/main/resources/application.properties | 1 + 6 files changed, 49 insertions(+), 42 deletions(-) delete mode 100644 integration-tests/rest-openapi/src/main/java/org/apache/camel/quarkus/component/rest/openapi/it/model/Fruit.java diff --git a/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc b/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc index 0fb359d61e20..a142ee95106e 100644 --- a/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc +++ b/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc @@ -138,8 +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` @@ -151,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` | @@ -178,6 +179,12 @@ If `true`, use JsonIgnoreProperties(ignoreUnknown = true) annotation in the gene Additional properties to be used in the mustache templates. | `Map` | + +|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 list of the 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 546af5455ffa..d69cf7b95a8a 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 boolean trigger(CodeGenContext context) throws CodeGenException { } try { - List specFiles = new ArrayList<>(); + Set specFiles = new HashSet<>(); if (Files.isDirectory(context.inputDir())) { try (Stream protoFilesPaths = Files.walk(context.inputDir())) { protoFilesPaths @@ -78,6 +80,24 @@ public boolean trigger(CodeGenContext context) throws CodeGenException { } } + Optional 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.warn("Can not find location " + location + " failing with " + e); + } + } + } + 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 boolean trigger(CodeGenContext context) throws CodeGenException { Boolean.class); for (String specFile : specFiles) { + LOG.info("generate models for " + 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 891ff14c265e..854f57394d98 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 @@ -39,8 +39,9 @@ public class RestOpenApiBuildTimeConfig { public static class CodeGenConfig { /** - * 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. * * @asciidoclet */ @@ -56,7 +57,7 @@ public static class CodeGenConfig { 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 */ @@ -94,5 +95,13 @@ public static class CodeGenConfig { */ @ConfigItem public Map additionalProperties; + + /** + * A comma list of the spec locations. + * + * @asciidoclet + */ + @ConfigItem + public Optional 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 73b66b027113..a178327d3cd5 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 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 8b21d8809d86..000000000000 --- 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 01f7a2133a3b..be863666997d 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 From d5131a2790a00c106fb8ed3d4fe3adb5d1647397 Mon Sep 17 00:00:00 2001 From: Zheng Feng Date: Tue, 12 Nov 2024 15:49:17 +0800 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: James Netherton --- .../deployment/CamelQuarkusSwaggerCodegenProvider.java | 4 ++-- .../rest/openapi/runtime/RestOpenApiBuildTimeConfig.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 d69cf7b95a8a..6f8aedf435c9 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 @@ -93,7 +93,7 @@ public boolean trigger(CodeGenContext context) throws CodeGenException { Path path = Path.of(uri); specFiles.add(path.toAbsolutePath().toString()); } catch (Exception e) { - LOG.warn("Can not find location " + location + " failing with " + e); + LOG.warnf(e, "Can not find location %s", location); } } } @@ -106,7 +106,7 @@ public boolean trigger(CodeGenContext context) throws CodeGenException { Boolean.class); for (String specFile : specFiles) { - LOG.info("generate models for " + specFile); + 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 854f57394d98..2a70dae1596c 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 @@ -97,7 +97,7 @@ public static class CodeGenConfig { public Map additionalProperties; /** - * A comma list of the spec locations. + * A comma separated list of OpenAPI spec locations. * * @asciidoclet */ From d67d7bcc03eb752962fe2dd7b32c73d5dd44b216 Mon Sep 17 00:00:00 2001 From: Zheng Feng Date: Tue, 12 Nov 2024 16:17:39 +0800 Subject: [PATCH 3/3] Regen --- docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc b/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc index a142ee95106e..00a36119c232 100644 --- a/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc +++ b/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc @@ -182,7 +182,7 @@ Additional properties to be used in the mustache templates. |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 list of the spec locations. +A comma separated list of OpenAPI spec locations. | `string` | |===