micronaut-projects / micronaut-hibernate-validator

Integration with Hibernate Validator for Micronaut
Apache License 2.0
3 stars 7 forks source link

Custom value extractor not found #449

Open loicmathieu opened 2 weeks ago

loicmathieu commented 2 weeks ago

Expected Behavior

We should be able to create a custom value extractor and use it.

Actual Behaviour

Give the following value extractor

@Singleton
public class PropertyValueExtractor implements ValueExtractor<Property<@ExtractedValue ?>> {

    @Override
    public void extractValues(Property<?> originalValue, ValueReceiver receiver) {
        receiver.value( null, originalValue.getExpression());
    }
}

We should be able to use it to add validation annotations to the Property type parameter like that:

    @NotNull
    private Property<@Min(10) Integer> number;

But when we do that, it fails with the following error:

jakarta.validation.ConstraintDeclarationException: HV000197: No value extractor found for type parameter 'T' of type com.example.Property.
    at org.hibernate.validator.internal.metadata.core.MetaConstraints.addValueExtractorDescriptorForTypeArgumentLocation(MetaConstraints.java:147)
    at org.hibernate.validator.internal.metadata.core.MetaConstraints.create(MetaConstraints.java:63)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findTypeUseConstraints(AnnotationMetaDataProvider.java:806)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findTypeArgumentsConstraints(AnnotationMetaDataProvider.java:776)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findTypeAnnotationConstraints(AnnotationMetaDataProvider.java:609)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findPropertyMetaData(AnnotationMetaDataProvider.java:241)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getFieldMetaData(AnnotationMetaDataProvider.java:229)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.retrieveBeanConfiguration(AnnotationMetaDataProvider.java:130)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfiguration(AnnotationMetaDataProvider.java:121)
    at org.hibernate.validator.internal.metadata.BeanMetaDataManagerImpl.getBeanConfigurationForHierarchy(BeanMetaDataManagerImpl.java:234)
    at org.hibernate.validator.internal.metadata.BeanMetaDataManagerImpl.createBeanMetaData(BeanMetaDataManagerImpl.java:201)
    at org.hibernate.validator.internal.metadata.BeanMetaDataManagerImpl.getBeanMetaData(BeanMetaDataManagerImpl.java:165)
    at org.hibernate.validator.internal.engine.ValidatorImpl.buildNewLocalExecutionContext(ValidatorImpl.java:772)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedAnnotatedObjectForCurrentGroup(ValidatorImpl.java:627)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedConstraints(ValidatorImpl.java:590)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateParametersInContext(ValidatorImpl.java:880)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateParameters(ValidatorImpl.java:283)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateParameters(ValidatorImpl.java:235)
    at io.micronaut.validation.ValidatingInterceptor.intercept(ValidatingInterceptor.java:99)
    at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:143)
    at com.example.$Example$Definition$Intercepted.post(Unknown Source)
    at com.example.$Example$Definition$Exec.dispatch(Unknown Source)

See the following reproducer, you can reproduce the error with the following curl command:

curl -XPOST --data '{"number": 42, "string": "Hello"}' -H "Content-Type: application/json" localhost:808

value-extractor.zip

You must use Micronaut validation 4.8.0 due to https://github.com/micronaut-projects/micronaut-validation/issues/432

Steps To Reproduce

No response

Environment Information

Ubuntu 24.10 Java 21

Example Application

No response

Version

4.6.3

graemerocher commented 2 weeks ago

if you would like to contribute a PR to resolve this would be appreciated. In general Hibernate Validator support is in maintenance mode and we recommend users switch to Micronaut's implementation

loicmathieu commented 2 weeks ago

I have no idea where to start, and if it would take long or not.

We may try to update to Micronaut's implementation but as I understand it, it's based on an annotation processor and as our plugins can contribute validators and we don't want too much dependencies between our plugins and Micronaut I'm not sure it will work.