This is an automated email from the ASF dual-hosted git repository.

kusal pushed a commit to branch WW-5516-attrmap-npe-67
in repository https://gitbox.apache.org/repos/asf/struts.git

commit c36eafa37af354a5bb596f72f68c009d8db862f2
Author: Kusal Kithul-Godage <g...@kusal.io>
AuthorDate: Tue Feb 4 00:51:51 2025 +1100

    WW-5516 Fix AttributeMap NPE when PageContext has no request
---
 .../apache/struts2/dispatcher/AttributeMap.java    |  2 +-
 .../struts2/dispatcher/AttributeMapTest.java       | 24 +++++++++++++++++++++-
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/struts2/dispatcher/AttributeMap.java 
b/core/src/main/java/org/apache/struts2/dispatcher/AttributeMap.java
index dbdc686bb..1b4f727cd 100644
--- a/core/src/main/java/org/apache/struts2/dispatcher/AttributeMap.java
+++ b/core/src/main/java/org/apache/struts2/dispatcher/AttributeMap.java
@@ -86,7 +86,7 @@ public class AttributeMap extends AbstractMap<String, Object> 
{
 
         PageContext pc = getPageContext();
 
-        if (pc == null) {
+        if (pc == null || pc.getRequest() == null) {
             RequestMap request = (RequestMap) 
context.get(DispatcherConstants.REQUEST);
             SessionMap session = (SessionMap) 
context.get(DispatcherConstants.SESSION);
             ApplicationMap application = (ApplicationMap) 
context.get(DispatcherConstants.APPLICATION);
diff --git 
a/core/src/test/java/org/apache/struts2/dispatcher/AttributeMapTest.java 
b/core/src/test/java/org/apache/struts2/dispatcher/AttributeMapTest.java
index 7878150e3..15fbb962d 100644
--- a/core/src/test/java/org/apache/struts2/dispatcher/AttributeMapTest.java
+++ b/core/src/test/java/org/apache/struts2/dispatcher/AttributeMapTest.java
@@ -40,6 +40,11 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 public class AttributeMapTest {
 
@@ -360,4 +365,21 @@ public class AttributeMapTest {
         assertEquals("value", value);
     }
 
-}
\ No newline at end of file
+    @Test
+    public void get_whenPageContextHasNoRequest() {
+        PageContext pageContext = mock(PageContext.class);
+        when(pageContext.getRequest()).thenReturn(null);
+
+        HttpServletRequest req = new MockHttpServletRequest();
+        req.setAttribute("attr", "reqValue");
+
+        AttributeMap attributeMap = new AttributeMap(new HashMap<String, 
Object>() {{
+            put(StrutsStatics.PAGE_CONTEXT, pageContext);
+            put(DispatcherConstants.REQUEST, new RequestMap(req));
+        }});
+
+        assertEquals("reqValue", attributeMap.get("attr"));
+        verify(pageContext, never()).findAttribute(anyString());
+    }
+
+}

Reply via email to