From 19c71ff1537711ce0b802545e8327d564ee8f769 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Sat, 25 Apr 2020 09:09:15 +0200 Subject: [PATCH 1/4] WW-5070 Adds more sophisticated logic to search for the Root --- .../org/apache/struts2/json/JSONResult.java | 22 ++++++++++++++----- 1 file changed, 16 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 0547e92c8..5c38e58cb 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,22 @@ 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(); + } + if (rootObject == null) { + LOG.debug("Neither #action nor ModelDriven, peeking up object from top of the stack"); + rootObject = stack.peek(); + } } return rootObject; } @@ -239,7 +248,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 +294,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 */ From 333f7812073a51530407538033fa6ff747470c02 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Mon, 27 Apr 2020 08:17:35 +0200 Subject: [PATCH 2/4] WW-5070 Generates a new serialVersionUID --- .../json/src/main/java/org/apache/struts2/json/JSONResult.java | 2 ++ 1 file changed, 2 insertions(+) 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 5c38e58cb..bb35bf2de 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 @@ -69,6 +69,8 @@ import com.opensymphony.xwork2.util.WildcardUtil; */ public class JSONResult implements Result { + private static final long serialVersionUID = 233903199020467341L; + private static final Logger LOG = LogManager.getLogger(JSONResult.class); /** From 48435c42095b201f606eb39e86137cb213bfbd4d Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Mon, 27 Apr 2020 08:19:29 +0200 Subject: [PATCH 3/4] WW-5070 Uses else if to avoid double operation on stack --- .../src/main/java/org/apache/struts2/json/JSONResult.java | 5 ++--- 1 file changed, 2 insertions(+), 3 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 bb35bf2de..ba46d96be 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 @@ -224,9 +224,8 @@ public class JSONResult implements Result { 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(); - } - if (rootObject == null) { - LOG.debug("Neither #action nor ModelDriven, peeking up object from top of the stack"); + } else if (rootObject == null) { + LOG.debug("Neither #action nor ModelDriven, peeking up object from the top of the stack"); rootObject = stack.peek(); } } From d826efb9d09d2fc0b09a2eafde5bafc38ff3c6a8 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Mon, 27 Apr 2020 08:20:39 +0200 Subject: [PATCH 4/4] WW-5070 Re-phrases JavaDoc for setRoot() method --- .../src/main/java/org/apache/struts2/json/JSONResult.java | 4 ++-- 1 file changed, 2 insertions(+), 2 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 ba46d96be..26c17357a 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 @@ -296,8 +296,8 @@ public class JSONResult implements Result { /** * 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 + * If the Action implements {@link ModelDriven}, the Model will be used instead, + * with the logic assuming the Model was pushed onto the top of the stack. * * @param root OGNL expression of root object to be serialized */