From 88b05cdaed62025d0eb8ed16ef38b1a892f95365 Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Mon, 14 Sep 2020 18:00:54 +0430 Subject: [PATCH] WW-5087 handle Parameter.Empty properly There was a bug with AliasInterceptor not handling the Parameter.Empty that is returned from HttpParameters.get(). Since HttpParameters.get() always returns a non-null value, the Evaluated object is treated as always being defined, which results in the empty value being set incorrectly on the stack. --- .../xwork2/interceptor/AliasInterceptor.java | 6 ++++- .../interceptor/AliasInterceptorTest.java | 24 +++++++++++++++++++ core/src/test/resources/xwork-sample.xml | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/opensymphony/xwork2/interceptor/AliasInterceptor.java b/core/src/main/java/com/opensymphony/xwork2/interceptor/AliasInterceptor.java index b2d9dbdbe..d5135bf68 100644 --- a/core/src/main/java/com/opensymphony/xwork2/interceptor/AliasInterceptor.java +++ b/core/src/main/java/com/opensymphony/xwork2/interceptor/AliasInterceptor.java @@ -32,6 +32,7 @@ import com.opensymphony.xwork2.util.reflection.ReflectionContextState; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.struts2.dispatcher.HttpParameters; +import org.apache.struts2.dispatcher.Parameter; import java.util.Map; @@ -173,7 +174,10 @@ public class AliasInterceptor extends AbstractInterceptor { HttpParameters contextParameters = ActionContext.getContext().getParameters(); if (null != contextParameters) { - value = new Evaluated(contextParameters.get(name)); + Parameter param = contextParameters.get(name); + if (param.isDefined()) { + value = new Evaluated(param.getValue()); + } } } if (value.isDefined()) { diff --git a/core/src/test/java/com/opensymphony/xwork2/interceptor/AliasInterceptorTest.java b/core/src/test/java/com/opensymphony/xwork2/interceptor/AliasInterceptorTest.java index 6c0480db3..90141252e 100644 --- a/core/src/test/java/com/opensymphony/xwork2/interceptor/AliasInterceptorTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/interceptor/AliasInterceptorTest.java @@ -23,6 +23,7 @@ import com.opensymphony.xwork2.config.entities.ActionConfig; import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider; import com.opensymphony.xwork2.mock.MockActionInvocation; import com.opensymphony.xwork2.mock.MockActionProxy; +import org.apache.struts2.dispatcher.HttpParameters; import java.util.HashMap; import java.util.Map; @@ -60,7 +61,30 @@ public class AliasInterceptorTest extends XWorkTestCase { actionOne.setFoo(17); actionOne.setBar(23); proxy.execute(); + assertEquals("name to be copied", actionOne.getAliasSource()); assertEquals(actionOne.getAliasSource(), actionOne.getAliasDest()); + assertNull(actionOne.getBlah()); // WW-5087 + } + + public void testNotExisting() throws Exception { + Map params = new HashMap<>(); + Map httpParams = new HashMap<>(); + httpParams.put("notExisting", "from http parameter"); + params.put(ActionContext.PARAMETERS, HttpParameters.create(httpParams).build()); + + XmlConfigurationProvider provider = new XmlConfigurationProvider("xwork-sample.xml"); + container.inject(provider); + loadConfigurationProviders(provider); + ActionProxy proxy = actionProxyFactory.createActionProxy("", "aliasTest", null, params); + SimpleAction actionOne = (SimpleAction) proxy.getAction(); + + // prevent ERROR result + actionOne.setFoo(-1); + actionOne.setBar(1); + + proxy.execute(); + assertEquals("from http parameter", actionOne.getBlah()); + assertNull(actionOne.getAliasDest()); // WW-5087 } public void testInvalidAliasExpression() throws Exception { diff --git a/core/src/test/resources/xwork-sample.xml b/core/src/test/resources/xwork-sample.xml index 875c4e5e2..5ff1e39e3 100644 --- a/core/src/test/resources/xwork-sample.xml +++ b/core/src/test/resources/xwork-sample.xml @@ -97,7 +97,7 @@ - #{ "aliasSource" : "aliasDest", "bar":"baz" } + #{ "aliasSource" : "aliasDest", "bar":"baz", "notExisting":"blah" }