Skip to content

Commit 5faace0

Browse files
committed
Predetermine validation groups on initialization
Closes gh-32068
1 parent 5656eac commit 5faace0

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

Diff for: spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -78,6 +78,8 @@ public class InvocableHandlerMethod extends HandlerMethod {
7878
@Nullable
7979
private MethodValidator methodValidator;
8080

81+
private Class<?>[] validationGroups = EMPTY_GROUPS;
82+
8183

8284
/**
8385
* Create an instance from a {@code HandlerMethod}.
@@ -149,6 +151,8 @@ public void setDataBinderFactory(WebDataBinderFactory dataBinderFactory) {
149151
*/
150152
public void setMethodValidator(@Nullable MethodValidator methodValidator) {
151153
this.methodValidator = methodValidator;
154+
this.validationGroups = (methodValidator != null ?
155+
methodValidator.determineValidationGroups(getBean(), getBridgedMethod()) : EMPTY_GROUPS);
152156
}
153157

154158

@@ -180,17 +184,16 @@ public Object invokeForRequest(NativeWebRequest request, @Nullable ModelAndViewC
180184
logger.trace("Arguments: " + Arrays.toString(args));
181185
}
182186

183-
Class<?>[] groups = getValidationGroups();
184187
if (shouldValidateArguments() && this.methodValidator != null) {
185188
this.methodValidator.applyArgumentValidation(
186-
getBean(), getBridgedMethod(), getMethodParameters(), args, groups);
189+
getBean(), getBridgedMethod(), getMethodParameters(), args, this.validationGroups);
187190
}
188191

189192
Object returnValue = doInvoke(args);
190193

191194
if (shouldValidateReturnValue() && this.methodValidator != null) {
192195
this.methodValidator.applyReturnValueValidation(
193-
getBean(), getBridgedMethod(), getReturnType(), returnValue, groups);
196+
getBean(), getBridgedMethod(), getReturnType(), returnValue, this.validationGroups);
194197
}
195198

196199
return returnValue;
@@ -238,11 +241,6 @@ protected Object[] getMethodArgumentValues(NativeWebRequest request, @Nullable M
238241
return args;
239242
}
240243

241-
private Class<?>[] getValidationGroups() {
242-
return ((shouldValidateArguments() || shouldValidateReturnValue()) && this.methodValidator != null ?
243-
this.methodValidator.determineValidationGroups(getBean(), getBridgedMethod()) : EMPTY_GROUPS);
244-
}
245-
246244
/**
247245
* Invoke the handler method with the given argument values.
248246
*/

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -87,6 +87,8 @@ public class InvocableHandlerMethod extends HandlerMethod {
8787
@Nullable
8888
private MethodValidator methodValidator;
8989

90+
private Class<?>[] validationGroups = EMPTY_GROUPS;
91+
9092

9193
/**
9294
* Create an instance from a {@code HandlerMethod}.
@@ -151,6 +153,8 @@ public void setReactiveAdapterRegistry(ReactiveAdapterRegistry registry) {
151153
*/
152154
public void setMethodValidator(@Nullable MethodValidator methodValidator) {
153155
this.methodValidator = methodValidator;
156+
this.validationGroups = (methodValidator != null ?
157+
methodValidator.determineValidationGroups(getBean(), getBridgedMethod()) : EMPTY_GROUPS);
154158
}
155159

156160

@@ -166,10 +170,9 @@ public Mono<HandlerResult> invoke(
166170
ServerWebExchange exchange, BindingContext bindingContext, Object... providedArgs) {
167171

168172
return getMethodArgumentValues(exchange, bindingContext, providedArgs).flatMap(args -> {
169-
Class<?>[] groups = getValidationGroups();
170173
if (shouldValidateArguments() && this.methodValidator != null) {
171174
this.methodValidator.applyArgumentValidation(
172-
getBean(), getBridgedMethod(), getMethodParameters(), args, groups);
175+
getBean(), getBridgedMethod(), getMethodParameters(), args, this.validationGroups);
173176
}
174177
Object value;
175178
Method method = getBridgedMethod();
@@ -262,11 +265,6 @@ private void logArgumentErrorIfNecessary(ServerWebExchange exchange, MethodParam
262265
}
263266
}
264267

265-
private Class<?>[] getValidationGroups() {
266-
return ((shouldValidateArguments() || shouldValidateReturnValue()) && this.methodValidator != null ?
267-
this.methodValidator.determineValidationGroups(getBean(), getBridgedMethod()) : EMPTY_GROUPS);
268-
}
269-
270268
private static boolean isAsyncVoidReturnType(MethodParameter returnType, @Nullable ReactiveAdapter adapter) {
271269
if (adapter != null && adapter.supportsEmpty()) {
272270
if (adapter.isNoValue()) {

0 commit comments

Comments
 (0)