Skip to content

Commit 7b79801

Browse files
github-actions[bot]mcruzdevhbelmiro
authored
Add validation when the input-base-dir is not present (#928) (#954)
* Add some validations for server configs * Update server/deployment/src/test/java/io/quarkiverse/openapi/server/generator/deployment/CodegenTest.java * Update server/deployment/src/main/java/io/quarkiverse/openapi/server/generator/deployment/codegen/ApicurioOpenApiServerCodegen.java * Update server/deployment/src/main/java/io/quarkiverse/openapi/server/generator/deployment/codegen/ApicurioOpenApiServerCodegen.java --------- Co-authored-by: Matheus Cruz <[email protected]> Co-authored-by: Helber Belmiro <[email protected]>
1 parent 0c957de commit 7b79801

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

server/deployment/src/main/java/io/quarkiverse/openapi/server/generator/deployment/codegen/ApicurioOpenApiServerCodegen.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,20 @@ private Path getInputBaseDir(final Path sourceDir, final Config config) {
4646

4747
@Override
4848
public boolean shouldRun(Path sourceDir, Config config) {
49-
if (config.getOptionalValue(CodegenConfig.getSpecPropertyName(), String.class).isEmpty()) {
50-
return false;
49+
boolean specIsPresent = config.getOptionalValue(CodegenConfig.getSpecPropertyName(), String.class).isPresent();
50+
if (!specIsPresent) {
51+
log.warn("The {} property is not present, the code generation will be ignored",
52+
CodegenConfig.getSpecPropertyName());
5153
}
52-
Path path = getInputBaseDir(sourceDir, config);
53-
return Files.isDirectory(path);
54+
return specIsPresent;
5455
}
5556

5657
@Override
5758
public boolean trigger(CodeGenContext context) throws CodeGenException {
5859
final Path openApiDir = getInputBaseDir(context.inputDir(), context.config());
60+
61+
validateOpenApiDir(context, openApiDir);
62+
5963
final Path outDir = context.outDir();
6064
final ApicurioCodegenWrapper apicurioCodegenWrapper = new ApicurioCodegenWrapper(
6165
context.config(), outDir.toFile());
@@ -92,6 +96,19 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
9296
return true;
9397
}
9498

99+
private static void validateOpenApiDir(CodeGenContext context, Path openApiDir) throws CodeGenException {
100+
if (!Files.exists(openApiDir)) {
101+
throw new CodeGenException(
102+
"The OpenAPI input base directory does not exist. Please create the directory at " + context.inputDir());
103+
}
104+
105+
if (!Files.isDirectory(openApiDir)) {
106+
throw new CodeGenException(
107+
"The OpenAPI input base directory is not a directory. Please create the directory at "
108+
+ context.inputDir());
109+
}
110+
}
111+
95112
private File convertToJSON(Path yamlPath) throws CodeGenException {
96113
try {
97114
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());

server/deployment/src/test/java/io/quarkiverse/openapi/server/generator/deployment/CodegenTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.apache.commons.io.FileUtils;
1111
import org.eclipse.microprofile.config.Config;
12+
import org.junit.jupiter.api.Assertions;
1213
import org.junit.jupiter.api.BeforeAll;
1314
import org.junit.jupiter.api.Test;
1415

@@ -60,4 +61,14 @@ public void testInputDir() throws CodeGenException {
6061
Path.of("target/generated-test-sources/inputDir/io/petstore/PetResource.java")));
6162
}
6263

64+
@Test
65+
public void shouldGenerateAnErrorWhenInputDirIsNotExist() {
66+
Config config = MockConfigUtils.getTestConfig("doesNotExistDir.application.properties");
67+
CodeGenContext codeGenContext = new CodeGenContext(null, Path.of(OUT_DIR, "inputDir"), WORK_DIR,
68+
INPUT_DIR, false, config, true);
69+
ApicurioOpenApiServerCodegen apicurioOpenApiServerCodegen = new ApicurioOpenApiServerCodegen();
70+
71+
Assertions.assertThrows(CodeGenException.class, () -> apicurioOpenApiServerCodegen.trigger(codeGenContext));
72+
}
73+
6374
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
quarkus.openapi.generator.spec=petstore-openapi-2.json
2+
quarkus.openapi.generator.base-package=io.petstore

0 commit comments

Comments
 (0)