Skip to content

Commit

Permalink
Change base directory + add include property (quarkiverse#179)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
gwydionmv and Gwydion Martin authored Dec 2, 2022
1 parent c85cfff commit ce7b1b0
Show file tree
Hide file tree
Showing 21 changed files with 877 additions and 47 deletions.
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ You probably already have this configuration if you created your application wit

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

If you want to change the directory where OpenAPI files must be found, use the property `quarkus.openapi-generator.codegen.input-base-dir`.
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:

```properties
quarkus.openapi-generator.codegen.input-base-dir=openapi-definitions
```

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`:

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

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

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

## Ignoring OpenAPI Specification Files
## Filtering OpenAPI Specification Files

To ignore code generation for specific OpenAPI specification files, you can set the `quarkus.openapi-generator.codegen.ignore` property.
By default, the extension will process every OpenAPI specification file in the given path.
To limit code generation to only a specific set of OpenAPI specification files, you can set the `quarkus.openapi-generator.codegen.include` property.
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:

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:
```properties
quarkus.openapi-generator.codegen.include=include-openapi.yaml,include-openapi-2.yaml
```

If you prefer to specify which files you want to skip, you can set the `quarkus.openapi-generator.codegen.exclude` property.
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:

```properties
quarkus.openapi-generator.codegen.ignore=ignored-openapi.yaml,ignored-openapi-2.yaml
quarkus.openapi-generator.codegen.exclude=exclude-openapi.yaml,exclude-openapi-2.yaml
```

IMPORTANT: `exclude` supersedes `include`, meaning that if a file is in both property it will NOT be analysed.

See the module [ignore](integration-tests/ignore) for an example of how to use this feature.

## Authentication Support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class CodegenConfig {
public static final String API_PKG_SUFFIX = ".api";
public static final String MODEL_PKG_SUFFIX = ".model";
public static final String VERBOSE_PROPERTY_NAME = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".verbose";
public static final String INPUT_BASE_DIR = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".input-base-dir";
public static final String INCLUDE_FILES = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".include";
public static final String EXCLUDE_FILES = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".exclude";
public static final String VALIDATE_SPEC_PROPERTY_NAME = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".validateSpec";
public static final String DEFAULT_SECURITY_SCHEME = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".default.security.scheme";
// package visibility for unit tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.quarkiverse.openapi.generator.deployment.codegen;

import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.EXCLUDE_FILES;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.INCLUDE_FILES;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.INPUT_BASE_DIR;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.VALIDATE_SPEC_PROPERTY_NAME;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.VERBOSE_PROPERTY_NAME;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getAdditionalModelTypeAnnotationsPropertyName;
Expand All @@ -19,6 +22,7 @@
import org.eclipse.microprofile.config.Config;
import org.openapitools.codegen.config.GlobalSettings;

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

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

/**
* The input base directory from
*
* <pre>
* src/main
*
* <pre>
* directory.
* Ignored if INPUT_BASE_DIR is specified.
**/
@Override
public String inputDirectory() {
return "openapi";
}

@Override
public boolean shouldRun(Path sourceDir, Config config) {
String inputBaseDir = getInputBaseDirRelativeToModule(sourceDir, config);
if (inputBaseDir != null && !Files.isDirectory(Path.of(inputBaseDir))) {
throw new OpenApiGeneratorException(String.format("Invalid path on %s: %s", INPUT_BASE_DIR, inputBaseDir));
}
return inputBaseDir != null || Files.isDirectory(sourceDir);
}

@Override
public boolean trigger(CodeGenContext context) throws CodeGenException {
final Path outDir = context.outDir();
final Path openApiDir = context.inputDir();
final List<String> ignoredFiles = context.config()
.getOptionalValues("quarkus.openapi-generator.codegen.ignore", String.class).orElse(List.of());
String inputBaseDir = getInputBaseDirRelativeToModule(context.inputDir(), context.config());
final Path openApiDir = inputBaseDir != null ? Path.of(inputBaseDir) : context.inputDir();
final List<String> filesToInclude = context.config().getOptionalValues(INCLUDE_FILES, String.class).orElse(List.of());
final List<String> filesToExclude = context.config().getOptionalValues(EXCLUDE_FILES, String.class).orElse(List.of());

if (Files.isDirectory(openApiDir)) {
try (Stream<Path> openApiFilesPaths = Files.walk(openApiDir)) {
openApiFilesPaths
.filter(Files::isRegularFile)
.filter(path -> {
String fileName = path.getFileName().toString();
return fileName.endsWith(inputExtension()) && !ignoredFiles.contains(fileName);
return fileName.endsWith(inputExtension())
&& !filesToExclude.contains(fileName)
&& (filesToInclude.isEmpty() || filesToInclude.contains(fileName));
})
.forEach(openApiFilePath -> generate(context.config(), openApiFilePath, outDir));
} catch (IOException e) {
Expand Down Expand Up @@ -112,4 +138,9 @@ private String getBasePackage(final Config config, final Path openApiFilePath) {
.getOptionalValue(getBasePackagePropertyName(openApiFilePath), String.class)
.orElse(String.format("%s.%s", DEFAULT_PACKAGE, getSanitizedFileName(openApiFilePath)));
}

private String getInputBaseDirRelativeToModule(final Path sourceDir, final Config config) {
String baseModuleDirectory = sourceDir.toString().substring(0, sourceDir.toString().lastIndexOf("src"));
return config.getOptionalValue(INPUT_BASE_DIR, String.class).map(s -> baseModuleDirectory + s).orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ void shouldReplaceFileImportWithInputStream() throws URISyntaxException, FileNot
.stream()
.map(importDeclaration -> importDeclaration.getName().asString())
.collect(Collectors.toList());
assertThat(imports).contains("java.io.InputStream");
assertThat(imports).doesNotContain("java.io.File");
assertThat(imports).contains("java.io.InputStream")
.doesNotContain("java.io.File");
}

@Test
Expand Down Expand Up @@ -446,7 +446,7 @@ void verifyAdditionalModelTypeAnnotations() throws URISyntaxException {

generatedFiles.stream()
.filter(file -> file.getPath().matches(".*/model/.*.java"))
.forEach(file -> verifyModelAdditionalAnnotations(file));
.forEach(this::verifyModelAdditionalAnnotations);
}

private void verifyModelAdditionalAnnotations(File file) {
Expand Down
Loading

0 comments on commit ce7b1b0

Please sign in to comment.