Support declarativeBinding mode in DataBinder

Closes gh-30948
This commit is contained in:
rstoyanchev
2023-07-25 18:04:21 +03:00
parent 37eaded63d
commit 67e3d86bd8
7 changed files with 106 additions and 0 deletions

View File

@@ -140,6 +140,9 @@ public class ServletRequestDataBinder extends WebDataBinder {
* @see #bind(org.springframework.beans.PropertyValues)
*/
public void bind(ServletRequest request) {
if (shouldNotBindPropertyValues()) {
return;
}
MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);
MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);
if (multipartRequest != null) {

View File

@@ -44,6 +44,9 @@ public class ConfigurableWebBindingInitializer implements WebBindingInitializer
private boolean directFieldAccess = false;
@Nullable
private Boolean declarativeBinding;
@Nullable
private MessageCodesResolver messageCodesResolver;
@@ -99,6 +102,23 @@ public class ConfigurableWebBindingInitializer implements WebBindingInitializer
return this.directFieldAccess;
}
/**
* Set whether to bind only fields intended for binding as described in
* {@link org.springframework.validation.DataBinder#setDeclarativeBinding}.
* @since 6.1
*/
public void setDeclarativeBinding(boolean declarativeBinding) {
this.declarativeBinding = declarativeBinding;
}
/**
* Return whether to bind only fields intended for binding.
* @since 6.1
*/
public boolean isDeclarativeBinding() {
return (this.declarativeBinding != null ? this.declarativeBinding : false);
}
/**
* Set the strategy to use for resolving errors into message codes.
* Applies the given strategy to all data binders used by this controller.
@@ -197,6 +217,9 @@ public class ConfigurableWebBindingInitializer implements WebBindingInitializer
if (this.directFieldAccess) {
binder.initDirectFieldAccess();
}
if (this.declarativeBinding != null) {
binder.setDeclarativeBinding(this.declarativeBinding);
}
if (this.messageCodesResolver != null) {
binder.setMessageCodesResolver(this.messageCodesResolver);
}

View File

@@ -93,6 +93,9 @@ public class WebExchangeDataBinder extends WebDataBinder {
* @return a {@code Mono<Void>} that completes when binding is complete
*/
public Mono<Void> bind(ServerWebExchange exchange) {
if (shouldNotBindPropertyValues()) {
return Mono.empty();
}
return getValuesToBind(exchange)
.doOnNext(map -> doBind(new MutablePropertyValues(map)))
.then();

View File

@@ -138,6 +138,9 @@ public class WebRequestDataBinder extends WebDataBinder {
* @see #bind(org.springframework.beans.PropertyValues)
*/
public void bind(WebRequest request) {
if (shouldNotBindPropertyValues()) {
return;
}
MutablePropertyValues mpvs = new MutablePropertyValues(request.getParameterMap());
if (request instanceof NativeWebRequest nativeRequest) {
MultipartRequest multipartRequest = nativeRequest.getNativeRequest(MultipartRequest.class);