Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Missing Hints for Hibernate Constraint Validators #424

Closed
hakandilek opened this issue Dec 20, 2020 · 1 comment
Closed

Missing Hints for Hibernate Constraint Validators #424

hakandilek opened this issue Dec 20, 2020 · 1 comment
Assignees
Labels
type: compatibility Native image compatibility issue
Milestone

Comments

@hakandilek
Copy link

Hello everyone,

If I add the hibernate-validators dependency on a reactive webflux project and use the standard validation annotations like javax.validation.Valid on @RestController endpoints for validating beans with constrains like javax.validation.constraints.NotNull I see the following error on requests on those endpoints:

Failed to instantiate [org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator]: No default constructor found;

Worst part is, it returns HTTP-200 instead of a 5xx response due to the reactive nature (I suppose) of the application.

Here is the full DEBUG log of the request

DEBUG 1 --- [ctor-http-nio-4] o.s.w.s.adapter.HttpWebHandlerAdapter    : [8ed1577c-3] HTTP POST "/products"
DEBUG 1 --- [ctor-http-nio-4] s.w.r.r.m.a.RequestMappingHandlerMapping : [8ed1577c-3] Mapped to com.example.ProductController#create(Product)
DEBUG 1 --- [ctor-http-nio-4] .r.m.a.RequestBodyMethodArgumentResolver : [8ed1577c-3] Content-Type:application/json
DEBUG 1 --- [ctor-http-nio-4] .r.m.a.RequestBodyMethodArgumentResolver : [8ed1577c-3] 0..1 [com.example.Product]
DEBUG 1 --- [ctor-http-nio-4] o.s.http.codec.json.Jackson2JsonDecoder  : [8ed1577c-3] Decoded [Product{id=null, name=aaaa, code=1111}]
DEBUG 1 --- [ctor-http-nio-4] o.s.web.method.HandlerMethod             : [8ed1577c-3] Could not resolve parameter [0] in public reactor.core.publisher.Mono<com.example.Product> com.example.ProductController.create(com.example.Product): Error creating bean with name 'org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator.<init>()
DEBUG 1 --- [ctor-http-nio-4] o.s.w.s.adapter.HttpWebHandlerAdapter    : [8ed1577c-3] Completed 200 OK

If I provide the following reflect-config.json it works:

[
  {
    "name": "org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator",
    "allPublicConstructors": true,
    "allDeclaredMethods":true
  }
]

DEBUG log for the working endpoint after providing the reflect-config.json

DEBUG 1 --- [or-http-epoll-3] o.s.w.s.adapter.HttpWebHandlerAdapter    : [f6f13f67-1] HTTP POST "/products"
DEBUG 1 --- [or-http-epoll-3] s.w.r.r.m.a.RequestMappingHandlerMapping : [f6f13f67-1] Mapped to com.example.ProductController#create(Product)
DEBUG 1 --- [or-http-epoll-3] .r.m.a.RequestBodyMethodArgumentResolver : [f6f13f67-1] Content-Type:application/json
DEBUG 1 --- [or-http-epoll-3] .r.m.a.RequestBodyMethodArgumentResolver : [f6f13f67-1] 0..1 [com.example.Product]
DEBUG 1 --- [or-http-epoll-3] o.s.http.codec.json.Jackson2JsonDecoder  : [f6f13f67-1] Decoded [Product{id=null, name=aaaa, code=1111}]
DEBUG 1 --- [or-http-epoll-3] com.example.ProductController            : create(Product{id=null, name=aaaa, code=1111})
DEBUG 1 --- [or-http-epoll-3] o.s.w.r.r.m.a.ResponseBodyResultHandler  : [f6f13f67-1] Using 'application/json' given [*/*] and supported [application/json, application/*+json, application/x-ndjson, text/event-stream]
DEBUG 1 --- [or-http-epoll-3] o.s.w.r.r.m.a.ResponseBodyResultHandler  : [f6f13f67-1] 0..1 [com.example.Product]
DEBUG 1 --- [or-http-epoll-3] o.s.r2dbc.core.DefaultDatabaseClient     : Executing SQL statement [INSERT INTO T_PRODUCT (NAME, CODE) VALUES ($1, $2)]
DEBUG 1 --- [tor-tcp-epoll-1] io.r2dbc.postgresql.QUERY                : Executing query: INSERT INTO T_PRODUCT (NAME, CODE) VALUES ($1, $2) RETURNING *
DEBUG 1 --- [tor-tcp-epoll-1] o.s.http.codec.json.Jackson2JsonEncoder  : [f6f13f67-1] Encoding [Product{id=1, name=aaaa, code=1111}]
DEBUG 1 --- [or-http-epoll-3] o.s.w.s.adapter.HttpWebHandlerAdapter    : [f6f13f67-1] Completed 201 CREATED

ValidationHints.java already provides some hints but I think it's not complete and further hints should be provided for all *Validator classes under org.hibernate.validator.internal.constraintvalidators and it's sub-packages.

Here is the full-example with the reflect-config.json:
https://github.com/hakandilek/spring-native-sample/tree/hibernate-constraintvalidators

@sdeleuze sdeleuze added this to the 0.9.0 milestone Jan 4, 2021
@sdeleuze
Copy link
Contributor

sdeleuze commented Jan 4, 2021

Thanks for raising it.

Complementary note, I also see a ClassNotFoundException: org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator error on this sample (modified to use @SpringBootApplication instead of @Configuration on the main class).

@sdeleuze sdeleuze self-assigned this Jan 4, 2021
@sdeleuze sdeleuze added the type: compatibility Native image compatibility issue label Jan 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: compatibility Native image compatibility issue
Development

No branches or pull requests

2 participants