Skip to content

Commit 7dd622b

Browse files
committed
Support name attribute in @ModelAttribute in WebFlux
Prior to this commit, the `name` attribute in @ModelAttribute was not supported when using WebFlux. This is because MethodParameter was used instead of SynthesizingMethodParameter when retrieving the @ModelAttribute annotation. In other words, @AliasFor was not honored because the annotation was not synthesized. Consequently, only the `value` attribute was supported in WebFlux when specifying a custom name via @ModelAttribute. This commit fixes this by using SynthesizingMethodParameter to retrieve the @ModelAttribute annotation. Closes gh-28423
1 parent 64c96c5 commit 7dd622b

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ErrorsMethodArgumentResolver.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.core.MethodParameter;
2323
import org.springframework.core.ReactiveAdapter;
2424
import org.springframework.core.ReactiveAdapterRegistry;
25+
import org.springframework.core.annotation.SynthesizingMethodParameter;
2526
import org.springframework.util.Assert;
2627
import org.springframework.util.StringUtils;
2728
import org.springframework.validation.BindingResult;
@@ -38,6 +39,7 @@
3839
* model attribute in the method signature.
3940
*
4041
* @author Rossen Stoyanchev
42+
* @author Sam Brannen
4143
* @since 5.0
4244
*/
4345
public class ErrorsMethodArgumentResolver extends HandlerMethodArgumentResolverSupport {
@@ -79,7 +81,7 @@ private Object getErrors(MethodParameter parameter, BindingContext context) {
7981
"Errors argument must be declared immediately after a model attribute argument");
8082

8183
int index = parameter.getParameterIndex() - 1;
82-
MethodParameter attributeParam = MethodParameter.forExecutable(parameter.getExecutable(), index);
84+
MethodParameter attributeParam = SynthesizingMethodParameter.forExecutable(parameter.getExecutable(), index);
8385
ReactiveAdapter adapter = getAdapterRegistry().getAdapter(attributeParam.getParameterType());
8486

8587
Assert.state(adapter == null, "An @ModelAttribute and an Errors/BindingResult argument " +
@@ -88,8 +90,8 @@ private Object getErrors(MethodParameter parameter, BindingContext context) {
8890
"handle a WebExchangeBindException error signal through the async type.");
8991

9092
ModelAttribute ann = attributeParam.getParameterAnnotation(ModelAttribute.class);
91-
String name = (ann != null && StringUtils.hasText(ann.value()) ?
92-
ann.value() : Conventions.getVariableNameForParameter(attributeParam));
93+
String name = (ann != null && StringUtils.hasText(ann.name()) ? ann.name() :
94+
Conventions.getVariableNameForParameter(attributeParam));
9395
Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name);
9496

9597
Assert.state(errors != null, () -> "An Errors/BindingResult argument is expected " +

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ErrorsMethodArgumentResolverTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void handle(
180180

181181
@SuppressWarnings("unused")
182182
void handleWithCustomModelAttributeName(
183-
@ModelAttribute("custom") Foo foo,
183+
@ModelAttribute(name = "custom") Foo foo,
184184
Errors errors,
185185
@ModelAttribute Mono<Foo> fooMono,
186186
BindingResult bindingResult,

0 commit comments

Comments
 (0)