Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set use-bean-validation false as default #820

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -123,16 +125,36 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
Optional<String> templateBaseDir = getTemplateBaseDirRelativeToSourceRoot(context.inputDir(), context.config());
Path templateDir = templateBaseDir.map(Path::of)
.orElseGet(() -> context.workDir().resolve("classes").resolve("templates"));
openApiFilesPaths
List<Path> 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);
Expand All @@ -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<String> configurations = StreamSupport.stream(context.config().getPropertyNames().spliterator(), false)
.collect(Collectors.toList());
Expand All @@ -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);
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
Original file line number Diff line number Diff line change
@@ -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};
Expand Down