Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WW-5451 Fixes NPE when iterator starts with null #1008

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@ public boolean start(Writer writer) {
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,42 @@ public List<String> getItems() {
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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

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;
Expand Down Expand Up @@ -722,6 +721,41 @@ public void testCounterWithList() throws JspException {
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"});
Expand Down