You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2019/12/05 16:48:34 UTC

svn commit: r1870882 - in /velocity/engine/trunk/velocity-engine-core/src: main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java test/java/org/apache/velocity/test/issues/Velocity904TestCase.java

Author: cbrisson
Date: Thu Dec  5 16:48:34 2019
New Revision: 1870882

URL: http://svn.apache.org/viewvc?rev=1870882&view=rev
Log:
[core] Fix VELOCITY-904: preserve argument literal *even* if the argument passed to the macro is not null (for .B.C)

Added:
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity904TestCase.java   (with props)
Modified:
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java?rev=1870882&r1=1870881&r2=1870882&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java Thu Dec  5 16:48:34 2019
@@ -384,7 +384,10 @@ public class VelocimacroProxy extends Di
             context.put(macroArg.name, newVal);
             values[(i-1) * 2 + 1] = newVal;
 
-            if (preserveArgumentsLiterals && newVal == null && argNode != null)
+            /* when preserveArgumentsLiterals is true, we still store the actual reference passed to the macro
+               even if the value is not null, because *if* the argument is set to null *during* the macro rendering
+               we still expect the passed argument literal to be displayed to be fully backward compatible. */
+            if (preserveArgumentsLiterals && /* newVal == null && */ argNode != null)
             {
                 context.put(literalArgArray[i], argNode);
             }

Added: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity904TestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity904TestCase.java?rev=1870882&view=auto
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity904TestCase.java (added)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity904TestCase.java Thu Dec  5 16:48:34 2019
@@ -0,0 +1,61 @@
+package org.apache.velocity.test.issues;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.test.BaseTestCase;
+
+/**
+ * This class tests VELOCITY-904.
+ */
+public class Velocity904TestCase extends BaseTestCase
+{
+    public Velocity904TestCase(String name)
+    {
+       super(name);
+    }
+
+    @Override
+    protected void setUpEngine(VelocityEngine engine)
+    {
+        engine.setProperty("velocimacro.arguments.preserve_literals", getName().contains("NoPreserve") ? "false" : "true");
+    }
+
+    public void testNullArgNoPreserve()
+    {
+        assertEvalEquals("$parameter", "#macro(testmacro $parameter)$parameter#end#testmacro($return)");
+    }
+
+    public void testNullArgPreserve()
+    {
+        assertEvalEquals("$return", "#macro(testmacro $parameter)$parameter#end#testmacro($return)");
+    }
+
+    public void testArgSetToNullNoPreserve()
+    {
+        assertEvalEquals("$input", "#macro(mymacro $input)#set($input = $null)$input#end#set($variable = 'value')#mymacro($variable)");
+    }
+
+    public void testArgSetToNullPreserve()
+    {
+        assertEvalEquals("$variable", "#macro(mymacro $input)#set($input = $null)$input#end#set($variable = 'value')#mymacro($variable)");
+    }
+
+}

Propchange: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity904TestCase.java
------------------------------------------------------------------------------
    svn:executable = *