Merge pull request #446 from apache/WW-5096-fix-static

[WW-5096] Fix StaticParametersInterceptor param overwrite
This commit is contained in:
Lukasz Lenart
2020-11-20 07:47:20 +01:00
committed by GitHub
3 changed files with 56 additions and 14 deletions
@@ -227,21 +227,13 @@ public class StaticParametersInterceptor extends AbstractInterceptor {
protected void addParametersToContext(ActionContext ac, Map<String, ?> newParams) {
HttpParameters previousParams = ac.getParameters();
HttpParameters.Builder combinedParams = HttpParameters.create();
HttpParameters.Builder combinedParams;
if (overwrite) {
if (previousParams != null) {
combinedParams = combinedParams.withParent(previousParams);
}
if (newParams != null) {
combinedParams = combinedParams.withExtraParams(newParams);
}
combinedParams = HttpParameters.create().withParent( previousParams);
combinedParams = combinedParams.withExtraParams(newParams);
} else {
if (newParams != null) {
combinedParams = combinedParams.withExtraParams(newParams);
}
if (previousParams != null) {
combinedParams = combinedParams.withParent(previousParams);
}
combinedParams = HttpParameters.create(newParams);
combinedParams = combinedParams.withExtraParams(previousParams);
}
ac.setParameters(combinedParams.build());
}
@@ -192,7 +192,11 @@ public class HttpParameters implements Map<String, Parameter>, Cloneable {
for (Map.Entry<String, Object> entry : requestParameterMap.entrySet()) {
String name = entry.getKey();
Object value = entry.getValue();
parameters.put(name, new Parameter.Request(name, value));
if (value instanceof Parameter) {
parameters.put(name, (Parameter) value);
} else {
parameters.put(name, new Parameter.Request(name, value));
}
}
return new HttpParameters(parameters);
@@ -28,6 +28,7 @@ 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;
/**
@@ -139,6 +140,51 @@ public class StaticParametersInterceptorTest extends XWorkTestCase {
assertEquals(0, ActionContext.getContext().getParameters().keySet().size());
}
public void testOverwrite() throws Exception {
MockActionInvocation mai = new MockActionInvocation();
MockActionProxy map = new MockActionProxy();
ActionConfig ac = new ActionConfig.Builder("", "", "")
.addParam("name", "${hero}")
.build();
map.setConfig(ac);
mai.setProxy(map);
mai.setAction(new SimpleFooAction());
Map<String, String> existingParams = new HashMap<>();
existingParams.put("name", "Akash");
ActionContext.getContext().setParameters(HttpParameters.create(existingParams).build());
int before = ActionContext.getContext().getValueStack().size();
interceptor.setOverwrite("true");
interceptor.intercept(mai);
assertEquals(before, ActionContext.getContext().getValueStack().size());
assertEquals("${hero}", ActionContext.getContext().getParameters().get("name").toString());
}
public void testNoOverwrite() throws Exception {
MockActionInvocation mai = new MockActionInvocation();
MockActionProxy map = new MockActionProxy();
ActionConfig ac = new ActionConfig.Builder("", "", "")
.addParam("name", "${hero}")
.build();
map.setConfig(ac);
mai.setProxy(map);
mai.setAction(new SimpleFooAction());
mai.setInvocationContext(ActionContext.getContext());
Map<String, String> existingParams = new HashMap<>();
existingParams.put("name", "Akash");
ActionContext.getContext().setParameters(HttpParameters.create(existingParams).build());
int before = ActionContext.getContext().getValueStack().size();
interceptor.setOverwrite("false");
interceptor.intercept(mai);
assertEquals(before, ActionContext.getContext().getValueStack().size());
assertEquals("Akash", ActionContext.getContext().getParameters().get("name").toString());
}
public void testFewParametersParse() throws Exception {
MockActionInvocation mai = new MockActionInvocation();
MockActionProxy map = new MockActionProxy();