Skip to content

Commit a33adac

Browse files
committed
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
1 parent 08663d4 commit a33adac

File tree

2 files changed

+277
-135
lines changed

2 files changed

+277
-135
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,6 +65,7 @@
6565
*
6666
* @author Rossen Stoyanchev
6767
* @author Juergen Hoeller
68+
* @author Sam Brannen
6869
* @since 5.0
6970
*/
7071
public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentResolverSupport {
@@ -122,7 +123,7 @@ public Mono<Object> resolveArgument(
122123

123124
return valueMono.flatMap(value -> {
124125
WebExchangeDataBinder binder = context.createDataBinder(exchange, value, name);
125-
return bindRequestParameters(binder, exchange)
126+
return (bindingDisabled(parameter) ? Mono.empty() : bindRequestParameters(binder, exchange))
126127
.doOnError(bindingResultMono::onError)
127128
.doOnSuccess(aVoid -> {
128129
validateIfApplicable(binder, parameter);
@@ -147,6 +148,16 @@ public Mono<Object> resolveArgument(
147148
});
148149
}
149150

151+
/**
152+
* Determine if binding should be disabled for the supplied {@link MethodParameter},
153+
* based on the {@link ModelAttribute#binding} annotation attribute.
154+
* @since 5.2.15
155+
*/
156+
private boolean bindingDisabled(MethodParameter parameter) {
157+
ModelAttribute modelAttribute = parameter.getParameterAnnotation(ModelAttribute.class);
158+
return (modelAttribute != null && !modelAttribute.binding());
159+
}
160+
150161
/**
151162
* Extension point to bind the request to the target object.
152163
* @param binder the data binder instance to use for the binding

0 commit comments

Comments
 (0)