Merge pull request #406 from apache/WW-5070-root-action-model

[WW-5070] Adds more sophisticated logic to search for the Root
This commit is contained in:
Lukasz Lenart
2020-05-03 12:01:29 +02:00
committed by GitHub
@@ -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,7 +69,7 @@ import com.opensymphony.xwork2.util.WildcardUtil;
*/
public class JSONResult implements Result {
private static final long serialVersionUID = 8624350183189931165L;
private static final long serialVersionUID = 233903199020467341L;
private static final Logger LOG = LogManager.getLogger(JSONResult.class);
@@ -212,12 +213,21 @@ 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();
} else if (rootObject == null) {
LOG.debug("Neither #action nor ModelDriven, peeking up object from the top of the stack");
rootObject = stack.peek();
}
}
return rootObject;
}
@@ -239,7 +249,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 +295,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}, 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
*/