Muskaan12 created VELOCITY-988:
----------------------------------
Summary: Regression Bug in Velocity Library: JSON if Condition
with {} Failing in v2.3 and Later
Key: VELOCITY-988
URL: https://issues.apache.org/jira/browse/VELOCITY-988
Project: Velocity
Issue Type: Bug
Components: Engine
Affects Versions: 2.4.1, 2.4, 2.3
Reporter: Muskaan12
I have identified a regression bug in the Velocity library that affects the
handling of {{if}} conditions within JSON documents when curly braces
({{{{}}{}{{}}}}) are present. This issue is identified in version 2.3 and
present in later versions as well, but was working as expected in version 1.7.
To help reproduce the issue, I have provided a sample code along with the stack
trace in the following
{code:java}
public static void main(String[] args) {
// Initialize the VelocityEngine
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.init();
// Define the template with a basic and simple if condition
String template = "{\n" +
" #if($!{$SubjectData.StudyEventData['@StudyEventRepeatKey']})\n" +
" \"eventRepeatKey\":
\"$SubjectData.StudyEventData['@StudyEventRepeatKey']\",\n" +
" #end\n" +
" \"otherField\": \"someValue\"\n" +
"}";
// Create VelocityContext and add data
VelocityContext context = new VelocityContext();
context.put("SubjectData", Map.of("StudyEventData",
Map.of("@StudyEventRepeatKey", "123")));
// Render the template
StringWriter writer = new StringWriter();
velocityEngine.evaluate(context, writer, "Template", template);
// Print the rendered JSON
System.out.println(writer.toString());
}{code}
*Stacktrace:*
*=============*
*Exception in thread "main" org.apache.velocity.excep*
*Exception in thread "main" org.apache.velocity.exception.ParseErrorException:
Encountered ")\n" at Template[line 2, column 64]*
*Was expecting one of:*
*"||" ...*
*")" ...*
*<WHITESPACE> ...*
*<NEWLINE> ...*
*"-" ...*
*"+" ...*
{*}"{*}" ...*
*"/" ...*
*"%" ...*
*<LOGICAL_AND> ...*
*<LOGICAL_OR> ...*
*<LOGICAL_LT> ...*
*<LOGICAL_LE> ...*
*<LOGICAL_GT> ...*
*<LOGICAL_GE> ...*
*<LOGICAL_EQUALS> ...*
*<LOGICAL_NOT_EQUALS> ...*
**
*at
org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1440)*
*at
org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1398)*
*at org.apache.velocity.app.VelocityEngine.evaluate(VelocityEngine.java:213)*
*at org.example.VelocityExample.main(VelocityExample.java:28)*
*tion.ParseErrorException: Encountered ")\n" at Template[line 2, column 64]*
*Was expecting one of:*
*"||" ...*
*")" ...*
*<WHITESPACE> ...*
*<NEWLINE> ...*
*"-" ...*
*"+" ...*
{*}"{*}" ...*
*"/" ...*
*"%" ...*
*<LOGICAL_AND> ...*
*<LOGICAL_OR> ...*
*<LOGICAL_LT> ...*
*<LOGICAL_LE> ...*
*<LOGICAL_GT> ...*
*<LOGICAL_GE> ...*
*<LOGICAL_EQUALS> ...*
*<LOGICAL_NOT_EQUALS> ...*
**
*at
org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1440)*
*at
org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1398)*
*at org.apache.velocity.app.VelocityEngine.evaluate(VelocityEngine.java:213)*
*at org.example.VelocityExample.main(VelocityExample.java:28)*
--
This message was sent by Atlassian Jira
(v8.20.10#820010)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]