Skip to content

Commit

Permalink
WW-5378 Add option to disable ValueStack context fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Dec 27, 2023
1 parent f6fbab9 commit eb3928c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -172,6 +173,11 @@ protected void setLogMissingProperties(String logMissingProperties) {
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()
*/
Expand Down Expand Up @@ -417,6 +423,9 @@ private Object tryFindValue(String expr, Class asType) throws OgnlException {
}

protected Object findInContext(String name) {
if (!shouldFallbackToContext) {
return null;
}
return getContext().get(name);
}

Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/org/apache/struts2/StrutsConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1122,9 +1122,18 @@ public void testExpOverrides() {
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() {
Expand Down

0 comments on commit eb3928c

Please sign in to comment.