Skip to content

Commit

Permalink
[issue-39] Added feature to ignore code generation for specific OpenA… (
Browse files Browse the repository at this point in the history
quarkiverse#171)

* [issue-39] Added feature to ignore code generation for specific OpenAPI documents

Signed-off-by: Helber Belmiro <[email protected]>

* [issue-39] Improved loop

Signed-off-by: Helber Belmiro <[email protected]>

Signed-off-by: Helber Belmiro <[email protected]>
  • Loading branch information
hbelmiro authored Nov 14, 2022
1 parent 2ca2af1 commit 92b5a96
Show file tree
Hide file tree
Showing 9 changed files with 456 additions and 7 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ 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

To ignore code generation for specific OpenAPI specification files, you can set the `quarkus.openapi-generator.codegen.ignore` property.

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.ignore=ignored-openapi.yaml,ignored-openapi-2.yaml
```

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

## Authentication Support

If your OpenAPI specification file has `securitySchemes` [definitions](https://spec.openapis.org/oas/v3.1.0#security-scheme-object), the inner generator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package io.quarkiverse.openapi.generator.deployment.codegen;

import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;

import org.eclipse.microprofile.config.Config;
Expand All @@ -18,6 +17,16 @@
import io.quarkus.deployment.CodeGenProvider;
import io.smallrye.config.SmallRyeConfig;

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;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getBasePackagePropertyName;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getCustomRegisterProvidersFormat;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getImportMappingsPropertyName;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getSanitizedFileName;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getSkipFormModelPropertyName;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getTypeMappingsPropertyName;

/**
* Code generation for OpenApi Client. Generates Java classes from OpenApi spec files located in src/main/openapi or
* src/test/openapi
Expand All @@ -41,16 +50,18 @@ public String inputDirectory() {
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());

if (Files.isDirectory(openApiDir)) {
try (Stream<Path> openApiFilesPaths = Files.walk(openApiDir)) {
openApiFilesPaths
.filter(Files::isRegularFile)
.map(Path::toString)
.filter(s -> s.endsWith(this.inputExtension()))
.map(Path::of).forEach(openApiFilePath -> {
this.generate(context.config(), openApiFilePath, outDir);
});
.filter(path -> {
String fileName = path.getFileName().toString();
return fileName.endsWith(inputExtension()) && !ignoredFiles.contains(fileName);
})
.forEach(openApiFilePath -> generate(context.config(), openApiFilePath, outDir));
} catch (IOException e) {
throw new CodeGenException("Failed to generate java files from OpenApi files in " + openApiDir.toAbsolutePath(),
e);
Expand Down
90 changes: 90 additions & 0 deletions integration-tests/ignore/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-openapi-generator-integration-tests</artifactId>
<groupId>io.quarkiverse.openapi.generator</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-openapi-generator-it-ignore</artifactId>
<name>Quarkus - Openapi Generator - Integration Tests - Ignore</name>
<description>Example project with OpenAPI documents that should be ignored</description>

<dependencies>
<dependency>
<groupId>io.quarkiverse.openapi.generator</groupId>
<artifactId>quarkus-openapi-generator</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${native.surefire.skip}</skipTests>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>

</project>
18 changes: 18 additions & 0 deletions integration-tests/ignore/src/main/openapi/ignored-openapi-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
openapi: 3.0.3
info:
title: greeting-flow API
version: "1.0"
paths:
/hello_ignored_2:
get:
tags:
- Ignored OpenAPI Resource 2
operationId: hello_ignored_2
responses:
"200":
description: OK
content:
text/plain:
schema:
type: string
18 changes: 18 additions & 0 deletions integration-tests/ignore/src/main/openapi/ignored-openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
openapi: 3.0.3
info:
title: greeting-flow API
version: "1.0"
paths:
/hello_ignored:
get:
tags:
- Ignored OpenAPI Resource
operationId: hello_ignored
responses:
"200":
description: OK
content:
text/plain:
schema:
type: string
Loading

0 comments on commit 92b5a96

Please sign in to comment.