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

lukaszlenart pushed a commit to branch fix/WW-5424-class-cast-exception
in repository https://gitbox.apache.org/repos/asf/struts.git

commit 4a8ff99b1cc04bc69927aa3508c64fcb9d95519e
Author: Lukasz Lenart <lukaszlen...@apache.org>
AuthorDate: Sun Jun 2 13:53:37 2024 +0200

    WW-5424 Fixes ClassCastException when using short var name in s:set tag
---
 .../java/org/apache/struts2/components/Set.java    | 12 ++---
 .../org/apache/struts2/views/jsp/SetTagTest.java   | 58 ++++++++++++++++++----
 2 files changed, 55 insertions(+), 15 deletions(-)

diff --git a/core/src/main/java/org/apache/struts2/components/Set.java 
b/core/src/main/java/org/apache/struts2/components/Set.java
index cca990ea8..4d8f7c898 100644
--- a/core/src/main/java/org/apache/struts2/components/Set.java
+++ b/core/src/main/java/org/apache/struts2/components/Set.java
@@ -104,17 +104,17 @@ public class Set extends ContextBean {
         body="";
 
         if (DispatcherConstants.APPLICATION.equalsIgnoreCase(scope)) {
-            stack.setValue("#application['" + getVar() + "']", o);
+            stack.setValue(String.format("#application[\"%s\"]", getVar()), o);
         } else if (DispatcherConstants.SESSION.equalsIgnoreCase(scope)) {
-            stack.setValue("#session['" + getVar() + "']", o);
+            stack.setValue(String.format("#session[\"%s\"]", getVar()), o);
         } else if (DispatcherConstants.REQUEST.equalsIgnoreCase(scope)) {
-            stack.setValue("#request['" + getVar() + "']", o);
+            stack.setValue(String.format("#request[\"%s\"]", getVar()), o);
         } else if (DispatcherConstants.PAGE.equalsIgnoreCase(scope)) {
-            stack.setValue("#attr['" + getVar() + "']", o, false);
+            stack.setValue(String.format("#attr[\"%s\"]", getVar()), o, false);
         } else {
             // Default scope is action. Note: The action scope handling also 
adds the var to the page scope.
-            stack.getContext().put(getVar(), o);
-            stack.setValue("#attr['" + getVar() + "']", o, false);
+            putInContext(o);
+            stack.setValue(String.format("#attr[\"%s\"]", getVar()), o, false);
         }
 
         return super.end(writer, body);
diff --git a/core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java 
b/core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java
index 9e5c30eb5..f14e07735 100644
--- a/core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java
+++ b/core/src/test/java/org/apache/struts2/views/jsp/SetTagTest.java
@@ -22,14 +22,10 @@ import com.mockobjects.servlet.MockJspWriter;
 import java.io.IOException;
 import javax.servlet.jsp.JspException;
 
-
-/**
- */
 public class SetTagTest extends AbstractUITagTest {
 
-    Chewbacca chewie;
-    SetTag tag;
-
+    private Chewbacca chewie;
+    private SetTag tag;
 
     public void testApplicationScope() throws JspException {
         tag.setName("foo");
@@ -397,6 +393,50 @@ public class SetTagTest extends AbstractUITagTest {
                 strutsBodyTagsAreReflectionEqual(tag, freshTag));
     }
 
+    public void testShortVarNameInPageScope() throws JspException {
+        tag.setName("f");
+        tag.setValue("name");
+        tag.setScope("page");
+
+        tag.doStartTag();
+        tag.doEndTag();
+
+        assertEquals("chewie", pageContext.getAttribute("f"));
+    }
+
+    public void testShortVarNameInRequestScope() throws JspException {
+        tag.setName("f");
+        tag.setValue("name");
+        tag.setScope("request");
+
+        tag.doStartTag();
+        tag.doEndTag();
+
+        assertEquals("chewie", request.getAttribute("f"));
+    }
+
+    public void testShortVarNameInSessionScope() throws JspException {
+        tag.setName("f");
+        tag.setValue("name");
+        tag.setScope("session");
+
+        tag.doStartTag();
+        tag.doEndTag();
+
+        assertEquals("chewie", session.get("f"));
+    }
+
+    public void testShortVarNameInApplicationScope() throws JspException {
+        tag.setName("f");
+        tag.setValue("name");
+        tag.setScope("application");
+
+        tag.doStartTag();
+        tag.doEndTag();
+
+        assertEquals("chewie", servletContext.getAttribute("f"));
+    }
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -408,9 +448,9 @@ public class SetTagTest extends AbstractUITagTest {
     }
 
 
-    public class Chewbacca {
-        String name;
-        boolean furry;
+    public static class Chewbacca {
+        private String name;
+        private boolean furry;
 
         public Chewbacca(String name, boolean furry) {
             this.name = name;

Reply via email to