Skip to content

Commit c10e233

Browse files
committed
Fix ActionPropertyAccessor on Spring Framework 6.2
Prior to this commit, if a Spring Expression Language (SpEL) expression did not include parentheses for a MultiAction method that does not accept a RequestContext argument, the flow would pass on Spring Framework versions prior to 6.2, but the same flow would fail on Spring Framework 6.2 M1 or later. The following is such an expression and is effectively a property reference which must be handled by a registered PropertyAccessor: "reportActions.evaluateReport". The reason for the change in behavior is that Spring Framework 6.2 M1 includes a bug fix for ordering SpEL PropertyAccessors. That bug fix correctly results in Spring Web Flow's ActionPropertyAccessor being evaluated before SpEL's ReflectivePropertyAccessor. Consequently, an expression such as "reportActions.evaluateReport" is now handled by ActionPropertyAccessor which indirectly requires that the action method accept a RequestContext argument. When the method does not accept a RequestContext argument, the flow fails with a NoSuchMethodException. To address that, this commit revises the implementation of canRead(...) in ActionPropertyAccessor so that it only returns `true` if the action method accepts a RequestContext argument. When ActionPropertyAccessor's canRead(...) method returns `false`, the generic ReflectivePropertyAccessor will be used instead, which restores the pre-6.2 behavior for such expressions. The test suite passes for the following Spring Framework versions. - ./gradlew -PspringFrameworkVersion=6.0.23 --rerun-tasks clean check - ./gradlew -PspringFrameworkVersion=6.1.14 --rerun-tasks clean check - ./gradlew -PspringFrameworkVersion=6.2.0-RC3 --rerun-tasks clean check See spring-projects/spring-framework#33861 See spring-projects/spring-framework#33862 Closes gh-1802
1 parent c055518 commit c10e233

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed

spring-webflow/src/main/java/org/springframework/webflow/expression/spel/ActionPropertyAccessor.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
*/
1616
package org.springframework.webflow.expression.spel;
1717

18+
import java.lang.reflect.Method;
19+
1820
import org.springframework.expression.AccessException;
1921
import org.springframework.expression.EvaluationContext;
2022
import org.springframework.expression.PropertyAccessor;
2123
import org.springframework.expression.TypedValue;
24+
import org.springframework.util.ReflectionUtils;
2225
import org.springframework.webflow.action.MultiAction;
2326
import org.springframework.webflow.execution.Action;
2427
import org.springframework.webflow.execution.AnnotatedAction;
28+
import org.springframework.webflow.execution.RequestContext;
2529

2630
/**
2731
* <p>
@@ -32,6 +36,7 @@
3236
* @see org.springframework.webflow.action.EvaluateAction
3337
*
3438
* @author Rossen Stoyanchev
39+
* @author Sam Brannen
3540
* @since 2.1
3641
*/
3742
public class ActionPropertyAccessor implements PropertyAccessor {
@@ -43,7 +48,16 @@ public Class<?>[] getSpecificTargetClasses() {
4348

4449
@Override
4550
public boolean canRead(EvaluationContext context, Object target, String name) {
46-
return true;
51+
// Ensure the target is an Action.
52+
if (!(target instanceof Action)) {
53+
return false;
54+
}
55+
// Ensure the method adheres to the signature required by:
56+
// Action: execute(RequestContext)
57+
// or
58+
// MultiAction: <method name>(RequestContext)
59+
Method method = ReflectionUtils.findMethod(target.getClass(), name, RequestContext.class);
60+
return (method != null);
4761
}
4862

4963
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2004-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.webflow.action;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.stereotype.Component;
27+
import org.springframework.test.annotation.DirtiesContext;
28+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
29+
import org.springframework.webflow.config.AbstractFlowConfiguration;
30+
import org.springframework.webflow.definition.registry.FlowDefinitionRegistry;
31+
import org.springframework.webflow.execution.Event;
32+
import org.springframework.webflow.execution.RequestContext;
33+
import org.springframework.webflow.executor.FlowExecutor;
34+
import org.springframework.webflow.test.MockExternalContext;
35+
36+
/**
37+
* Integration tests for {@link MultiAction}.
38+
*
39+
* @author Sam Brannen
40+
* @since 3.0.1
41+
* @see <a href="https://github.com/spring-projects/spring-webflow/issues/1802">gh-1802</a>
42+
*/
43+
@SpringJUnitConfig
44+
@DirtiesContext
45+
class MultiActionIntegrationTests {
46+
47+
private static final String WITH_REQUEST_CONTEXT = "withRequestContext";
48+
49+
private static final String WITHOUT_REQUEST_CONTEXT = "withoutRequestContext";
50+
51+
52+
@Autowired
53+
FlowExecutor flowExecutor;
54+
55+
@Autowired
56+
CountingMultiAction multiAction;
57+
58+
59+
@BeforeEach
60+
void resetCounters() {
61+
multiAction.counterWithRequestContext = 0;
62+
multiAction.counterWithoutRequestContext = 0;
63+
}
64+
65+
@Test
66+
void spelExpressionsWithRequestContext() {
67+
assertCounters(0, 0);
68+
launchFlowExecution(WITH_REQUEST_CONTEXT);
69+
assertCounters(2, 0);
70+
}
71+
72+
@Test
73+
void spelExpressionsWithoutRequestContext() {
74+
assertCounters(0, 0);
75+
launchFlowExecution(WITHOUT_REQUEST_CONTEXT);
76+
assertCounters(0, 2);
77+
}
78+
79+
private void launchFlowExecution(String flowId) {
80+
flowExecutor.launchExecution(flowId, null, new MockExternalContext());
81+
}
82+
83+
private void assertCounters(int counterWithRequestContext, int counterWithoutRequestContext) {
84+
assertEquals(counterWithRequestContext, multiAction.counterWithRequestContext, "counterWithRequestContext");
85+
assertEquals(counterWithoutRequestContext, multiAction.counterWithoutRequestContext, "counterWithoutRequestContext");
86+
}
87+
88+
89+
90+
@Configuration
91+
static class WebFlowConfig extends AbstractFlowConfiguration {
92+
93+
@Bean
94+
CountingMultiAction countingMultiAction() {
95+
return new CountingMultiAction();
96+
}
97+
98+
@Bean
99+
FlowExecutor flowExecutor() {
100+
return getFlowExecutorBuilder(flowRegistry()).build();
101+
}
102+
103+
@Bean
104+
FlowDefinitionRegistry flowRegistry() {
105+
return getFlowDefinitionRegistryBuilder()
106+
.setBasePath("classpath:/org/springframework/webflow/action")
107+
.addFlowLocation("multi-action-with-request-context.xml", WITH_REQUEST_CONTEXT)
108+
.addFlowLocation("multi-action-without-request-context.xml", WITHOUT_REQUEST_CONTEXT)
109+
.build();
110+
}
111+
}
112+
113+
@Component("countingMultiAction")
114+
static class CountingMultiAction extends MultiAction {
115+
116+
int counterWithRequestContext = 0;
117+
118+
int counterWithoutRequestContext = 0;
119+
120+
public Event incrementWithRequestContext(RequestContext context) {
121+
counterWithRequestContext++;
122+
return success();
123+
}
124+
125+
public Event incrementWithoutRequestContext() {
126+
counterWithoutRequestContext++;
127+
return success();
128+
}
129+
}
130+
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<flow xmlns="http://www.springframework.org/schema/webflow"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/webflow https://www.springframework.org/schema/webflow/spring-webflow.xsd">
4+
5+
<on-start>
6+
<!-- Property Access: handled by org.springframework.webflow.expression.spel.ActionPropertyAccessor -->
7+
<evaluate expression="countingMultiAction.incrementWithRequestContext" />
8+
9+
<!-- Method Invocation: handled by org.springframework.expression.spel.ast.MethodReference -->
10+
<evaluate expression="countingMultiAction.incrementWithRequestContext(#requestContext)" />
11+
</on-start>
12+
13+
<end-state id="end" />
14+
15+
</flow>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<flow xmlns="http://www.springframework.org/schema/webflow"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/webflow https://www.springframework.org/schema/webflow/spring-webflow.xsd">
4+
5+
<on-start>
6+
<!-- Property Access: handled by org.springframework.expression.spel.support.ReflectivePropertyAccessor -->
7+
<evaluate expression="countingMultiAction.incrementWithoutRequestContext" />
8+
9+
<!-- Method Invocation: handled by org.springframework.expression.spel.ast.MethodReference -->
10+
<evaluate expression="countingMultiAction.incrementWithoutRequestContext()" />
11+
</on-start>
12+
13+
<end-state id="end" />
14+
15+
</flow>

0 commit comments

Comments
 (0)