Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #6702 to add custom openapi spec locations #6769

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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`
|

Expand All @@ -178,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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,7 +67,7 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
}

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
Expand All @@ -78,6 +80,24 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
}
}

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);
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -94,5 +95,13 @@ public static class CodeGenConfig {
*/
@ConfigItem
public Map<String, String> additionalProperties;

/**
* A comma separated list of OpenAPI spec locations.
*
* @asciidoclet
*/
@ConfigItem
public Optional<String> locations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down