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

Code cleanup. Update libraries. #373

Merged
merged 3 commits into from
Apr 1, 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
5 changes: 1 addition & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ micronaut-docs = "2.0.0"
managed-hibernate-validator = '8.0.1.Final'

micronaut-serde = "2.8.2"
micronaut-test = "4.0.0"
micronaut-test = "4.2.1"
micronaut-validation = "4.4.4"

groovy = "4.0.13"


[libraries]
# Core
micronaut-core = { module = 'io.micronaut:micronaut-core-bom', version.ref = 'micronaut' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -45,7 +44,7 @@
@Singleton
public class DefaultParameterNameProvider implements ParameterNameProvider {

private static final Set<String> INTERNAL_CLASS_NAMES = CollectionUtils.setOf(Object.class.getName(), "groovy.lang.GroovyObject");
private static final Set<String> INTERNAL_CLASS_NAMES = Set.of(Object.class.getName(), "groovy.lang.GroovyObject");
private final BeanContext beanContext;

/**
Expand All @@ -66,8 +65,8 @@ public List<String> 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;
})
Expand Down Expand Up @@ -98,7 +97,7 @@ public List<String> getParameterNames(Method method) {
* @return list of strings
*/
protected List<String> defaultParameterTypes(Class<?>[] parameterTypes) {
List<String> names = new ArrayList<>(parameterTypes.length);
var names = new ArrayList<String>(parameterTypes.length);
for (int i = 0; i < parameterTypes.length; i++) {
names.add("arg" + i);
}
Expand All @@ -107,7 +106,7 @@ protected List<String> defaultParameterTypes(Class<?>[] parameterTypes) {

private List<String> doGetParameterNames(Executable executable) {
Parameter[] parameters = executable.getParameters();
List<String> parameterNames = new ArrayList<>(parameters.length);
var parameterNames = new ArrayList<String>(parameters.length);

for (Parameter parameter : parameters) {
parameterNames.add(parameter.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -143,16 +140,16 @@ public <T> void validateBean(@NonNull BeanResolutionContext resolutionContext, @
}

private <T> void failOnError(@NonNull BeanResolutionContext resolutionContext, Set<ConstraintViolation<T>> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
*/
@Factory
@Requires(classes = HibernateValidator.class)
@TypeHint(value = HibernateValidator.class)
@TypeHint(HibernateValidator.class)
public class ValidatorFactoryProvider {

@Inject
Expand All @@ -71,7 +71,7 @@ public class ValidatorFactoryProvider {
@Singleton
@Requires(classes = HibernateValidator.class)
ValidatorFactory validatorFactory(Optional<Environment> environment) {
Configuration validatorConfiguration = Validation.byDefaultProvider()
Configuration<?> validatorConfiguration = Validation.byDefaultProvider()
.configure();

validatorConfiguration.messageInterpolator(messageInterpolator.orElseGet(ParameterMessageInterpolator::new));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ")
Expand All @@ -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 ")
Expand Down
Loading