Merge pull request #1008 from apache/feature/WW-5451-null-iterator

WW-5451 Fixes NPE when iterator starts with null
This commit is contained in:
Lukasz Lenart
2024-08-01 12:01:46 +02:00
committed by GitHub
3 changed files with 75 additions and 2 deletions
@@ -306,7 +306,10 @@ public class IteratorComponent extends ContextBean {
if ((iterator != null) && iterator.hasNext()) {
Object currentValue = iterator.next();
stack.push(currentValue);
threadAllowlist.allowClass(currentValue.getClass());
if (currentValue != null) {
threadAllowlist.allowClass(currentValue.getClass());
}
String var = getVar();
@@ -184,6 +184,42 @@ public class IteratorComponentTest extends StrutsInternalTestCase {
assertEquals("1, 2, , 4, ", out.getBuffer().toString());
}
public void testIteratorWithNullsOnly() {
// given
stack.push(new FooAction() {
private final List<String> items = Arrays.asList(null, null, null);
public List<String> getItems() {
return items;
}
});
StringWriter out = new StringWriter();
ic.setValue("items");
ic.setVar("val");
Property prop = new Property(stack);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(prop);
String body = ", ";
// when
assertTrue(ic.start(out));
for (int i = 0; i < 3; i++) {
prop.start(out);
prop.end(out, body);
ic.end(out, null);
}
// then
assertEquals(", , , ", out.getBuffer().toString());
}
public void testIteratorWithDifferentLocale() {
// given
ActionContext.getContext().withLocale(new Locale("fa_IR"));
@@ -20,7 +20,6 @@ package org.apache.struts2.views.jsp;
import com.mockobjects.servlet.MockBodyContent;
import com.mockobjects.servlet.MockJspWriter;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.collections.ListUtils;
import javax.servlet.jsp.JspException;
@@ -722,6 +721,41 @@ public class IteratorTagTest extends AbstractUITagTest {
validateCounter(new String[]{"a", "b", "c"});
}
public void testNullElements() throws JspException {
Foo foo = new Foo();
foo.setArray(new String[3]);
stack.push(foo);
tag.setValue("array");
tag.setVar("anId");
// one
int result = tag.doStartTag();
assertEquals(TagSupport.EVAL_BODY_INCLUDE, result);
assertNull(stack.peek());
assertNull(stack.getContext().get("anId"));
tag.doInitBody();
// two
result = tag.doAfterBody();
assertEquals(TagSupport.EVAL_BODY_AGAIN, result);
assertNull(stack.peek());
assertNull(stack.getContext().get("anId"));
// three
result = tag.doAfterBody();
assertEquals(TagSupport.EVAL_BODY_AGAIN, result);
assertNull(stack.peek());
assertNull(stack.getContext().get("anId"));
result = tag.doAfterBody();
assertEquals(TagSupport.SKIP_BODY, result);
result = tag.doEndTag();
assertEquals(TagSupport.EVAL_PAGE, result);
}
public void testCounterWithArray() throws JspException {
Foo foo = new Foo();
foo.setArray(new String[]{"a", "b", "c", "d"});