You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by ku...@apache.org on 2025/02/03 14:00:46 UTC

(struts) branch WW-5516-attrmap-npe-67 created (now c36eafa37)

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

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


      at c36eafa37 WW-5516 Fix AttributeMap NPE when PageContext has no request

This branch includes the following new commits:

     new c36eafa37 WW-5516 Fix AttributeMap NPE when PageContext has no request

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(struts) 01/01: WW-5516 Fix AttributeMap NPE when PageContext has no request

Posted by ku...@apache.org.
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 <gi...@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());
+    }
+
+}