Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BindingResult error when ModelAttribute has custom name in WebFlux #28422

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -86,7 +86,7 @@ private Object getErrors(MethodParameter parameter, BindingContext context) {
"Either declare the @ModelAttribute without an async wrapper type or " +
"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()) ?
ann.value() : Conventions.getVariableNameForParameter(attributeParam));
Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name);
Expand Down
Expand Up @@ -81,6 +81,21 @@ void resolve() {
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) {
DataBinder binder = this.bindingContext.createDataBinder(this.exchange, target, name);
return binder.getBindingResult();
Expand All @@ -98,6 +113,21 @@ void resolveWithMono() {
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
void resolveWithMonoOnBindingResultAndModelAttribute() {
MethodParameter parameter = this.testMethod.arg(BindingResult.class);
Expand Down Expand Up @@ -150,4 +180,13 @@ void handle(
String string) {
}

@SuppressWarnings("unused")
void handleWithModelAttributeValue(
@ModelAttribute("custom") Foo foo,
Errors errors,
@ModelAttribute Mono<Foo> fooMono,
BindingResult bindingResult,
Mono<Errors> errorsMono,
String string) {
}
}