From 5faace0eb341032a1c25c1da1f714da1abc44f01 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 23 Jan 2024 11:15:12 +0100 Subject: [PATCH] Predetermine validation groups on initialization Closes gh-32068 --- .../method/support/InvocableHandlerMethod.java | 16 +++++++--------- .../result/method/InvocableHandlerMethod.java | 14 ++++++-------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java b/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java index 1ed1a131bf38..11f0fcb49066 100644 --- a/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java +++ b/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java @@ -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 void setDataBinderFactory(WebDataBinderFactory dataBinderFactory) { */ public void setMethodValidator(@Nullable MethodValidator methodValidator) { this.methodValidator = methodValidator; + this.validationGroups = (methodValidator != null ? + methodValidator.determineValidationGroups(getBean(), getBridgedMethod()) : EMPTY_GROUPS); } @@ -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; @@ -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. */ diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java index 5df721559f1f..88edf33d36a4 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java @@ -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 void setReactiveAdapterRegistry(ReactiveAdapterRegistry registry) { */ public void setMethodValidator(@Nullable MethodValidator methodValidator) { this.methodValidator = methodValidator; + this.validationGroups = (methodValidator != null ? + methodValidator.determineValidationGroups(getBean(), getBridgedMethod()) : EMPTY_GROUPS); } @@ -166,10 +170,9 @@ public Mono 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(); @@ -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()) {