Uploaded image for project: 'Velocity'
  1. Velocity
  2. VELOCITY-872

Parse error from arithmetic expression as an argument to a method

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.0
    • None
    • None
    • VTL in user macro in Atlassian Confluence 5.9.4

    Description

      The following results in a ParserErrorException:

      ## @noparams
      
      #set ($list = ["a", "b", "c"])
      #set ($last = $list.get($list.size() - 1))
      <p>$last</p>
      

      The message that appears in atlassian-confluence.log:

      org.apache.velocity.exception.ParseErrorException: Encountered "-" at getRenderedContent[line 4, column 38]
      Was expecting one of:
          "," ...
          ")" ...
      
              at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1202)
              at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1164)
              at org.apache.velocity.app.VelocityEngine.evaluate(VelocityEngine.java:219)
              at com.atlassian.confluence.util.velocity.VelocityUtils.writeRenderedContent(VelocityUtils.java:148)
              at com.atlassian.confluence.util.velocity.VelocityUtils.getRenderedContent(VelocityUtils.java:130)
              at com.atlassian.confluence.util.velocity.VelocityUtils.getRenderedContent(VelocityUtils.java:117)
              at com.atlassian.confluence.util.velocity.VelocityUtils.getRenderedContent(VelocityUtils.java:113)
              at com.atlassian.confluence.macro.GenericVelocityMacro.execute(GenericVelocityMacro.java:101)
              at com.atlassian.confluence.content.render.xhtml.view.macro.ViewMacroMarshaller.executeMacro(ViewMacroMarshaller.java:229)
              at com.atlassian.confluence.content.render.xhtml.view.macro.ViewMacroMarshaller.marshalInternal(ViewMacroMarshaller.java:156)
              at com.atlassian.confluence.content.render.xhtml.view.macro.ViewMacroMarshaller.lambda$marshal$445(ViewMacroMarshaller.java:113)
              at com.atlassian.confluence.impl.content.render.xhtml.analytics.MetricsCollectingMarshaller.marshal(MetricsCollectingMarshaller.java:49)
      ~~~snipped~~~
      

      The Velocity parser does not appear to support arithmetic expressions as arguments in method calls. I find this extremely surprising, given the ubiquity of this basic syntax among languages. I hope that this is just a result of Confluence using an older version of Velocity. Regardless, it's annoying that absolutely nowhere in the documentation does it mention anything concerning this limitation.

      This is a workaround:

      ## This works as expected:
      #set ($lastIndex = $list.size() - 1)
      #set ($last = $list.get($lastIndex))
      <p>$last</p>
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            alex35 Alexander Rosenberg
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: