Fix BindingResult error when ModelAttribute has custom name in WebFlux
Closes gh-28422
This commit is contained in:
parent
f771603789
commit
39e3876301
|
|
@ -86,7 +86,7 @@ public class ErrorsMethodArgumentResolver extends HandlerMethodArgumentResolverS
|
||||||
"Either declare the @ModelAttribute without an async wrapper type or " +
|
"Either declare the @ModelAttribute without an async wrapper type or " +
|
||||||
"handle a WebExchangeBindException error signal through the async type.");
|
"handle a WebExchangeBindException error signal through the async type.");
|
||||||
|
|
||||||
ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
|
ModelAttribute ann = attributeParam.getParameterAnnotation(ModelAttribute.class);
|
||||||
String name = (ann != null && StringUtils.hasText(ann.value()) ?
|
String name = (ann != null && StringUtils.hasText(ann.value()) ?
|
||||||
ann.value() : Conventions.getVariableNameForParameter(attributeParam));
|
ann.value() : Conventions.getVariableNameForParameter(attributeParam));
|
||||||
Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name);
|
Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name);
|
||||||
|
|
|
||||||
|
|
@ -81,6 +81,21 @@ class ErrorsMethodArgumentResolverTests {
|
||||||
assertThat(actual).isSameAs(bindingResult);
|
assertThat(actual).isSameAs(bindingResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void resolveOnBindingResultAndModelAttributeWithCustomValue() {
|
||||||
|
BindingResult bindingResult = createBindingResult(new Foo(), "custom");
|
||||||
|
this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "custom", bindingResult);
|
||||||
|
|
||||||
|
ResolvableMethod testMethod = ResolvableMethod.on(getClass())
|
||||||
|
.named("handleWithModelAttributeValue").build();
|
||||||
|
|
||||||
|
MethodParameter parameter = testMethod.arg(Errors.class);
|
||||||
|
Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
|
||||||
|
.block(Duration.ofMillis(5000));
|
||||||
|
|
||||||
|
assertThat(actual).isSameAs(bindingResult);
|
||||||
|
}
|
||||||
|
|
||||||
private BindingResult createBindingResult(Foo target, String name) {
|
private BindingResult createBindingResult(Foo target, String name) {
|
||||||
DataBinder binder = this.bindingContext.createDataBinder(this.exchange, target, name);
|
DataBinder binder = this.bindingContext.createDataBinder(this.exchange, target, name);
|
||||||
return binder.getBindingResult();
|
return binder.getBindingResult();
|
||||||
|
|
@ -98,6 +113,21 @@ class ErrorsMethodArgumentResolverTests {
|
||||||
assertThat(actual).isSameAs(bindingResult);
|
assertThat(actual).isSameAs(bindingResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void resolveWithMonoOnBindingResultAndModelAttributeWithCustomValue() {
|
||||||
|
BindingResult bindingResult = createBindingResult(new Foo(), "custom");
|
||||||
|
this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "custom", Mono.just(bindingResult));
|
||||||
|
|
||||||
|
ResolvableMethod testMethod = ResolvableMethod.on(getClass())
|
||||||
|
.named("handleWithModelAttributeValue").build();
|
||||||
|
|
||||||
|
MethodParameter parameter = testMethod.arg(Errors.class);
|
||||||
|
Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
|
||||||
|
.block(Duration.ofMillis(5000));
|
||||||
|
|
||||||
|
assertThat(actual).isSameAs(bindingResult);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void resolveWithMonoOnBindingResultAndModelAttribute() {
|
void resolveWithMonoOnBindingResultAndModelAttribute() {
|
||||||
MethodParameter parameter = this.testMethod.arg(BindingResult.class);
|
MethodParameter parameter = this.testMethod.arg(BindingResult.class);
|
||||||
|
|
@ -150,4 +180,13 @@ class ErrorsMethodArgumentResolverTests {
|
||||||
String string) {
|
String string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
void handleWithModelAttributeValue(
|
||||||
|
@ModelAttribute("custom") Foo foo,
|
||||||
|
Errors errors,
|
||||||
|
@ModelAttribute Mono<Foo> fooMono,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Mono<Errors> errorsMono,
|
||||||
|
String string) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue