Skip to content

Commit c0dd8dd

Browse files
authored
Merge pull request #1210 from apache/WW-5516-attrmap-npe-67
2 parents 07603b9 + c36eafa commit c0dd8dd

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

core/src/main/java/org/apache/struts2/dispatcher/AttributeMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public Object get(Object key) {
8686

8787
PageContext pc = getPageContext();
8888

89-
if (pc == null) {
89+
if (pc == null || pc.getRequest() == null) {
9090
RequestMap request = (RequestMap) context.get(DispatcherConstants.REQUEST);
9191
SessionMap session = (SessionMap) context.get(DispatcherConstants.SESSION);
9292
ApplicationMap application = (ApplicationMap) context.get(DispatcherConstants.APPLICATION);

core/src/test/java/org/apache/struts2/dispatcher/AttributeMapTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
import static org.junit.Assert.assertNull;
4141
import static org.junit.Assert.assertThrows;
4242
import static org.junit.Assert.assertTrue;
43+
import static org.mockito.ArgumentMatchers.anyString;
44+
import static org.mockito.Mockito.mock;
45+
import static org.mockito.Mockito.never;
46+
import static org.mockito.Mockito.verify;
47+
import static org.mockito.Mockito.when;
4348

4449
public class AttributeMapTest {
4550

@@ -360,4 +365,21 @@ public void shouldContainsKey() {
360365
assertEquals("value", value);
361366
}
362367

363-
}
368+
@Test
369+
public void get_whenPageContextHasNoRequest() {
370+
PageContext pageContext = mock(PageContext.class);
371+
when(pageContext.getRequest()).thenReturn(null);
372+
373+
HttpServletRequest req = new MockHttpServletRequest();
374+
req.setAttribute("attr", "reqValue");
375+
376+
AttributeMap attributeMap = new AttributeMap(new HashMap<String, Object>() {{
377+
put(StrutsStatics.PAGE_CONTEXT, pageContext);
378+
put(DispatcherConstants.REQUEST, new RequestMap(req));
379+
}});
380+
381+
assertEquals("reqValue", attributeMap.get("attr"));
382+
verify(pageContext, never()).findAttribute(anyString());
383+
}
384+
385+
}

0 commit comments

Comments
 (0)