[jboss-jira] [JBoss JIRA] (WFLY-11956) @PostConstruct on @ApplicationScoped bean called too late in case @Valid is annotated on a business method

Ronald Sigal (Jira) issues at jboss.org
Mon Apr 22 21:49:00 EDT 2019


    [ https://issues.jboss.org/browse/WFLY-11956?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13725137#comment-13725137 ] 

Ronald Sigal commented on WFLY-11956:
-------------------------------------

There seem to be two issues.

1. As Christian has verified, it seems that getHelloGreeting() is getting called because A) it is a getter, and B) @Valid says that the returned GreetingModel should be validated, which means that getHelloGreeting() must be called.

[By the way, getHelloGreeting() is called twice by way of isCascadeRequired() and getCascadableValue() in the method validateCascadedConstraints() in org.hibernate.validator.internal.engine.ValidatorImpl:
{code}
if ( isCascadeRequired( validationContext, valueContext.getCurrentBean(), valueContext.getPropertyPath(), elementType ) ) {
	Object value = getCascadableValue( validationContext, valueContext.getCurrentBean(), cascadable );
{code}
]

It seems to me that there should be a global way to prevent getters from being called. In fact, the default behavior is that getters are *not* called. But, beyond that, adding a META-INF/validation.xml with something like
{code}
    <executable-validation>
        <default-validated-executable-types>
            <executable-type>CONSTRUCTORS</executable-type>
            <executable-type>NON_GETTER_METHODS</executable-type>
        </default-validated-executable-types>
    </executable-validation>
{code}
should, I think, solve the problem. When I try that, though, it doesn't seem to help. [~guillaume.smet], can you comment on that?

2. The other issue is the fact that validation is instigated by JaxrsInjectionTarget.inject(). Probably I should check for the presence of a @PostConstruct annotated method, and, in its presence, start validation from JaxrsInjectionTarget.postConstruct(). So that's a fixable bug.

> @PostConstruct on @ApplicationScoped bean called too late in case @Valid is annotated on a business method
> ----------------------------------------------------------------------------------------------------------
>
>                 Key: WFLY-11956
>                 URL: https://issues.jboss.org/browse/WFLY-11956
>             Project: WildFly
>          Issue Type: Bug
>          Components: Bean Validation, REST
>    Affects Versions: 16.0.0.Final
>            Reporter: Joerg Baesner
>            Assignee: Ronald Sigal
>            Priority: Major
>         Attachments: logging.txt, playground.zip
>
>
> Having a bean class with {{@ApplicationScoped}}, which has a {{@PostConstruct}} and is implementing  the following _Interface_:
> {code}
> @Path("/validated")
> public interface ValidatedJaxRsInterface {
>     
>     @GET
>     @Valid
>     @Produces(MediaType.APPLICATION_JSON)
>     GreetingModel getHelloGreeting();
> }
> {code}
> will result in calling the {{getHelloGreeting}} method of the implementation class twice *_before_* the {{@PostConstruct}} is getting executed.
> This can be reproduced with the attached reproducer application...



--
This message was sent by Atlassian Jira
(v7.12.1#712002)


More information about the jboss-jira mailing list