Predetermine validation groups on initialization

Closes gh-32068
This commit is contained in:
Juergen Hoeller 2024-01-23 11:15:12 +01:00
parent 5656eaccb7
commit 5faace0eb3
2 changed files with 13 additions and 17 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@ -78,6 +78,8 @@ public class InvocableHandlerMethod extends HandlerMethod {
@Nullable
private MethodValidator methodValidator;
private Class<?>[] validationGroups = EMPTY_GROUPS;
/**
* Create an instance from a {@code HandlerMethod}.
@ -149,6 +151,8 @@ public class InvocableHandlerMethod extends HandlerMethod {
*/
public void setMethodValidator(@Nullable MethodValidator methodValidator) {
this.methodValidator = methodValidator;
this.validationGroups = (methodValidator != null ?
methodValidator.determineValidationGroups(getBean(), getBridgedMethod()) : EMPTY_GROUPS);
}
@ -180,17 +184,16 @@ public class InvocableHandlerMethod extends HandlerMethod {
logger.trace("Arguments: " + Arrays.toString(args));
}
Class<?>[] groups = getValidationGroups();
if (shouldValidateArguments() && this.methodValidator != null) {
this.methodValidator.applyArgumentValidation(
getBean(), getBridgedMethod(), getMethodParameters(), args, groups);
getBean(), getBridgedMethod(), getMethodParameters(), args, this.validationGroups);
}
Object returnValue = doInvoke(args);
if (shouldValidateReturnValue() && this.methodValidator != null) {
this.methodValidator.applyReturnValueValidation(
getBean(), getBridgedMethod(), getReturnType(), returnValue, groups);
getBean(), getBridgedMethod(), getReturnType(), returnValue, this.validationGroups);
}
return returnValue;
@ -238,11 +241,6 @@ public class InvocableHandlerMethod extends HandlerMethod {
return args;
}
private Class<?>[] getValidationGroups() {
return ((shouldValidateArguments() || shouldValidateReturnValue()) && this.methodValidator != null ?
this.methodValidator.determineValidationGroups(getBean(), getBridgedMethod()) : EMPTY_GROUPS);
}
/**
* Invoke the handler method with the given argument values.
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@ -87,6 +87,8 @@ public class InvocableHandlerMethod extends HandlerMethod {
@Nullable
private MethodValidator methodValidator;
private Class<?>[] validationGroups = EMPTY_GROUPS;
/**
* Create an instance from a {@code HandlerMethod}.
@ -151,6 +153,8 @@ public class InvocableHandlerMethod extends HandlerMethod {
*/
public void setMethodValidator(@Nullable MethodValidator methodValidator) {
this.methodValidator = methodValidator;
this.validationGroups = (methodValidator != null ?
methodValidator.determineValidationGroups(getBean(), getBridgedMethod()) : EMPTY_GROUPS);
}
@ -166,10 +170,9 @@ public class InvocableHandlerMethod extends HandlerMethod {
ServerWebExchange exchange, BindingContext bindingContext, Object... providedArgs) {
return getMethodArgumentValues(exchange, bindingContext, providedArgs).flatMap(args -> {
Class<?>[] groups = getValidationGroups();
if (shouldValidateArguments() && this.methodValidator != null) {
this.methodValidator.applyArgumentValidation(
getBean(), getBridgedMethod(), getMethodParameters(), args, groups);
getBean(), getBridgedMethod(), getMethodParameters(), args, this.validationGroups);
}
Object value;
Method method = getBridgedMethod();
@ -262,11 +265,6 @@ public class InvocableHandlerMethod extends HandlerMethod {
}
}
private Class<?>[] getValidationGroups() {
return ((shouldValidateArguments() || shouldValidateReturnValue()) && this.methodValidator != null ?
this.methodValidator.determineValidationGroups(getBean(), getBridgedMethod()) : EMPTY_GROUPS);
}
private static boolean isAsyncVoidReturnType(MethodParameter returnType, @Nullable ReactiveAdapter adapter) {
if (adapter != null && adapter.supportsEmpty()) {
if (adapter.isNoValue()) {