diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 62cf724e..6b1b3d50 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,15 +1,14 @@ [versions] -micronaut = "4.3.7" -micronaut-docs = "2.0.0" managed-hibernate-validator = '8.0.1.Final' +groovy = "4.0.18" + +micronaut = "4.3.7" +micronaut-docs = "2.0.0" micronaut-serde = "2.8.1" -micronaut-test = "4.0.0" +micronaut-test = "4.2.1" micronaut-validation = "4.4.0" -groovy = "4.0.13" - - [libraries] # Core micronaut-core = { module = 'io.micronaut:micronaut-core-bom', version.ref = 'micronaut' } diff --git a/gradlew.bat b/gradlew.bat index 93e3f59f..25da30db 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 if %ERRORLEVEL% equ 0 goto execute -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto execute -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail diff --git a/hibernate-validator/build.gradle.kts b/hibernate-validator/build.gradle.kts index f8e1cba6..ee8f912a 100644 --- a/hibernate-validator/build.gradle.kts +++ b/hibernate-validator/build.gradle.kts @@ -5,16 +5,22 @@ plugins { dependencies { annotationProcessor(mn.micronaut.graal) annotationProcessor(mn.micronaut.inject.java) + compileOnly(mn.micronaut.router) + api(libs.hibernate.validator) + implementation(mn.micronaut.inject) implementation(mnValidation.micronaut.validation) testAnnotationProcessor(mnValidation.micronaut.validation.processor) - testCompileOnly(mnValidation.micronaut.validation.processor) testAnnotationProcessor(mn.micronaut.inject.java) + + testCompileOnly(mnValidation.micronaut.validation.processor) testCompileOnly(mn.micronaut.inject.groovy) + testImplementation(mn.micronaut.http.server.netty) - testRuntimeOnly(mn.snakeyaml) testImplementation(mnSerde.micronaut.serde.jackson) + + testRuntimeOnly(mn.snakeyaml) } diff --git a/hibernate-validator/src/main/java/io/micronaut/configuration/hibernate/validator/DefaultParameterNameProvider.java b/hibernate-validator/src/main/java/io/micronaut/configuration/hibernate/validator/DefaultParameterNameProvider.java index 469ab7ee..312f4acd 100644 --- a/hibernate-validator/src/main/java/io/micronaut/configuration/hibernate/validator/DefaultParameterNameProvider.java +++ b/hibernate-validator/src/main/java/io/micronaut/configuration/hibernate/validator/DefaultParameterNameProvider.java @@ -15,15 +15,6 @@ */ package io.micronaut.configuration.hibernate.validator; -import io.micronaut.context.BeanContext; -import io.micronaut.core.type.Argument; -import io.micronaut.core.util.CollectionUtils; -import io.micronaut.inject.DelegatingBeanDefinition; -import io.micronaut.inject.ExecutableMethod; - -import jakarta.inject.Singleton; - -import jakarta.validation.ParameterNameProvider; import java.lang.reflect.Constructor; import java.lang.reflect.Executable; import java.lang.reflect.Method; @@ -36,6 +27,14 @@ import java.util.Set; import java.util.stream.Collectors; +import io.micronaut.context.BeanContext; +import io.micronaut.core.type.Argument; +import io.micronaut.inject.DelegatingBeanDefinition; +import io.micronaut.inject.ExecutableMethod; + +import jakarta.inject.Singleton; +import jakarta.validation.ParameterNameProvider; + /** * Default implementation of the {@link ParameterNameProvider} interface that. * @@ -45,7 +44,7 @@ @Singleton public class DefaultParameterNameProvider implements ParameterNameProvider { - private static final Set INTERNAL_CLASS_NAMES = CollectionUtils.setOf(Object.class.getName(), "groovy.lang.GroovyObject"); + private static final Set INTERNAL_CLASS_NAMES = Set.of(Object.class.getName(), "groovy.lang.GroovyObject"); private final BeanContext beanContext; /** @@ -66,8 +65,8 @@ public List getParameterNames(Constructor constructor) { return beanContext.getBeanDefinitions(declaringClass) .stream() .map(bd -> { - if (bd instanceof DelegatingBeanDefinition) { - return ((DelegatingBeanDefinition) bd).getTarget(); + if (bd instanceof DelegatingBeanDefinition dbd) { + return dbd.getTarget(); } return bd; }) @@ -98,7 +97,7 @@ public List getParameterNames(Method method) { * @return list of strings */ protected List defaultParameterTypes(Class[] parameterTypes) { - List names = new ArrayList<>(parameterTypes.length); + var names = new ArrayList(parameterTypes.length); for (int i = 0; i < parameterTypes.length; i++) { names.add("arg" + i); } @@ -107,7 +106,7 @@ protected List defaultParameterTypes(Class[] parameterTypes) { private List doGetParameterNames(Executable executable) { Parameter[] parameters = executable.getParameters(); - List parameterNames = new ArrayList<>(parameters.length); + var parameterNames = new ArrayList(parameters.length); for (Parameter parameter : parameters) { parameterNames.add(parameter.getName()); diff --git a/hibernate-validator/src/main/java/io/micronaut/configuration/hibernate/validator/MicronautHibernateValidator.java b/hibernate-validator/src/main/java/io/micronaut/configuration/hibernate/validator/MicronautHibernateValidator.java index ff92423d..1d0f3fe5 100644 --- a/hibernate-validator/src/main/java/io/micronaut/configuration/hibernate/validator/MicronautHibernateValidator.java +++ b/hibernate-validator/src/main/java/io/micronaut/configuration/hibernate/validator/MicronautHibernateValidator.java @@ -15,6 +15,10 @@ */ package io.micronaut.configuration.hibernate.validator; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.Set; + import io.micronaut.context.BeanResolutionContext; import io.micronaut.context.annotation.Primary; import io.micronaut.context.annotation.Replaces; @@ -24,21 +28,14 @@ import io.micronaut.core.annotation.Nullable; import io.micronaut.core.util.StringUtils; import io.micronaut.inject.BeanDefinition; -import io.micronaut.inject.annotation.AnnotatedElementValidator; -import io.micronaut.inject.validation.BeanDefinitionValidator; import io.micronaut.validation.validator.DefaultValidator; -import io.micronaut.validation.validator.ExecutableMethodValidator; -import io.micronaut.validation.validator.ReactiveValidator; -import io.micronaut.validation.validator.Validator; import io.micronaut.validation.validator.ValidatorConfiguration; import jakarta.inject.Singleton; import jakarta.validation.ConstraintViolation; +import jakarta.validation.Validator; import jakarta.validation.ValidatorFactory; import jakarta.validation.metadata.BeanDescriptor; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.util.Set; /** * Replaces Micronaut Validator with Hibernate backed implementation. @@ -50,9 +47,9 @@ @Primary @Requires(property = ValidatorConfiguration.ENABLED, value = StringUtils.TRUE, defaultValue = StringUtils.TRUE) @Replaces(DefaultValidator.class) -public class MicronautHibernateValidator extends DefaultValidator implements Validator, ExecutableMethodValidator, ReactiveValidator, AnnotatedElementValidator, BeanDefinitionValidator { +public class MicronautHibernateValidator extends DefaultValidator { - private final jakarta.validation.Validator validator; + private final Validator validator; /** * Default constructor. @@ -63,7 +60,7 @@ public class MicronautHibernateValidator extends DefaultValidator implements Val protected MicronautHibernateValidator(ValidatorFactory validatorFactory, @NonNull ValidatorConfiguration configuration) { super(configuration); - this.validator = validatorFactory.getValidator(); + validator = validatorFactory.getValidator(); } @NonNull @@ -143,16 +140,16 @@ public void validateBean(@NonNull BeanResolutionContext resolutionContext, @ } private void failOnError(@NonNull BeanResolutionContext resolutionContext, Set> errors, Class beanType) { - if (!errors.isEmpty()) { - StringBuilder builder = new StringBuilder(); - builder.append("Validation failed for bean definition ["); - builder.append(beanType.getName()); - builder.append("]\nList of constraint violations:[\n"); - for (ConstraintViolation violation : errors) { - builder.append("\t").append(violation.getPropertyPath()).append(" - ").append(violation.getMessage()).append("\n"); - } - builder.append("]"); - throw new BeanInstantiationException(resolutionContext, builder.toString()); + if (errors.isEmpty()) { + return; + } + var builder = new StringBuilder("Validation failed for bean definition [") + .append(beanType.getName()) + .append("]\nList of constraint violations:[\n"); + for (ConstraintViolation violation : errors) { + builder.append('\t').append(violation.getPropertyPath()).append(" - ").append(violation.getMessage()).append('\n'); } + builder.append(']'); + throw new BeanInstantiationException(resolutionContext, builder.toString()); } } diff --git a/hibernate-validator/src/main/java/io/micronaut/configuration/hibernate/validator/ValidatorFactoryProvider.java b/hibernate-validator/src/main/java/io/micronaut/configuration/hibernate/validator/ValidatorFactoryProvider.java index cc11d49c..177498a5 100644 --- a/hibernate-validator/src/main/java/io/micronaut/configuration/hibernate/validator/ValidatorFactoryProvider.java +++ b/hibernate-validator/src/main/java/io/micronaut/configuration/hibernate/validator/ValidatorFactoryProvider.java @@ -45,7 +45,7 @@ */ @Factory @Requires(classes = HibernateValidator.class) -@TypeHint(value = HibernateValidator.class) +@TypeHint(HibernateValidator.class) public class ValidatorFactoryProvider { @Inject @@ -71,7 +71,7 @@ public class ValidatorFactoryProvider { @Singleton @Requires(classes = HibernateValidator.class) ValidatorFactory validatorFactory(Optional environment) { - Configuration validatorConfiguration = Validation.byDefaultProvider() + Configuration validatorConfiguration = Validation.byDefaultProvider() .configure(); validatorConfiguration.messageInterpolator(messageInterpolator.orElseGet(ParameterMessageInterpolator::new)); diff --git a/hibernate-validator/src/test/groovy/io/micronaut/configuration/hibernate/validator/ValidatedBeanSpec.groovy b/hibernate-validator/src/test/groovy/io/micronaut/configuration/hibernate/validator/ValidatedBeanSpec.groovy index 2ac16c1b..c6cb9f17 100644 --- a/hibernate-validator/src/test/groovy/io/micronaut/configuration/hibernate/validator/ValidatedBeanSpec.groovy +++ b/hibernate-validator/src/test/groovy/io/micronaut/configuration/hibernate/validator/ValidatedBeanSpec.groovy @@ -60,7 +60,7 @@ List of constraint violations:[ void "test validated bean invalid bean custom validator"() { given: - System.setProperty("a.url", "http://www.google.com") + System.setProperty("a.url", "https://www.google.com") System.setProperty("a.number", "3") System.setProperty("a.nobean", "abc") ApplicationContext applicationContext = new DefaultApplicationContext("test ") @@ -85,7 +85,7 @@ List of constraint violations:[ void "test validated bean invalid bean custom validator that isnt a bean"() { given: - System.setProperty("a.url", "http://www.google.com") + System.setProperty("a.url", "https://www.google.com") System.setProperty("a.number", "10") System.setProperty("a.nobean", "") ApplicationContext applicationContext = new DefaultApplicationContext("test ")