Skip to content

Commit

Permalink
Predetermine validation groups on initialization
Browse files Browse the repository at this point in the history
Closes gh-32068
  • Loading branch information
jhoeller committed Jan 23, 2024
1 parent 5656eac commit 5faace0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -149,6 +151,8 @@ public void setDataBinderFactory(WebDataBinderFactory dataBinderFactory) {
*/
public void setMethodValidator(@Nullable MethodValidator methodValidator) {
this.methodValidator = methodValidator;
this.validationGroups = (methodValidator != null ?
methodValidator.determineValidationGroups(getBean(), getBridgedMethod()) : EMPTY_GROUPS);
}


Expand Down Expand Up @@ -180,17 +184,16 @@ public Object invokeForRequest(NativeWebRequest request, @Nullable ModelAndViewC
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;
Expand Down Expand Up @@ -238,11 +241,6 @@ protected Object[] getMethodArgumentValues(NativeWebRequest request, @Nullable M
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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -151,6 +153,8 @@ public void setReactiveAdapterRegistry(ReactiveAdapterRegistry registry) {
*/
public void setMethodValidator(@Nullable MethodValidator methodValidator) {
this.methodValidator = methodValidator;
this.validationGroups = (methodValidator != null ?
methodValidator.determineValidationGroups(getBean(), getBridgedMethod()) : EMPTY_GROUPS);
}


Expand All @@ -166,10 +170,9 @@ public Mono<HandlerResult> invoke(
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();
Expand Down Expand Up @@ -262,11 +265,6 @@ private void logArgumentErrorIfNecessary(ServerWebExchange exchange, MethodParam
}
}

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()) {
Expand Down

0 comments on commit 5faace0

Please sign in to comment.