Skip to content

Commit

Permalink
Fix ActionPropertyAccessor on Spring Framework 6.2
Browse files Browse the repository at this point in the history
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 spring-projectsgh-1802
  • Loading branch information
sbrannen committed Nov 9, 2024
1 parent c055518 commit c10e233
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
*/
package org.springframework.webflow.expression.spel;

import java.lang.reflect.Method;

import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;
import org.springframework.util.ReflectionUtils;
import org.springframework.webflow.action.MultiAction;
import org.springframework.webflow.execution.Action;
import org.springframework.webflow.execution.AnnotatedAction;
import org.springframework.webflow.execution.RequestContext;

/**
* <p>
Expand All @@ -32,6 +36,7 @@
* @see org.springframework.webflow.action.EvaluateAction
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 2.1
*/
public class ActionPropertyAccessor implements PropertyAccessor {
Expand All @@ -43,7 +48,16 @@ public Class<?>[] getSpecificTargetClasses() {

@Override
public boolean canRead(EvaluationContext context, Object target, String name) {
return true;
// Ensure the target is an Action.
if (!(target instanceof Action)) {
return false;
}
// Ensure the method adheres to the signature required by:
// Action: execute(RequestContext)
// or
// MultiAction: <method name>(RequestContext)
Method method = ReflectionUtils.findMethod(target.getClass(), name, RequestContext.class);
return (method != null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright 2004-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.webflow.action;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.webflow.config.AbstractFlowConfiguration;
import org.springframework.webflow.definition.registry.FlowDefinitionRegistry;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
import org.springframework.webflow.executor.FlowExecutor;
import org.springframework.webflow.test.MockExternalContext;

/**
* Integration tests for {@link MultiAction}.
*
* @author Sam Brannen
* @since 3.0.1
* @see <a href="https://github.com/spring-projects/spring-webflow/issues/1802">gh-1802</a>
*/
@SpringJUnitConfig
@DirtiesContext
class MultiActionIntegrationTests {

private static final String WITH_REQUEST_CONTEXT = "withRequestContext";

private static final String WITHOUT_REQUEST_CONTEXT = "withoutRequestContext";


@Autowired
FlowExecutor flowExecutor;

@Autowired
CountingMultiAction multiAction;


@BeforeEach
void resetCounters() {
multiAction.counterWithRequestContext = 0;
multiAction.counterWithoutRequestContext = 0;
}

@Test
void spelExpressionsWithRequestContext() {
assertCounters(0, 0);
launchFlowExecution(WITH_REQUEST_CONTEXT);
assertCounters(2, 0);
}

@Test
void spelExpressionsWithoutRequestContext() {
assertCounters(0, 0);
launchFlowExecution(WITHOUT_REQUEST_CONTEXT);
assertCounters(0, 2);
}

private void launchFlowExecution(String flowId) {
flowExecutor.launchExecution(flowId, null, new MockExternalContext());
}

private void assertCounters(int counterWithRequestContext, int counterWithoutRequestContext) {
assertEquals(counterWithRequestContext, multiAction.counterWithRequestContext, "counterWithRequestContext");
assertEquals(counterWithoutRequestContext, multiAction.counterWithoutRequestContext, "counterWithoutRequestContext");
}



@Configuration
static class WebFlowConfig extends AbstractFlowConfiguration {

@Bean
CountingMultiAction countingMultiAction() {
return new CountingMultiAction();
}

@Bean
FlowExecutor flowExecutor() {
return getFlowExecutorBuilder(flowRegistry()).build();
}

@Bean
FlowDefinitionRegistry flowRegistry() {
return getFlowDefinitionRegistryBuilder()
.setBasePath("classpath:/org/springframework/webflow/action")
.addFlowLocation("multi-action-with-request-context.xml", WITH_REQUEST_CONTEXT)
.addFlowLocation("multi-action-without-request-context.xml", WITHOUT_REQUEST_CONTEXT)
.build();
}
}

@Component("countingMultiAction")
static class CountingMultiAction extends MultiAction {

int counterWithRequestContext = 0;

int counterWithoutRequestContext = 0;

public Event incrementWithRequestContext(RequestContext context) {
counterWithRequestContext++;
return success();
}

public Event incrementWithoutRequestContext() {
counterWithoutRequestContext++;
return success();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow https://www.springframework.org/schema/webflow/spring-webflow.xsd">

<on-start>
<!-- Property Access: handled by org.springframework.webflow.expression.spel.ActionPropertyAccessor -->
<evaluate expression="countingMultiAction.incrementWithRequestContext" />

<!-- Method Invocation: handled by org.springframework.expression.spel.ast.MethodReference -->
<evaluate expression="countingMultiAction.incrementWithRequestContext(#requestContext)" />
</on-start>

<end-state id="end" />

</flow>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow https://www.springframework.org/schema/webflow/spring-webflow.xsd">

<on-start>
<!-- Property Access: handled by org.springframework.expression.spel.support.ReflectivePropertyAccessor -->
<evaluate expression="countingMultiAction.incrementWithoutRequestContext" />

<!-- Method Invocation: handled by org.springframework.expression.spel.ast.MethodReference -->
<evaluate expression="countingMultiAction.incrementWithoutRequestContext()" />
</on-start>

<end-state id="end" />

</flow>

0 comments on commit c10e233

Please sign in to comment.