Skip to content

Commit ce7b1b0

Browse files
gwydionmvGwydion Martin
andauthored
Change base directory + add include property (#179)
* Change base directory + add include property * Changes from PR * Changes from PR * Changes from PR * Generalize baseModuleDirectory path * format code * Make shouldRun throw an exception when the path is invalid * Revert "Make shouldRun throw an exception when the path is invalid" This reverts commit c083139. Co-authored-by: Gwydion Martin <[email protected]>
1 parent c85cfff commit ce7b1b0

File tree

21 files changed

+877
-47
lines changed

21 files changed

+877
-47
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,16 @@ You probably already have this configuration if you created your application wit
5353

5454
Now, create the directory `openapi` under your `src/main/` path and add the OpenAPI spec files there. We support JSON, YAML and YML extensions.
5555

56+
If you want to change the directory where OpenAPI files must be found, use the property `quarkus.openapi-generator.codegen.input-base-dir`.
57+
IMPORTANT: it is relative to the project base directory. For example, if you have a project called `MyJavaProject` and decide to place them in `MyJavaProject/openapi-definitions`, use the following property:
58+
59+
```properties
60+
quarkus.openapi-generator.codegen.input-base-dir=openapi-definitions
61+
```
62+
5663
To fine tune the configuration for each spec file, add the following entry to your properties file. In this example, our spec file is in `src/main/openapi/petstore.json`:
5764

5865
```properties
59-
quarkus.openapi-generator.codegen.spec.petstore_json.base-package=org.acme.openapi
6066
quarkus.openapi-generator.codegen.spec.petstore_json.additional-model-type-annotations[email protected];@org.test.Bar
6167
```
6268

@@ -120,16 +126,25 @@ Since the most part of this extension work is in the `generate-code` execution p
120126

121127
For more information, see the [Maven Logging Configuration](https://maven.apache.org/maven-logging.html) guide.
122128

123-
## Ignoring OpenAPI Specification Files
129+
## Filtering OpenAPI Specification Files
124130

125-
To ignore code generation for specific OpenAPI specification files, you can set the `quarkus.openapi-generator.codegen.ignore` property.
131+
By default, the extension will process every OpenAPI specification file in the given path.
132+
To limit code generation to only a specific set of OpenAPI specification files, you can set the `quarkus.openapi-generator.codegen.include` property.
133+
For instance, if you want to limit code generation for `include-openapi.yaml` and `include-openapi-2.yaml` files, you need to define the property like:
126134

127-
For instance, if you want to ignore code generation for `ignored-openapi.yaml` and `ignored-openapi-2.yaml` files, you need to define the `quarkus.openapi-generator.codegen.ignore` property like the following:
135+
```properties
136+
quarkus.openapi-generator.codegen.include=include-openapi.yaml,include-openapi-2.yaml
137+
```
138+
139+
If you prefer to specify which files you want to skip, you can set the `quarkus.openapi-generator.codegen.exclude` property.
140+
For instance, if you want to skip code generation for `exclude-openapi.yaml` and `exclude-openapi-2.yaml` files, you need to define the property like:
128141

129142
```properties
130-
quarkus.openapi-generator.codegen.ignore=ignored-openapi.yaml,ignored-openapi-2.yaml
143+
quarkus.openapi-generator.codegen.exclude=exclude-openapi.yaml,exclude-openapi-2.yaml
131144
```
132145

146+
IMPORTANT: `exclude` supersedes `include`, meaning that if a file is in both property it will NOT be analysed.
147+
133148
See the module [ignore](integration-tests/ignore) for an example of how to use this feature.
134149

135150
## Authentication Support

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CodegenConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class CodegenConfig {
1818
public static final String API_PKG_SUFFIX = ".api";
1919
public static final String MODEL_PKG_SUFFIX = ".model";
2020
public static final String VERBOSE_PROPERTY_NAME = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".verbose";
21+
public static final String INPUT_BASE_DIR = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".input-base-dir";
22+
public static final String INCLUDE_FILES = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".include";
23+
public static final String EXCLUDE_FILES = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".exclude";
2124
public static final String VALIDATE_SPEC_PROPERTY_NAME = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".validateSpec";
2225
public static final String DEFAULT_SECURITY_SCHEME = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".default.security.scheme";
2326
// package visibility for unit tests

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.quarkiverse.openapi.generator.deployment.codegen;
22

3+
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.EXCLUDE_FILES;
4+
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.INCLUDE_FILES;
5+
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.INPUT_BASE_DIR;
36
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.VALIDATE_SPEC_PROPERTY_NAME;
47
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.VERBOSE_PROPERTY_NAME;
58
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getAdditionalModelTypeAnnotationsPropertyName;
@@ -19,6 +22,7 @@
1922
import org.eclipse.microprofile.config.Config;
2023
import org.openapitools.codegen.config.GlobalSettings;
2124

25+
import io.quarkiverse.openapi.generator.OpenApiGeneratorException;
2226
import io.quarkiverse.openapi.generator.deployment.CodegenConfig;
2327
import io.quarkiverse.openapi.generator.deployment.circuitbreaker.CircuitBreakerConfigurationParser;
2428
import io.quarkiverse.openapi.generator.deployment.wrapper.OpenApiClientGeneratorWrapper;
@@ -41,25 +45,47 @@ public abstract class OpenApiGeneratorCodeGenBase implements CodeGenProvider {
4145

4246
private static final String DEFAULT_PACKAGE = "org.openapi.quarkus";
4347

48+
/**
49+
* The input base directory from
50+
*
51+
* <pre>
52+
* src/main
53+
*
54+
* <pre>
55+
* directory.
56+
* Ignored if INPUT_BASE_DIR is specified.
57+
**/
4458
@Override
4559
public String inputDirectory() {
4660
return "openapi";
4761
}
4862

63+
@Override
64+
public boolean shouldRun(Path sourceDir, Config config) {
65+
String inputBaseDir = getInputBaseDirRelativeToModule(sourceDir, config);
66+
if (inputBaseDir != null && !Files.isDirectory(Path.of(inputBaseDir))) {
67+
throw new OpenApiGeneratorException(String.format("Invalid path on %s: %s", INPUT_BASE_DIR, inputBaseDir));
68+
}
69+
return inputBaseDir != null || Files.isDirectory(sourceDir);
70+
}
71+
4972
@Override
5073
public boolean trigger(CodeGenContext context) throws CodeGenException {
5174
final Path outDir = context.outDir();
52-
final Path openApiDir = context.inputDir();
53-
final List<String> ignoredFiles = context.config()
54-
.getOptionalValues("quarkus.openapi-generator.codegen.ignore", String.class).orElse(List.of());
75+
String inputBaseDir = getInputBaseDirRelativeToModule(context.inputDir(), context.config());
76+
final Path openApiDir = inputBaseDir != null ? Path.of(inputBaseDir) : context.inputDir();
77+
final List<String> filesToInclude = context.config().getOptionalValues(INCLUDE_FILES, String.class).orElse(List.of());
78+
final List<String> filesToExclude = context.config().getOptionalValues(EXCLUDE_FILES, String.class).orElse(List.of());
5579

5680
if (Files.isDirectory(openApiDir)) {
5781
try (Stream<Path> openApiFilesPaths = Files.walk(openApiDir)) {
5882
openApiFilesPaths
5983
.filter(Files::isRegularFile)
6084
.filter(path -> {
6185
String fileName = path.getFileName().toString();
62-
return fileName.endsWith(inputExtension()) && !ignoredFiles.contains(fileName);
86+
return fileName.endsWith(inputExtension())
87+
&& !filesToExclude.contains(fileName)
88+
&& (filesToInclude.isEmpty() || filesToInclude.contains(fileName));
6389
})
6490
.forEach(openApiFilePath -> generate(context.config(), openApiFilePath, outDir));
6591
} catch (IOException e) {
@@ -112,4 +138,9 @@ private String getBasePackage(final Config config, final Path openApiFilePath) {
112138
.getOptionalValue(getBasePackagePropertyName(openApiFilePath), String.class)
113139
.orElse(String.format("%s.%s", DEFAULT_PACKAGE, getSanitizedFileName(openApiFilePath)));
114140
}
141+
142+
private String getInputBaseDirRelativeToModule(final Path sourceDir, final Config config) {
143+
String baseModuleDirectory = sourceDir.toString().substring(0, sourceDir.toString().lastIndexOf("src"));
144+
return config.getOptionalValue(INPUT_BASE_DIR, String.class).map(s -> baseModuleDirectory + s).orElse(null);
145+
}
115146
}

deployment/src/test/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapperTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ void shouldReplaceFileImportWithInputStream() throws URISyntaxException, FileNot
352352
.stream()
353353
.map(importDeclaration -> importDeclaration.getName().asString())
354354
.collect(Collectors.toList());
355-
assertThat(imports).contains("java.io.InputStream");
356-
assertThat(imports).doesNotContain("java.io.File");
355+
assertThat(imports).contains("java.io.InputStream")
356+
.doesNotContain("java.io.File");
357357
}
358358

359359
@Test
@@ -446,7 +446,7 @@ void verifyAdditionalModelTypeAnnotations() throws URISyntaxException {
446446

447447
generatedFiles.stream()
448448
.filter(file -> file.getPath().matches(".*/model/.*.java"))
449-
.forEach(file -> verifyModelAdditionalAnnotations(file));
449+
.forEach(this::verifyModelAdditionalAnnotations);
450450
}
451451

452452
private void verifyModelAdditionalAnnotations(File file) {

0 commit comments

Comments
 (0)