Support @ModelAttribute(binding=false) with WebFlux

Prior to this commit, @ModelAttribute(binding=false) was honored with
Spring Web MVC but not with WebFlux.

This commit adds support for disabling binding via @ModelAttribute with
WebFlux by adding a check to resolveArgument(...) in
ModelAttributeMethodArgumentResolver.

Closes gh-26856
This commit is contained in:
Sam Brannen
2021-04-29 15:20:28 +02:00
parent 5667d459a7
commit 66cd979ce4
2 changed files with 275 additions and 133 deletions

View File

@@ -60,6 +60,7 @@ import org.springframework.web.server.ServerWebExchange;
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @author Sam Brannen
* @since 5.0
*/
public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentResolverSupport {
@@ -117,7 +118,7 @@ public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentR
return valueMono.flatMap(value -> {
WebExchangeDataBinder binder = context.createDataBinder(exchange, value, name);
return bindRequestParameters(binder, exchange)
return (bindingDisabled(parameter) ? Mono.empty() : bindRequestParameters(binder, exchange))
.doOnError(bindingResultSink::tryEmitError)
.doOnSuccess(aVoid -> {
validateIfApplicable(binder, parameter);
@@ -143,6 +144,16 @@ public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentR
});
}
/**
* Determine if binding should be disabled for the supplied {@link MethodParameter},
* based on the {@link ModelAttribute#binding} annotation attribute.
* @since 5.3.7
*/
private boolean bindingDisabled(MethodParameter parameter) {
ModelAttribute modelAttribute = parameter.getParameterAnnotation(ModelAttribute.class);
return (modelAttribute != null && !modelAttribute.binding());
}
/**
* Extension point to bind the request to the target object.
* @param binder the data binder instance to use for the binding