[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
Fri Apr 26 17:48:00 EDT 2019


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

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

Ok, consider
{code}
int id = 3;

@Path("id")
@GET
@Max(7)
public int getId() {
   return id;
}
{code} 
If I understand correctly, the value returned by getId() will be validated while the object is being constructed, and, by default, will not be validated if getId() is called as a resource method. That makes some sense, since it would be redundant to validate it again. 

Now, consider
{code}
int count = 0;

@Path("count")
@GET
@Max(1)
public int getCount() {
   return count++;
}
{code} 
When the return value of getCount() is validated at object construction time, it will pass. But if it were validated again when getCount() is called as a resource method, it would fail.

The point I'm trying to make is just that things get weird when a resource method looks like a getter. Note that the paragraph in Section 7.4 referred to by Santiago tries to make the same point:
{code}
Note that if validation for getter methods is enabled and a resource method’s signature obeys the rules for
getters, the resource method may be (unintentionally) invoked during validation. Conversely, if validation
for getter methods is disabled and the matching resource method’s signature obeys the rules for getters,
the JAX-RS runtime will still validate the method (i.e., the validation preference will be ignored) before
invocation.
{code}
I think the point (don't write resource methods that look like getter methods) is valid, but the details aren't right. I would say something like
{code}
Properties, which are characterized by the presence of a related getter method, are validated at object construction time. In particular,
the method is executed and the return value is validated. *N.B.* It is possible, but not a good idea, to write a resource method that also satisfies the
conditions to be a getter method. In that case, it will be treated and validated as a getter at object creation time, but, by default, since getters are not constrained methods, the return value will not be validated when the method is called as a resource method.
{code}
Does that sound right to anyone?

> @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