You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2020/04/25 07:10:14 UTC

[struts] 01/01: WW-5070 Adds more sophisticated logic to search for the Root

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

lukaszlenart pushed a commit to branch WW-5070-root-action-model
in repository https://gitbox.apache.org/repos/asf/struts.git

commit bd8ab61f8f8299a7f78518e495578d6921a69844
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Sat Apr 25 09:09:15 2020 +0200

    WW-5070 Adds more sophisticated logic to search for the Root
---
 .../main/java/org/apache/struts2/json/JSONResult.java  | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java b/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java
index 0547e92..646c44f 100644
--- a/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java
+++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONResult.java
@@ -27,6 +27,7 @@ import java.util.regex.Pattern;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import com.opensymphony.xwork2.ModelDriven;
 import org.apache.commons.lang3.BooleanUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.LogManager;
@@ -68,8 +69,6 @@ import com.opensymphony.xwork2.util.WildcardUtil;
  */
 public class JSONResult implements Result {
 
-    private static final long serialVersionUID = 8624350183189931165L;
-
     private static final Logger LOG = LogManager.getLogger(JSONResult.class);
 
     /**
@@ -212,12 +211,18 @@ public class JSONResult implements Result {
     }
 
     protected Object findRootObject(ActionInvocation invocation) {
+        ValueStack stack = invocation.getStack();
         Object rootObject;
         if (this.root != null) {
-            ValueStack stack = invocation.getStack();
+            LOG.debug("Root was defined as [{}], searching stack for it", this.root);
             rootObject = stack.findValue(root);
         } else {
-            rootObject = invocation.getStack().peek(); // model overrides action
+            LOG.debug("Root was not defined, searching for #action");
+            rootObject = stack.findValue("#action");
+            if (rootObject instanceof ModelDriven) {
+                LOG.debug("Action is an instance of ModelDriven, assuming model is on the top of the stack and using it");
+                rootObject = stack.peek();
+            }
         }
         return rootObject;
     }
@@ -239,7 +244,6 @@ public class JSONResult implements Result {
                 wrapSuffix));
     }
 
-    @SuppressWarnings("unchecked")
     protected org.apache.struts2.json.smd.SMD buildSMDObject(ActionInvocation invocation) {
         return new SMDGenerator(findRootObject(invocation), excludeProperties, ignoreInterfaces).generate(invocation);
     }
@@ -286,7 +290,9 @@ public class JSONResult implements Result {
     }
 
     /**
-     * Sets the root object to be serialized, defaults to the Action
+     * Sets the root object to be serialized, defaults to the Action.
+     * If the Action implements {@link ModelDriven}, model will be used instead
+     * and assumptions is the Model was pushed on the top of the stack
      *
      * @param root OGNL expression of root object to be serialized
      */