Hi,
After coding a Context using the AbstractContext I noticed
that on #foreach cycles null values were being replaced by
the previous non-null value.
After trying to find the problem at my (aham! flawless) code
and a bit of navigation trough Velocity code, my top suspect
was this bit at the AbstractContext class:
public Object put(String key, Object value)
{
/*
* don't even continue if key or value is null
*/
if (key == null)
{
return null;
}
else if (value == null)
{
return null;
}
return internalPut(key, value);
}
See? When the value is null it does NOTHING, which means
that the previous non null value remains for that key!!!
(Argh, not even a RuntimeException!)
Since this might be used with some kind of Map also
allergic to null values, I propose to fix it by replacing
this (do-nothing-on-null-value) bit:
else if (value == null)
{
return null;
}
...with this (remove-key-on-null-value) one:
else if (value == null)
{
return internalRemove(key);
// was: return null;
}
Leaving that "was" comment to give a clue to some potential
victim of the change.
IMHO, the current behavior is not acceptable since it does
produce output that does not match its original data and
this is the most reliable fix I can think of.
Better ideas someone?
(This kind of core class scares me a bit.)
The patch is attached.
Have fun,
Paulo Gaspar
Index: src/java/org/apache/velocity/context/AbstractContext.java
===================================================================
RCS file:
/home/cvspublic/jakarta-velocity/src/java/org/apache/velocity/context/AbstractContext.java,v
retrieving revision 1.8
diff -u -r1.8 AbstractContext.java
--- src/java/org/apache/velocity/context/AbstractContext.java 22 Oct 2001 03:53:23
-0000 1.8
+++ src/java/org/apache/velocity/context/AbstractContext.java 17 Feb 2002 03:53:57
+-0000
@@ -194,7 +194,7 @@
public Object put(String key, Object value)
{
/*
- * don't even continue if key or value is null
+ * don't even continue if key is null
*/
if (key == null)
@@ -203,7 +203,8 @@
}
else if (value == null)
{
- return null;
+ return internalRemove(key);
+ // was: return null;
}
return internalPut(key, value);
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>