From 5eb1b186a4dea8476fb88c6a13396dd026d8e231 Mon Sep 17 00:00:00 2001 From: Matheus Cruz <56329339+mcruzdev@users.noreply.github.com> Date: Wed, 16 Oct 2024 18:52:01 -0300 Subject: [PATCH] Set use-bean-validation false as default (#820) * Set use-bean-validation false as default * Remove unnused symbol --- .../deployment/OpenApiGeneratorOptions.java | 13 ++++++ .../codegen/OpenApiGeneratorCodeGenBase.java | 44 ++++++++++++++++--- .../OpenApiGeneratorStreamCodeGen.java | 12 ++++- .../wrapper/QuarkusJavaClientCodegen.java | 3 +- .../templates/libraries/microprofile/api.qute | 2 + .../libraries/microprofile/model.qute | 2 + 6 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/OpenApiGeneratorOptions.java diff --git a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/OpenApiGeneratorOptions.java b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/OpenApiGeneratorOptions.java new file mode 100644 index 000000000..87d545d9c --- /dev/null +++ b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/OpenApiGeneratorOptions.java @@ -0,0 +1,13 @@ +package io.quarkiverse.openapi.generator.deployment; + +import java.nio.file.Path; + +import org.eclipse.microprofile.config.Config; + +public record OpenApiGeneratorOptions( + Config config, + Path openApiFilePath, + Path outDir, + Path templateDir, + boolean isRestEasyReactive) { +} diff --git a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java index b00beb835..c2b0dd8f0 100644 --- a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java +++ b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java @@ -32,6 +32,7 @@ import org.openapitools.codegen.config.GlobalSettings; import io.quarkiverse.openapi.generator.deployment.CodegenConfig; +import io.quarkiverse.openapi.generator.deployment.OpenApiGeneratorOptions; import io.quarkiverse.openapi.generator.deployment.circuitbreaker.CircuitBreakerConfigurationParser; import io.quarkiverse.openapi.generator.deployment.wrapper.OpenApiClassicClientGeneratorWrapper; import io.quarkiverse.openapi.generator.deployment.wrapper.OpenApiClientGeneratorWrapper; @@ -109,6 +110,7 @@ public boolean trigger(CodeGenContext context) throws CodeGenException { if (Files.isDirectory(openApiDir)) { final boolean isRestEasyReactive = isRestEasyReactive(context); + boolean isHibernateValidatorPresent = isHibernateValidatorPresent(context); if (isRestEasyReactive) { if (!isJacksonReactiveClientPresent(context)) { @@ -123,16 +125,36 @@ public boolean trigger(CodeGenContext context) throws CodeGenException { Optional templateBaseDir = getTemplateBaseDirRelativeToSourceRoot(context.inputDir(), context.config()); Path templateDir = templateBaseDir.map(Path::of) .orElseGet(() -> context.workDir().resolve("classes").resolve("templates")); - openApiFilesPaths + List openApiPaths = openApiFilesPaths .filter(Files::isRegularFile) .filter(path -> { String fileName = path.getFileName().toString(); return fileName.endsWith(inputExtension()) && !filesToExclude.contains(fileName) && (filesToInclude.isEmpty() || filesToInclude.contains(fileName)); - }) - .forEach(openApiFilePath -> generate(context.config(), openApiFilePath, outDir, templateDir, - isRestEasyReactive)); + }).toList(); + + for (Path openApiPath : openApiPaths) { + + Boolean usingBeanValidation = getValues(context.config(), openApiPath, + CodegenConfig.ConfigName.BEAN_VALIDATION, Boolean.class) + .orElse(false); + + if (usingBeanValidation && !isHibernateValidatorPresent) { + throw new CodeGenException( + "You need to add io.quarkus:quarkus-hibernate-validator to your dependencies."); + } + + OpenApiGeneratorOptions options = new OpenApiGeneratorOptions( + context.config(), + openApiPath, + outDir, + templateDir, + isRestEasyReactive); + + generate(options); + } + } catch (IOException e) { throw new CodeGenException("Failed to generate java files from OpenApi files in " + openApiDir.toAbsolutePath(), e); @@ -150,6 +172,10 @@ private static boolean isJacksonClassicClientPresent(CodeGenContext context) { return isExtensionCapabilityPresent(context, Capability.RESTEASY_JSON_JACKSON_CLIENT); } + protected static boolean isHibernateValidatorPresent(CodeGenContext context) { + return isExtensionCapabilityPresent(context, Capability.HIBERNATE_VALIDATOR); + } + private void validateUserConfiguration(CodeGenContext context) throws CodeGenException { List configurations = StreamSupport.stream(context.config().getPropertyNames().spliterator(), false) .collect(Collectors.toList()); @@ -171,8 +197,12 @@ private static String determineRestClientReactiveJacksonCapabilityId() { } // TODO: do not generate if the output dir has generated files and the openapi file has the same checksum of the previous run - protected void generate(final Config config, final Path openApiFilePath, final Path outDir, - Path templateDir, boolean isRestEasyReactive) { + protected void generate(OpenApiGeneratorOptions options) { + Config config = options.config(); + Path openApiFilePath = options.openApiFilePath(); + Path outDir = options.outDir(); + boolean isRestEasyReactive = options.isRestEasyReactive(); + final String basePackage = getBasePackage(config, openApiFilePath); final Boolean verbose = config.getOptionalValue(getGlobalConfigName(CodegenConfig.ConfigName.VERBOSE), Boolean.class) .orElse(false); @@ -183,7 +213,7 @@ protected void generate(final Config config, final Path openApiFilePath, final P final OpenApiClientGeneratorWrapper generator = createGeneratorWrapper(openApiFilePath, outDir, isRestEasyReactive, verbose, validateSpec); - generator.withTemplateDir(templateDir); + generator.withTemplateDir(options.templateDir()); generator.withClassesCodeGenConfig(ClassCodegenConfigParser.parse(config, basePackage)) .withCircuitBreakerConfig(CircuitBreakerConfigurationParser.parse( diff --git a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorStreamCodeGen.java b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorStreamCodeGen.java index 04ef4acef..023d5652a 100644 --- a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorStreamCodeGen.java +++ b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorStreamCodeGen.java @@ -19,6 +19,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import io.quarkiverse.openapi.generator.deployment.OpenApiGeneratorOptions; import io.quarkus.bootstrap.prebuild.CodeGenException; import io.quarkus.deployment.CodeGenContext; import io.smallrye.config.SmallRyeConfigBuilder; @@ -74,8 +75,15 @@ public boolean trigger(CodeGenContext context) throws CodeGenException { StandardOpenOption.CREATE)) { outChannel.transferFrom(inChannel, 0, Integer.MAX_VALUE); LOGGER.debug("Saved OpenAPI spec input model in {}", openApiFilePath); - this.generate(this.mergeConfig(context, inputModel), openApiFilePath, outDir, - context.workDir().resolve("classes").resolve("templates"), isRestEasyReactive); + + OpenApiGeneratorOptions options = new OpenApiGeneratorOptions( + this.mergeConfig(context, inputModel), + openApiFilePath, + outDir, + context.workDir().resolve("classes").resolve("templates"), + isRestEasyReactive); + + this.generate(options); generated = true; } } catch (IOException e) { diff --git a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/QuarkusJavaClientCodegen.java b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/QuarkusJavaClientCodegen.java index 30747594c..ace6f5b97 100644 --- a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/QuarkusJavaClientCodegen.java +++ b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/QuarkusJavaClientCodegen.java @@ -53,7 +53,8 @@ public void processOpts() { this.testFolder = ""; this.embeddedTemplateDir = "templates"; - Boolean beanValidation = (Boolean) this.additionalProperties.getOrDefault("use-bean-validation", true); + Boolean beanValidation = (Boolean) this.additionalProperties.getOrDefault("use-bean-validation", false); + this.setUseBeanValidation(beanValidation); this.setPerformBeanValidation(beanValidation); diff --git a/client/deployment/src/main/resources/templates/libraries/microprofile/api.qute b/client/deployment/src/main/resources/templates/libraries/microprofile/api.qute index 3d291e43d..738a52b08 100644 --- a/client/deployment/src/main/resources/templates/libraries/microprofile/api.qute +++ b/client/deployment/src/main/resources/templates/libraries/microprofile/api.qute @@ -3,8 +3,10 @@ package {package}; import java.util.List; import java.util.Map; +{#if use-bean-validation} {! https://github.com/OpenAPITools/openapi-generator/issues/18974 !} import jakarta.validation.Valid; +{/if} {#for imp in imports} import {imp.import}; diff --git a/client/deployment/src/main/resources/templates/libraries/microprofile/model.qute b/client/deployment/src/main/resources/templates/libraries/microprofile/model.qute index cf121cebe..7b266fdc1 100644 --- a/client/deployment/src/main/resources/templates/libraries/microprofile/model.qute +++ b/client/deployment/src/main/resources/templates/libraries/microprofile/model.qute @@ -1,7 +1,9 @@ package {package}; +{#if use-bean-validation} {! https://github.com/OpenAPITools/openapi-generator/issues/18974 !} import jakarta.validation.Valid; +{/if} {#for imp in imports} import {imp.import};