You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by "Claude Brisson (JIRA)" <ji...@apache.org> on 2019/04/01 17:11:00 UTC

[jira] [Comment Edited] (VELOCITY-911) Incorrect evaluation of IF-Directive for objects with isEmpty-method

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

Claude Brisson edited comment on VELOCITY-911 at 4/1/19 5:10 PM:
-----------------------------------------------------------------

It is a documentation problem.

When evaluated in a boolean context, objects are tested with:
 * getAsBoolean() method if it exists
 * isEmpty() method if it exists

and although this is not explicitly stated in the docs, this is the expected behavior: when an object provides a mean to tell the outside world that it is empty, it generally means that it won't render anything.

If you want `#if($obj)` to return true while `$obj.isEmpty()` returns true, you can set:
{code:java}
directive.if.emptychek = false
{code}
in your velocity.properties file so that the behavior is reverted to the 1.7 one, or resort to a more explicit test like:
{code:java}
#if( $obj != $null )
{code}




was (Author: claude):
It is a documentation problem.

When evaluated in a boolean context, objects are tested with:
 * getAsBoolean() method if it exists
 * isEmpty() method if it exists

and although this is not explicitly stated in the docs, this is the expected behavior: when an object provides a mean to tell the outside world that it is empty, it generally means that it won't render anything.

If you want `#if($obj)` to return true while `$obj.isEmpty()` returns true, you can set:
{code:java}
directive.if.emptychek = false
{code}
in your velocity.properties file so that the behavior , or resort to a more explicit test like:
{code:java}
#if( $obj != $null )
{code}



> 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
>            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-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org