Skip to content

Commit

Permalink
Support @ModelAttribute(binding=false) with WebFlux
Browse files Browse the repository at this point in the history
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 spring-projectsgh-26856
  • Loading branch information
sbrannen committed May 3, 2021
1 parent 08663d4 commit a33adac
Show file tree
Hide file tree
Showing 2 changed files with 277 additions and 135 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
Expand Down Expand Up @@ -65,6 +65,7 @@
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @author Sam Brannen
* @since 5.0
*/
public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentResolverSupport {
Expand Down Expand Up @@ -122,7 +123,7 @@ public Mono<Object> resolveArgument(

return valueMono.flatMap(value -> {
WebExchangeDataBinder binder = context.createDataBinder(exchange, value, name);
return bindRequestParameters(binder, exchange)
return (bindingDisabled(parameter) ? Mono.empty() : bindRequestParameters(binder, exchange))
.doOnError(bindingResultMono::onError)
.doOnSuccess(aVoid -> {
validateIfApplicable(binder, parameter);
Expand All @@ -147,6 +148,16 @@ public Mono<Object> resolveArgument(
});
}

/**
* Determine if binding should be disabled for the supplied {@link MethodParameter},
* based on the {@link ModelAttribute#binding} annotation attribute.
* @since 5.2.15
*/
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
Expand Down
Loading

0 comments on commit a33adac

Please sign in to comment.