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

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

commit 2d8433d300fbfd5943132e2035265193dae26f22
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    |  4 +-
 .../struts2/dispatcher/AttributeMapTest.java       | 47 ++++++++++++++++------
 2 files changed, 36 insertions(+), 15 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 035b10f5d..a3bb6ba6e 100644
--- a/core/src/main/java/org/apache/struts2/dispatcher/AttributeMap.java
+++ b/core/src/main/java/org/apache/struts2/dispatcher/AttributeMap.java
@@ -18,9 +18,9 @@
  */
 package org.apache.struts2.dispatcher;
 
-import jakarta.servlet.jsp.PageContext;
 import org.apache.struts2.StrutsStatics;
 
+import jakarta.servlet.jsp.PageContext;
 import java.util.AbstractMap;
 import java.util.Collection;
 import java.util.Collections;
@@ -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 7ddbc55b3..d572a573e 100644
--- a/core/src/test/java/org/apache/struts2/dispatcher/AttributeMapTest.java
+++ b/core/src/test/java/org/apache/struts2/dispatcher/AttributeMapTest.java
@@ -18,18 +18,6 @@
  */
 package org.apache.struts2.dispatcher;
 
-import static org.hamcrest.CoreMatchers.hasItem;
-import static org.hamcrest.MatcherAssert.assertThat;
-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 java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
 import org.apache.struts2.StrutsStatics;
 import org.junit.Test;
 import org.springframework.mock.web.MockHttpServletRequest;
@@ -41,6 +29,22 @@ import jakarta.servlet.ServletContext;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpSession;
 import jakarta.servlet.jsp.PageContext;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.hasItem;
+import static org.hamcrest.MatcherAssert.assertThat;
+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 {
 
@@ -361,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);
+
+        var req = new MockHttpServletRequest();
+        req.setAttribute("attr", "reqValue");
+
+        var attributeMap = new AttributeMap(Map.of(
+                StrutsStatics.PAGE_CONTEXT, pageContext,
+                DispatcherConstants.REQUEST, new RequestMap(req)
+        ));
+
+        assertEquals("reqValue", attributeMap.get("attr"));
+        verify(pageContext, never()).findAttribute(anyString());
+    }
+
+}

Reply via email to