diff --git a/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc b/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc index fee26616da02..bfcaeb004b96 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` | + +|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 546af5455ffa..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 @@ -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.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 boolean trigger(CodeGenContext context) throws CodeGenException { 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 fcebc9e0a5d5..d6c107aa818e 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 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 */ @ConfigItem public Optional models; @@ -80,6 +85,14 @@ public static class CodeGenConfig { */ @ConfigItem public Map additionalProperties; + + /** + * A comma separated list of OpenAPI 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