[ 
https://issues.apache.org/jira/browse/VELOCITY-911?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16808426#comment-16808426
 ] 

M. Luppi commented on VELOCITY-911:
-----------------------------------

Thanks for your reply, [~claude]. I was aware of the 
_directive.if.emptycheck_-property that gives me the behavior of version 1.7. 
However, we would like to use this new feature in our project.

My suggestion is to extend and correct the documentation to reflect the actual 
behavior more accurately. This way the users get a clear reference and knows 
exactly how the engine evaluates objects in a boolean context. In fact, using 
such logic on custom objects is a (yet undocumented) feature and worth 
addressing. Maybe a remark concerning visibility (method needs to be public) 
would be appropriate as well.

Maybe something along the lines of in the user guide 
([http://velocity.apache.org/engine/2.1/user-guide.html#if-elseif-else]):
{panel}
 * ...
 * $foo is an object with a getAsBoolean() method which evaluates to a true 
value
 * $foo is an object with an isEmpty() method which evaluates to a true value
 * $foo is an object without a getAsBoolean() or isEmpty() method which is not 
null{panel}
I do not know about the precedence of getAsBoolean() and isEmpty() and whether 
getAsBoolean()==false would preemptively stop evaluation. So the above might 
not be correct and could need adjustment.

In the configuration page 
([https://velocity.apache.org/engine/2.1/configuration.html#if-directive]) this 
could look like this:
{panel}
 * ... 
 * return whether isEmpty() is false (covers String, all Collection classes and 
other objects that have a public isEmpty() method).
 * ...{panel}
Of course the cases with length() and size() should also be adopted if 
necessary. The getAsBoolean() is already well-documented there.


[~sdumitriu]: I do not understand why this issue was closed as "not a problem" 
at this point, unless I need to raise a new issue to address the documentation 
itself.

> Incorrect evaluation of IF-Directive for objects with isEmpty-method
> --------------------------------------------------------------------
>
>                 Key: VELOCITY-911
>                 URL: https://issues.apache.org/jira/browse/VELOCITY-911
>             Project: Velocity
>          Issue Type: Bug
>    Affects Versions: 2.0, 2.1
>            Reporter: M. Luppi
>            Assignee: Claude Brisson
>            Priority: Major
>         Attachments: VelocityIsEmptyBugMCVE.java, mcve-project.zip, 
> template.vm
>
>
> With Velocity 2.0 the logic of evaluation for IF-Directives changed and now 
> uses an extended approach to determine whether it is true. This bug report 
> only refers to the case where $foo is an +object+. The documentation for 2.0 
> and 2.1 state as follows:
> {quote}The variable $foo is evaluated to determine whether it is true, which 
> will happen under one of those circumstances: [...]
>  $foo is an object (other than a string, a number or a collection) which is 
> not null
> _Source: 
> [http://velocity.apache.org/engine/2.0/user-guide.html#if-elseif-else|http://velocity.apache.org/engine/2.0/user-guide.html#if-elseif-else]_
> {quote}
> After a long debugging session I have found that objects with a public 
> isEmpty-method are not being handled according to the documentation cited 
> above. Thereby, such objects are evaluated as false if there is an 
> isEmpty-method that returns false. The correct evaluation result according to 
> the documentation should be true since the object is not null and not a 
> string, a number or a collection.
> It gets even more confusing when we take access modifiers into account. If 
> the access of the isEmpty-method is not public, the behaviour is as it should 
> be. When we make it public, however, the above explained bug occurs in 
> version 2.0 as well as 2.1.
> See the attached classes and the template or the whole project of the MCVE 
> (minimal, complete, and verifiable example) as ZIP.
> _The output of the test is as follows:_
> {code:java}
> null object
> -----------
> [ OK ] Expected.
> non-null object with isEmpty-method (public)
> --------------------------------------------
> isEmpty evaluates to true
> [FAIL] Not expected since object is not null.
> isEmpty evaluates to false
> [ OK ] Expected.
> non-null object with isEmpty-method (package-private)
> -----------------------------------------------------
> isEmpty evaluates to true
> [ OK ] Expected.
> isEmpty evaluates to false
> [ OK ] Expected.
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@velocity.apache.org
For additional commands, e-mail: dev-h...@velocity.apache.org

Reply via email to