[3.0] Fix validation exception (#9620)

* [3.0] Fix validation exception

* fix ut
This commit is contained in:
Albumen Kevin 2022-01-26 11:40:42 +08:00 committed by GitHub
parent 2ab9698ce3
commit ccf20fb6de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 15 deletions

View File

@ -40,13 +40,13 @@ import static org.apache.dubbo.common.constants.FilterConstants.VALIDATION_KEY;
* In the above configuration a validation has been configured of type jvalidation. On invocation of method <b>save</b>
* dubbo will invoke {@link org.apache.dubbo.validation.support.jvalidation.JValidator}
* </pre>
*
* <p>
* To add a new type of validation
* <pre>
* e.g. &lt;dubbo:method name="save" validation="special" /&gt;
* where "special" is representing a validator for special character.
* </pre>
*
* <p>
* developer needs to do
* <br/>
* 1)Implement a SpecialValidation.java class (package name xxx.yyy.zzz) either by implementing {@link Validation} or extending {@link org.apache.dubbo.validation.support.AbstractValidation} <br/>
@ -65,6 +65,7 @@ public class ValidationFilter implements Filter {
/**
* Sets the validation instance for ValidationFilter
*
* @param validation Validation instance injected by dubbo framework based on "validation" attribute value.
*/
public void setValidation(Validation validation) {
@ -73,6 +74,7 @@ public class ValidationFilter implements Filter {
/**
* Perform the validation of before invoking the actual method based on <b>validation</b> attribute value.
*
* @param invoker service
* @param invocation invocation.
* @return Method invocation result
@ -81,17 +83,13 @@ public class ValidationFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
if (validation != null && !invocation.getMethodName().startsWith("$")
&& ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), VALIDATION_KEY))) {
&& ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), VALIDATION_KEY))) {
try {
Validator validator = validation.getValidator(invoker.getUrl());
if (validator != null) {
validator.validate(invocation.getMethodName(), invocation.getParameterTypes(), invocation.getArguments());
}
} catch (RpcException e) {
if(e.isValidation()){
return AsyncRpcResult.newDefaultAsyncResult(e, invocation);
}
throw e;
} catch (Throwable t) {
return AsyncRpcResult.newDefaultAsyncResult(t, invocation);

View File

@ -21,7 +21,6 @@ import org.apache.dubbo.common.bytecode.ClassGenerator;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.validation.MethodValidated;
import org.apache.dubbo.validation.Validator;
@ -293,7 +292,7 @@ public class JValidator implements Validator {
}
} catch (ValidationException e) {
// only use exception's message to avoid potential serialization issue
throw new RpcException(RpcException.VALIDATION_EXCEPTION, e.getMessage(), e);
throw new ValidationException(e.getMessage());
}
}

View File

@ -21,7 +21,6 @@ import org.apache.dubbo.common.bytecode.ClassGenerator;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.validation.MethodValidated;
import org.apache.dubbo.validation.Validator;
@ -293,7 +292,7 @@ public class JValidatorNew implements Validator {
}
} catch (ValidationException e) {
// only use exception's message to avoid potential serialization issue
throw new RpcException(RpcException.VALIDATION_EXCEPTION, e.getMessage(), e);
throw new ValidationException(e.getMessage());
}
}

View File

@ -17,12 +17,12 @@
package org.apache.dubbo.validation.support.jvalidation;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.validation.support.jvalidation.mock.ValidationParameter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import javax.validation.ValidationException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@ -47,13 +47,11 @@ public class JValidatorTest {
@Test
public void testItWhenItViolatedConstraint() throws Exception {
RpcException rpcException = Assertions.assertThrows(RpcException.class, () -> {
Assertions.assertThrows(ValidationException.class, () -> {
URL url = URL.valueOf("test://test:11/org.apache.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget");
JValidator jValidator = new JValidator(url);
jValidator.validate("someMethod2", new Class<?>[]{ValidationParameter.class}, new Object[]{new ValidationParameter()});
});
Assertions.assertTrue(rpcException.isValidation());
}
@Test