Skip to content

Commit ff58dfc

Browse files
Allow to skip spec validation (#123)
By adding a new application property, validateSpec, it will now be possible to bypass the Open API validation before executing the codegen. This is specifically important for scenarios where trying to integrate with API specs that are not fully compliant.
1 parent 5e3b014 commit ff58dfc

File tree

11 files changed

+27636
-3
lines changed

11 files changed

+27636
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,10 @@ public interface DefaultApi {
579579
}
580580
````
581581

582+
## Skip OpenAPI schema validation
583+
584+
Use the property key `quarkus.openapi-generator.codegen.validateSpec=false` to disable validating the input specification file before code generation. By default, invalid specifications will result in an error.
585+
582586
## Known Limitations
583587

584588
These are the known limitations of this pre-release version:

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ 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 VALIDATE_SPEC_PROPERTY_NAME = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".validateSpec";
2122
public static final String DEFAULT_SECURITY_SCHEME = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".default.security.scheme";
2223
// package visibility for unit tests
2324
static final String BUILD_TIME_SPEC_PREFIX_FORMAT = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".spec.%s";
@@ -38,6 +39,13 @@ public class CodegenConfig {
3839
*/
3940
@ConfigItem(name = "verbose", defaultValue = "false")
4041
public boolean verbose;
42+
43+
/**
44+
* Whether or not to skip validating the input spec prior to generation. By default, invalid specifications will result in
45+
* an error.
46+
*/
47+
@ConfigItem(name = "validateSpec", defaultValue = "true")
48+
public boolean validateSpec;
4149
/**
4250
* Security type for which security constraints should be created automatically if not explicitly defined
4351
*/

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

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

3+
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.VALIDATE_SPEC_PROPERTY_NAME;
34
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.VERBOSE_PROPERTY_NAME;
45
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getAdditionalModelTypeAnnotationsPropertyName;
56
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getBasePackagePropertyName;
@@ -68,13 +69,15 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
6869
protected void generate(final Config config, final Path openApiFilePath, final Path outDir) {
6970
final String basePackage = getBasePackage(config, openApiFilePath);
7071
final Boolean verbose = config.getOptionalValue(VERBOSE_PROPERTY_NAME, Boolean.class).orElse(false);
72+
final Boolean validateSpec = config.getOptionalValue(VALIDATE_SPEC_PROPERTY_NAME, Boolean.class).orElse(true);
7173
GlobalSettings.setProperty(OpenApiClientGeneratorWrapper.DEFAULT_SECURITY_SCHEME,
7274
config.getOptionalValue(CodegenConfig.DEFAULT_SECURITY_SCHEME, String.class).orElse(""));
7375

7476
final OpenApiClientGeneratorWrapper generator = new OpenApiClientGeneratorWrapper(
7577
openApiFilePath.normalize(),
7678
outDir,
77-
verbose)
79+
verbose,
80+
validateSpec)
7881
.withModelCodeGenConfig(ModelCodegenConfigParser.parse(config, basePackage))
7982
.withCircuitBreakerConfig(CircuitBreakerConfigurationParser.parse(
8083
config));

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public class OpenApiClientGeneratorWrapper {
4040
private String apiPackage = "";
4141
private String modelPackage = "";
4242

43-
public OpenApiClientGeneratorWrapper(final Path specFilePath, final Path outputDir, final boolean verbose) {
43+
public OpenApiClientGeneratorWrapper(final Path specFilePath, final Path outputDir, final boolean verbose,
44+
final boolean validateSpec) {
4445
// do not generate docs nor tests
4546
GlobalSettings.setProperty(CodegenConstants.API_DOCS, FALSE.toString());
4647
GlobalSettings.setProperty(CodegenConstants.API_TESTS, FALSE.toString());
@@ -60,6 +61,7 @@ public OpenApiClientGeneratorWrapper(final Path specFilePath, final Path outputD
6061
this.configurator.addAdditionalProperty("quarkus-generator",
6162
Collections.singletonMap("openApiSpecId", getSanitizedFileName(specFilePath)));
6263
this.configurator.addAdditionalProperty("openApiNullable", false);
64+
this.configurator.setValidateSpec(validateSpec);
6365
this.generator = new DefaultGenerator();
6466
}
6567

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ private OpenApiClientGeneratorWrapper createGeneratorWrapper(String specFileName
309309
.of(requireNonNull(this.getClass().getResource(String.format("/openapi/%s", specFileName))).toURI());
310310
final Path targetPath = Paths.get(getTargetDir(), "openapi-gen");
311311

312-
return new OpenApiClientGeneratorWrapper(openApiSpec, targetPath, false);
312+
return new OpenApiClientGeneratorWrapper(openApiSpec, targetPath, false, true);
313313
}
314314

315315
private String getTargetDir() throws URISyntaxException {

integration-tests/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<packaging>pom</packaging>
1212
<modules>
1313
<module>example-project</module>
14+
<module>test-project</module>
1415
<module>generation-tests</module>
1516
<module>generation-input</module>
1617
</modules>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<parent>
4+
<artifactId>quarkus-openapi-generator-integration-tests</artifactId>
5+
<groupId>io.quarkiverse.openapi.generator</groupId>
6+
<version>1.0.0-SNAPSHOT</version>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
10+
<artifactId>quarkus-openapi-generator-it-test-project</artifactId>
11+
<name>Quarkus - Openapi Generator - Integration Tests - Test Project</name>
12+
<description>Test project for general usage</description>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>io.quarkus</groupId>
17+
<artifactId>quarkus-resteasy</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>io.quarkiverse.openapi.generator</groupId>
21+
<artifactId>quarkus-openapi-generator</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>io.quarkus</groupId>
25+
<artifactId>quarkus-junit5</artifactId>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.github.tomakehurst</groupId>
30+
<artifactId>wiremock-jre8</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>io.quarkus</groupId>
38+
<artifactId>quarkus-maven-plugin</artifactId>
39+
<extensions>true</extensions>
40+
<executions>
41+
<execution>
42+
<goals>
43+
<goal>build</goal>
44+
<goal>generate-code</goal>
45+
<goal>generate-code-tests</goal>
46+
</goals>
47+
</execution>
48+
</executions>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
<profiles>
53+
<profile>
54+
<id>native-image</id>
55+
<activation>
56+
<property>
57+
<name>native</name>
58+
</property>
59+
</activation>
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<artifactId>maven-surefire-plugin</artifactId>
64+
<configuration>
65+
<skipTests>${native.surefire.skip}</skipTests>
66+
</configuration>
67+
</plugin>
68+
<plugin>
69+
<artifactId>maven-failsafe-plugin</artifactId>
70+
<executions>
71+
<execution>
72+
<goals>
73+
<goal>integration-test</goal>
74+
<goal>verify</goal>
75+
</goals>
76+
<configuration>
77+
<systemPropertyVariables>
78+
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
79+
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
80+
<maven.home>${maven.home}</maven.home>
81+
</systemPropertyVariables>
82+
</configuration>
83+
</execution>
84+
</executions>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
<properties>
89+
<quarkus.package.type>native</quarkus.package.type>
90+
</properties>
91+
</profile>
92+
</profiles>
93+
94+
</project>

0 commit comments

Comments
 (0)