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

Jakarta Bean Validators not working in native mode for CloudEvents api #7686

Closed
restalion opened this issue Mar 9, 2020 · 2 comments
Closed
Labels
kind/question Further information is requested

Comments

@restalion
Copy link

restalion commented Mar 9, 2020

Hi All,
I'm working in a quarkus project using sdk-java api to create and receive events into a kubernetes cluster. If we execute the project using the JVM it is working fine, but when I launch it as a native image I see the error below:

javax.validation.NoProviderFoundException: Unable to create a Configuration, because no Jakarta Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.

The problem seems to be in the validations included in cloudevents/sdk-java pom.xml:

        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernate-validator.version}</version>
        </dependency>

Expected behavior
Validations should work fine in JVM and Native.

Actual behavior
When executing in native mode we see an exception:
javax.validation.NoProviderFoundException: Unable to create a Configuration, because no Jakarta Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.

To Reproduce
Steps to reproduce the behavior:

  1. Clone the repo at https://github.com/restalion/quarkus-cloudevent-sample
  2. Go to cloned folder and run project in dev mode ./mvnw quarkus:dev
  3. Send a POST to localhost:8080/event-sample with header Content-Type:application/json and body {"code":1,"value":"This is a test"}
  4. You should see a trace with the creation of the cloud event and all the headers related:
    image
  5. Now we will try to do the same but using the native image, so, compile the project in native: ./mvnw package -Pnative
  6. Run the native image: target/quarkus-cloudevent-sample-1.0-SNAPSHOT-runner
  7. Send a POST to localhost:8080/event-sample with header Content-Type:application/json and body {"code":1,"value":"This is a test"}
  8. Now you will see an error as response:
    javax.validation.NoProviderFoundException: Unable to create a Configuration, because no Jakarta Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
    And, in logs, you can check that the cloud event was not created properly:
    image

Configuration
All the configuration is available in github project

Screenshots
Screenshots previously shared.

Environment (please complete the following information):

  • Output of uname -a or ver:

Darwin MacBook-Pro-de-Julio.local 19.3.0 Darwin Kernel Version 19.3.0: Thu Jan 9 20:58:23 PST 2020; root:xnu-6153.81.5~1/RELEASE_X86_64 x86_64

  • Output of java -version:
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b06)
OpenJDK 64-Bit GraalVM CE 19.3.1 (build 25.242-b06-jvmci-19.3-b07, mixed mode)
  • GraalVM version (if different from Java):
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b06)
OpenJDK 64-Bit GraalVM CE 19.3.1 (build 25.242-b06-jvmci-19.3-b07, mixed mode)
  • Quarkus version or git rev:
    <quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
    <quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
    <quarkus.platform.version>1.2.1.Final</quarkus.platform.version>
  • Build tool (ie. output of mvnw --version or gradlew --version):
Maven home: /.m2/wrapper/dists/apache-maven-3.6.2-bin/795eh28tki48bv3l67maojf0ra/apache-maven-3.6.2
Java version: 1.8.0_242, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/graalvm-ce-java8-19.3.1/Contents/Home/jre
Default locale: es_ES, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.3", arch: "x86_64", family: "mac"

Additional context
No additional context needed.

@restalion restalion added the kind/bug Something isn't working label Mar 9, 2020
@gsmet
Copy link
Member

gsmet commented Mar 9, 2020

CloudEvent is not able to build the ValidatorFactory as it requires a specific bootstrap for native images.

Quarkus initializes a ValidatorFactory itself and you should use it everywhere.

What you can do is to inject the Validator via CDI where you initialize the CloudEvent and use this method https://github.com/cloudevents/sdk-java/blob/master/api/src/main/java/io/cloudevents/v03/CloudEventBuilder.java#L253 to inject it into the builder.

This method has been specifically added for Quarkus.

@gsmet gsmet added kind/question Further information is requested and removed kind/bug Something isn't working labels Mar 9, 2020
@restalion
Copy link
Author

Thanks a lot Guillaume! It's working fine now. The code is updated in github but I add here the changes for future references:

I have injected the Validator in EventService class:
@Inject Validator validator;

And add a withValidator(validator) to the CloudEvent builder:

        CloudEventImpl<SampleEvent> ce =
            CloudEventBuilder.<SampleEvent>builder()
                .withType(type)
                .withId(id)
                .withTime(ZonedDateTime.now())
                .withDatacontenttype(ContentType.APPLICATION_JSON.toString())
                .withData(payload)
                .withExtension(tracing)
                .withSubject("Sample update")
                .withSource(URI.create("/sample-source"))
                .withValidator(validator)
                .build();

restalion added a commit to restalion/quarkus-cloudevent-sample that referenced this issue Mar 9, 2020
cdhermann added a commit to cdhermann/quarkusio.github.io that referenced this issue Jan 12, 2023
I'd like to propose an additional section `Native compilation` describing how to use the `Validator` to avoid running into quarkusio/quarkus#7686
cdhermann added a commit to cdhermann/quarkus that referenced this issue Jan 12, 2023
I'd like to propose an additional section `Native compilation` describing how to use the `Validator` to avoid running into quarkusio#7686
gsmet pushed a commit to cdhermann/quarkus that referenced this issue Jan 27, 2023
I'd like to propose an additional section `Native compilation` describing how to use the `Validator` to avoid running into quarkusio#7686
gsmet pushed a commit to gsmet/quarkus that referenced this issue Jan 28, 2023
I'd like to propose an additional section `Native compilation` describing how to use the `Validator` to avoid running into quarkusio#7686

(cherry picked from commit 1be06ed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants