WW-5378 Add option to disable ValueStack context fallback

This commit is contained in:
Kusal Kithul-Godage
2023-12-28 00:45:21 +11:00
parent f6fbab9a40
commit eb3928cb15
3 changed files with 27 additions and 1 deletions
@@ -76,6 +76,7 @@ public class OgnlValueStack implements Serializable, ValueStack, ClearableValueS
private transient XWorkConverter converter;
private boolean devMode;
private boolean logMissingProperties;
private boolean shouldFallbackToContext = true;
/**
* @since 6.4.0
@@ -172,6 +173,11 @@ public class OgnlValueStack implements Serializable, ValueStack, ClearableValueS
this.logMissingProperties = BooleanUtils.toBoolean(logMissingProperties);
}
@Inject(value = StrutsConstants.STRUTS_OGNL_VALUE_STACK_FALLBACK_TO_CONTEXT, required = false)
protected void setShouldFallbackToContext(String shouldFallbackToContext) {
this.shouldFallbackToContext = BooleanUtils.toBoolean(shouldFallbackToContext);
}
/**
* @see com.opensymphony.xwork2.util.ValueStack#getContext()
*/
@@ -417,6 +423,9 @@ public class OgnlValueStack implements Serializable, ValueStack, ClearableValueS
}
protected Object findInContext(String name) {
if (!shouldFallbackToContext) {
return null;
}
return getContext().get(name);
}
@@ -308,6 +308,14 @@ public final class StrutsConstants {
*/
public static final String STRUTS_OGNL_LOG_MISSING_PROPERTIES = "struts.ognl.logMissingProperties";
/**
* Determines whether lookups on the ValueStack should fallback to looking in the context if the OGNL expression
* fails or returns null.
*
* @since 6.4.0
*/
public static final String STRUTS_OGNL_VALUE_STACK_FALLBACK_TO_CONTEXT = "struts.ognl.valueStackFallbackToContext";
/**
* Logs properties that are not found (very verbose)
* @deprecated as of 6.0.0. Use {@link #STRUTS_OGNL_LOG_MISSING_PROPERTIES} instead.
@@ -1122,9 +1122,18 @@ public class OgnlValueStackTest extends XWorkTestCase {
assertEquals("Hello World", vs.findValue("claus", String.class));
assertEquals("Hello World", vs.findValue("top", String.class));
assertNull(vs.findValue("unknown", String.class));
}
public void testExprFallbackToContext() {
vs.getContext().put("santa", "Hello Santa");
assertEquals("Hello Santa", vs.findValue("santa", String.class));
assertNull(vs.findValue("unknown", String.class));
}
public void testExprFallbackToContext_disabled() {
vs.setShouldFallbackToContext("false");
vs.getContext().put("santa", "Hello Santa");
assertNull(vs.findValue("santa", String.class));
}
public void testWarnAboutInvalidProperties() {