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

Hibernate-Validator: Custom ValueExtractor is not automatically registered #20347

Closed
mschorsch opened this issue Sep 23, 2021 · 14 comments · Fixed by #20382
Closed

Hibernate-Validator: Custom ValueExtractor is not automatically registered #20347

mschorsch opened this issue Sep 23, 2021 · 14 comments · Fixed by #20382
Assignees
Labels
area/hibernate-validator Hibernate Validator kind/bug Something isn't working
Milestone

Comments

@mschorsch
Copy link
Contributor

mschorsch commented Sep 23, 2021

Describe the bug

A custom ValueExtractor is not automatically registered by quarkus (https://quarkus.io/guides/validation#hibernate-validator-extension-and-cdi).

2021-09-23 08:13:01,190 INFO  [io.qua.dep.dev.IsolatedDevModeMain] (main) Attempting to start live reload endpoint to recover from previous Quarkus startup failure
2021-09-23 08:13:01,193 ERROR [io.qua.run.boo.StartupActionImpl] (Quarkus Main Thread) Error running Quarkus: java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.runner.bootstrap.StartupActionImpl$1.run(StartupActionImpl.java:98)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.ExceptionInInitializerError
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at java.base/java.lang.Class.newInstance(Class.java:584)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:65)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:42)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:119)
	at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)
	... 6 more
Caused by: java.lang.RuntimeException: Failed to start quarkus
	at io.quarkus.runner.ApplicationImpl.<clinit>(ApplicationImpl.zig:428)
	... 15 more
Caused by: javax.validation.ConstraintDeclarationException: HV000197: No value extractor found for type parameter 'T' of type org.acme.Container.
	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:805)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findTypeArgumentsConstraints(AnnotationMetaDataProvider.java:775)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findTypeArgumentsConstraints(AnnotationMetaDataProvider.java:778)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findTypeAnnotationConstraintsForExecutableParameter(AnnotationMetaDataProvider.java:729)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getParameterMetaData(AnnotationMetaDataProvider.java:431)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findExecutableMetaData(AnnotationMetaDataProvider.java:308)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getMetaData(AnnotationMetaDataProvider.java:292)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getMethodMetaData(AnnotationMetaDataProvider.java:279)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.retrieveBeanConfiguration(AnnotationMetaDataProvider.java:130)
	at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfiguration(AnnotationMetaDataProvider.java:120)
	at org.hibernate.validator.internal.metadata.PredefinedScopeBeanMetaDataManager.getBeanConfigurationForHierarchy(PredefinedScopeBeanMetaDataManager.java:183)
	at org.hibernate.validator.internal.metadata.PredefinedScopeBeanMetaDataManager.createBeanMetaData(PredefinedScopeBeanMetaDataManager.java:150)
	at org.hibernate.validator.internal.metadata.PredefinedScopeBeanMetaDataManager.<init>(PredefinedScopeBeanMetaDataManager.java:100)
	at org.hibernate.validator.internal.engine.PredefinedScopeValidatorFactoryImpl.<init>(PredefinedScopeValidatorFactoryImpl.java:180)
	at org.hibernate.validator.PredefinedScopeHibernateValidator.buildValidatorFactory(PredefinedScopeHibernateValidator.java:42)
	at org.hibernate.validator.internal.engine.AbstractConfigurationImpl.buildValidatorFactory(AbstractConfigurationImpl.java:433)
	at io.quarkus.hibernate.validator.runtime.HibernateValidatorRecorder$1.created(HibernateValidatorRecorder.java:139)
	at io.quarkus.arc.runtime.ArcRecorder.initBeanContainer(ArcRecorder.java:70)
	at io.quarkus.deployment.steps.ArcProcessor$generateResources-1025303321.deploy_0(ArcProcessor$generateResources-1025303321.zig:139)
	at io.quarkus.deployment.steps.ArcProcessor$generateResources-1025303321.deploy(ArcProcessor$generateResources-1025303321.zig:40)
	at io.quarkus.runner.ApplicationImpl.<clinit>(ApplicationImpl.zig:386)
	... 15 more

2021-09-23 08:13:01,264 INFO  [org.jbo.threads] (main) JBoss Threads version 3.4.2.Final

Note: Registration via Service-Loader-API works as expected (https://docs.jboss.org/hibernate/validator/6.2/reference/en-US/html_single/#section-valueextraction-registeringvalueextractor).

Expected behavior

No response

Actual behavior

No response

How to Reproduce?

  1. https://code.quarkus.io/?e=resteasy-reactive-jackson&e=hibernate-validator
  2. Add the following classes:
package org.acme;

public class Container<T> {
}
package org.acme;

import javax.enterprise.context.ApplicationScoped;
import javax.validation.valueextraction.ExtractedValue;
import javax.validation.valueextraction.ValueExtractor;

@ApplicationScoped
public class ContainerValueExtractor
        implements ValueExtractor<Container<@ExtractedValue ?>> {

    @Override
    public void extractValues(Container<?> originalValue, ValueReceiver receiver) {
        // whatever
    }
}
package org.acme;

import javax.enterprise.context.ApplicationScoped;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;

@ApplicationScoped
@Path("/container")
public class ContainerResource {

    @GET
    @Path("/1")
    @Produces(MediaType.TEXT_PLAIN)
    public String containerMethod(
            @Valid List<@NotNull @NotNull Container<@NotNull String>> list
    ) {
        return "hello";
    }
}
  1. ./mvnw quarkus:dev

Output of uname -a or ver

No response

Output of java -version

Java 11

GraalVM version (if different from Java)

No response

Quarkus version or git rev

Quarkus 2.2.3.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Maven 3.8.1

Additional information

No response

@mschorsch mschorsch added the kind/bug Something isn't working label Sep 23, 2021
@quarkus-bot
Copy link

quarkus-bot bot commented Sep 23, 2021

/cc @gsmet, @yrodiere

@quarkus-bot quarkus-bot bot added the area/hibernate-validator Hibernate Validator label Sep 23, 2021
@geoand
Copy link
Contributor

geoand commented Sep 23, 2021

I sort of remember something similar being reported in the past, but I'll let the HV experts weigh in on this one.

One thing I would ask however is to give 2.3.0.CR1 a shot and see if the issue still persists.

@mschorsch
Copy link
Contributor Author

I sort of remember something similar being reported in the past, but I'll let the HV experts weigh in on this one.

One thing I would ask however is to give 2.3.0.CR1 a shot and see if the issue still persists.

Doesnt't work in 2.3.0.CR1 either.

@geoand
Copy link
Contributor

geoand commented Sep 23, 2021

Thanks for checking

@gsmet
Copy link
Member

gsmet commented Sep 23, 2021

It should work given https://github.com/quarkusio/quarkus/blob/main/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/HibernateValidatorRecorder.java#L133-L137 .

By any chance, is your ValueExtractor bean in an external library that is not indexed by Jandex?

If not, can you create a small reproducer?

@mschorsch
Copy link
Contributor Author

It should work given https://github.com/quarkusio/quarkus/blob/main/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/HibernateValidatorRecorder.java#L133-L137 .

By any chance, is your ValueExtractor bean in an external library that is not indexed by Jandex?

In our application the ValueExtractor is in a seperate maven module. The module is indexed by Jandex.

If not, can you create a small reproducer?

You can find a reproducer in the issue description under How to Reproduce?.

@gsmet
Copy link
Member

gsmet commented Sep 23, 2021

Hmm, how do you get your module indexed? Do you have an empty META-INF/beans.xml? Because that's what is needed to get ArC to find the beans.

@mschorsch
Copy link
Contributor Author

mschorsch commented Sep 23, 2021

In the reproducer above there isn't an external module. So it should work out of the box.

In our real application the Jandex index is created via the jandex-maven-plugin (https://quarkus.io/guides/cdi-reference#how-to-generate-a-jandex-index).

Do we have to create an empty file META-INF/beans.xml?

@yrodiere
Copy link
Member

In the reproducer above there isn't an external module. So it should work out of the box.

Not really a reproducer if it doesn't reproduce the problem ;)

Do we have to create an empty file META-INF/beans.xml?

According to the Quarkus CDI documentation, having a Jandex index in your dependency at META-INF/jandex.idx should be enough.

But adding META-INF/beans.xml is worth a try; at least it will give us more information about what's wrong exactly. Please try?

Or provide a reproducer, then we can try things ourselves :)

@mschorsch
Copy link
Contributor Author

In the reproducer above there isn't an external module. So it should work out of the box.

Not really a reproducer if it doesn't reproduce the problem ;)

Ok, i checked it. I followed exactly my description to reproduce the problem and i can reproduce the issue without problems. Even a simple ./mvnw clean install leads to the following error:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 7.075 s <<< FAILURE! - in org.acme.ReactiveGreetingResourceTest
[ERROR] org.acme.ReactiveGreetingResourceTest.testHelloEndpoint  Time elapsed: 0.008 s  <<< ERROR!
java.lang.RuntimeException: java.lang.ExceptionInInitializerError
Caused by: java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Failed to start quarkus
Caused by: javax.validation.ConstraintDeclarationException: HV000197: No value extractor found for type parameter 'T' of type org.acme.Container.

[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors: 
[ERROR]   ReactiveGreetingResourceTest.testHelloEndpoint » Runtime java.lang.ExceptionIn...
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

Do we have to create an empty file META-INF/beans.xml?

According to the Quarkus CDI documentation, having a Jandex index in your dependency at META-INF/jandex.idx should be enough.

But adding META-INF/beans.xml is worth a try; at least it will give us more information about what's wrong exactly. Please try?

I tried out your suggestion. Adding a META-INF/beans.xml doesn't help.

Or provide a reproducer, then we can try things ourselves :)

See above.

@yrodiere
Copy link
Member

Seems like I misunderstood what you were saying about "working out of the box". Sorry about that.

Thanks for the reproducer, I will have a look.

@mschorsch
Copy link
Contributor Author

No problem, thank you 👍

@yrodiere yrodiere self-assigned this Sep 23, 2021
@mschorsch
Copy link
Contributor Author

@yrodiere I've created a simple reproducer (see attached).

reproducer20347.zip

@yrodiere
Copy link
Member

yrodiere commented Sep 24, 2021

Thank you. Yes I'm working on it. The bad news is, even after I solve this problem, there are others that still prevent us from using value extractors... I'm on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hibernate-validator Hibernate Validator kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants