Skip to content

Commit

Permalink
Fix #6702 to add custom openapi spec locations (#6769)
Browse files Browse the repository at this point in the history
* Fix #6702 to add custom openapi spec locations

* Apply suggestions from code review

Co-authored-by: James Netherton <jamesnetherton@users.noreply.github.com>

* Regen

---------

Co-authored-by: James Netherton <jamesnetherton@users.noreply.github.com>
  • Loading branch information
2 people authored and JiriOndrusek committed Nov 13, 2024
1 parent 57463d8 commit 12aec55
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 41 deletions.
12 changes: 10 additions & 2 deletions docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`

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

Expand All @@ -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]
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 @@ -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;
Expand All @@ -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<String> models;
Expand Down Expand Up @@ -80,6 +85,14 @@ 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

0 comments on commit 12aec55

Please sign in to comment.