Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ enum ConfigName {
//global configs
VERBOSE("verbose"),
INPUT_BASE_DIR("input-base-dir"),
GAV_SCANNING("gav-scanning"),
TEMPLATE_BASE_DIR("template-base-dir"),
INCLUDE("include"),
EXCLUDE("exclude"),
EXCLUDE_GAV("exclude-gav"),
VALIDATE_SPEC("validateSpec"),
DEFAULT_SECURITY_SCHEME("default-security-scheme"),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.quarkiverse.openapi.generator.deployment.codegen;

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

import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.jboss.logging.Logger;

import io.quarkus.bootstrap.prebuild.CodeGenException;
import io.quarkus.deployment.CodeGenContext;
import io.quarkus.maven.dependency.ResolvedDependency;

public class GAVCoordinateOpenApiSpecInputProvider implements OpenApiSpecInputProvider {
private static final Logger LOG = Logger.getLogger(GAVCoordinateOpenApiSpecInputProvider.class);

private static final Set<String> SUPPORTED_EXTENSIONS = Set.of("yaml", "yml", "json");

@Override
public List<SpecInputModel> read(CodeGenContext context) throws CodeGenException {
if (!context.config().getOptionalValue(getGlobalConfigName(GAV_SCANNING), Boolean.class)
.orElse(true)) {
LOG.debug("GAV scanning is disabled.");
return List.of();
}

// Q: maybe a configuration property to enable GAV scanning (default: true)
List<String> gavsToExclude = context.config().getOptionalValues(getGlobalConfigName(EXCLUDE_GAV), String.class)
.orElse(List.of());

List<ResolvedDependency> yamlDependencies = context.applicationModel().getDependencies().stream()
.filter(rd -> SUPPORTED_EXTENSIONS.contains(rd.getType()))
.filter(rd -> !gavsToExclude.contains(rd.getKey().toGacString()))
.toList();

if (yamlDependencies.isEmpty()) {
LOG.debug("No suitable GAV dependencies found.");
return List.of();
}
var inputModels = new ArrayList<SpecInputModel>();
for (ResolvedDependency yamlDependency : yamlDependencies) {
var gacString = yamlDependency.getKey().toGacString();
var path = yamlDependency.getResolvedPaths().stream().findFirst()
.orElseThrow(() -> new CodeGenException("Could not find maven path of %s.".formatted(gacString)));
try {
inputModels.add(new SpecInputModel(gacString, Files.newInputStream(path)));
} catch (IOException e) {
throw new CodeGenException("Could not open input stream of %s from %s.".formatted(gacString, path.toString()),
e);
}
}
return inputModels;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.InputStream;
import java.util.List;

import io.quarkus.bootstrap.prebuild.CodeGenException;
import io.quarkus.deployment.CodeGenContext;

/**
Expand All @@ -13,8 +14,10 @@ public interface OpenApiSpecInputProvider {
/**
* Fetch OpenAPI specification files from a given source.
*
* @param context the current codegen context.
* @throws CodeGenException if an error occurs while reading the spec files.
* @return a list of spec files in {@link InputStream} format.
*/
List<SpecInputModel> read(CodeGenContext context);
List<SpecInputModel> read(CodeGenContext context) throws CodeGenException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkiverse.openapi.generator.deployment.codegen.GAVCoordinateOpenApiSpecInputProvider
53 changes: 53 additions & 0 deletions client/integration-tests/gav-source/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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>3.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-openapi-generator-gav-source</artifactId>
<name>Quarkus - OpenAPI Generator - Integration Tests - Client - GAV source</name>
<description>Example project for OpenAPI with GAV source</description>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>install-echo1</id>
<goals>
<goal>install-file</goal>
</goals>
<phase>install</phase>
<configuration>
<artifactId>quarkus-openapi-generator-gav-source-echo1</artifactId>
<groupId>io.quarkiverse.openapi.generator</groupId>
<version>3.0.0-SNAPSHOT</version>
<file>src/main/openapi/echo1.yaml</file>
<packaging>yaml</packaging>
</configuration>
</execution>
<execution>
<id>install-echo2</id>
<goals>
<goal>install-file</goal>
</goals>
<phase>install</phase>
<configuration>
<artifactId>quarkus-openapi-generator-gav-source-echo2</artifactId>
<groupId>io.quarkiverse.openapi.generator</groupId>
<version>3.0.0-SNAPSHOT</version>
<file>src/main/openapi/echo2.yaml</file>
<packaging>yaml</packaging>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
34 changes: 34 additions & 0 deletions client/integration-tests/gav-source/src/main/openapi/echo1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
openapi: 3.0.3
info:
title: echo
version: '1.0.0'
description: ""
paths:
/echo:
post:
summary: Echo
operationId: echo
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Message"
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Echo'
components:
schemas:
Echo:
type: object
properties:
echo:
type: string
Message:
type: object
properties:
message:
type: string
34 changes: 34 additions & 0 deletions client/integration-tests/gav-source/src/main/openapi/echo2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
openapi: 3.0.3
info:
title: echo
version: '1.0.0'
description: ""
paths:
/echo:
post:
summary: Echo
operationId: echo
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Message"
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Echo'
components:
schemas:
Echo:
type: object
properties:
echo:
type: string
Message:
type: object
properties:
message:
type: string
105 changes: 105 additions & 0 deletions client/integration-tests/gav/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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>3.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-openapi-generator-it-gav</artifactId>
<name>Quarkus - OpenAPI Generator - Integration Tests - Client - GAV</name>
<description>Example project for OpenAPI via GAV coordinates</description>

<dependencies>
<dependency>
<groupId>io.quarkiverse.openapi.generator</groupId>
<artifactId>quarkus-openapi-generator</artifactId>
</dependency>
<!-- include yaml GAV coordinates -->
<!-- echo1 should be generated -->
<dependency>
<groupId>io.quarkiverse.openapi.generator</groupId>
<artifactId>quarkus-openapi-generator-gav-source-echo1</artifactId>
<version>3.0.0-SNAPSHOT</version>
<type>yaml</type>
</dependency>
<!-- echo2 will be excluded during test -->
<dependency>
<groupId>io.quarkiverse.openapi.generator</groupId>
<artifactId>quarkus-openapi-generator-gav-source-echo2</artifactId>
<version>3.0.0-SNAPSHOT</version>
<type>yaml</type>
</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
quarkus.keycloak.devservices.enabled=false

quarkus.openapi-generator.codegen.exclude-gav=io.quarkiverse.openapi.generator:quarkus-openapi-generator-gav-source-echo2
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkiverse.openapi.generator.it;

import static org.assertj.core.api.Assertions.*;

import org.junit.jupiter.api.Test;

class QuarkusGAVOpenApiTest {
@Test
void apiIsBeingGenerated() {
assertThatCode(
() -> Class.forName(
"org.openapi.quarkus.io_quarkiverse_openapi_generator_quarkus_openapi_generator_gav_source_echo1.api.DefaultApi"))
.doesNotThrowAnyException();
}

@Test
void apiIsBeingNotGenerated() {
assertThatCode(
() -> Class.forName(
"org.openapi.quarkus.io_quarkiverse_openapi_generator_quarkus_openapi_generator_gav_source_echo2.api.DefaultApi"))
.isInstanceOf(ClassNotFoundException.class);
}
}
2 changes: 2 additions & 0 deletions client/integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<module>cookie-authentication</module>
<module>custom-templates</module>
<module>enum-property</module>
<module>gav-source</module>
<module>gav</module>
<module>enum-unexpected</module>
<module>exclude</module>
<module>generate-flags</module>
Expand Down
Loading