Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加MethodValidated注解的测试用例 及对MethodValidated使用场景和用法的说明注释 #784

Merged
merged 4 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class ValidationParameter implements Serializable {

private static final long serialVersionUID = 7158911668568000392L;

@NotNull(groups = ValidationService.Update.class)
private Integer id;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 @NotNull(groups = {ValidationService.Update.class,ValidationService.RelatedQuery.class})
 private Integer id;


@NotNull // 不允许为空
@Size(min = 2, max = 20) // 长度或大小范围
private String name;
Expand All @@ -52,6 +55,14 @@ public class ValidationParameter implements Serializable {
@Future // 必须为一个未来的时间
private Date expiryDate;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.alibaba.dubbo.config.validation;

import com.alibaba.dubbo.validation.MethodValidated;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
Expand All @@ -28,16 +30,31 @@
*/
public interface ValidationService { // 缺省可按服务接口区分验证场景,如:@NotNull(groups = ValidationService.class)

/**
* 没有加上“@MethodValidated(ValidationService.Save.class)”这句代码时,
* 现在的检查逻辑不会去检验groups = ValidationService.Save.class这个分组
*
* @param parameter
*/
@MethodValidated(Save.class)
void save(ValidationParameter parameter);

void update(ValidationParameter parameter);

void delete(@Min(1) long id, @NotNull @Size(min = 2, max = 16) @Pattern(regexp = "^[a-zA-Z]+$") String operator);

/**
* 假设关联查询的时候需要同时传id和email的值。这时需要检查Sava分组和Update分组。
* @param parameter
*/
@MethodValidated({Save.class, Update.class})
void relatedQuery(ValidationParameter parameter);

@interface Save {
} // 与方法同名接口,首字母大写,用于区分验证场景,如:@NotNull(groups = ValidationService.Save.class),可选

@interface Update {
} // 与方法同名接口,首字母大写,用于区分验证场景,如:@NotNull(groups = ValidationService.Update.class),可选

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可以添加

@interface RelatedQuery {}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public void update(ValidationParameter parameter) {
public void delete(long id, String operator) {
}

public void relatedQuery(ValidationParameter parameter){

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,36 @@ public void testValidation() {
Assert.assertNotNull(violations);
}

//检查Save分组 save error
try {
parameter = new ValidationParameter();
parameter.setName("liangfei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.save(parameter);
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
}

// relatedQuery error 不传id和email的值,触发Save和Update的检查异常
try {
parameter = new ValidationParameter();
parameter.setName("liangfei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.relatedQuery(parameter);
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertEquals(violations.size(),2);
}

// Save Error
try {
parameter = new ValidationParameter();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.alibaba.dubbo.validation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 方法分组验证注解
* 使用场景:当调用某个方法时,需要检查多个分组,可以在接口方法上加上该注解
* 用法 在接口方法上增加注解,如:
* @MethodValidated({ValidationService.Save.class, ValidationService.Update.class})
* void relatedQuery(ValidationParameter parameter);
* 表示relatedQuery这个方法需要同时检查Save和Update这两个分组
* @author: zhangyinyue
* @Createdate: 2017年10月10日 16:34
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad java doc, please follow java doc's specification

@Target({ElementType.METHOD})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以考虑支持参数注解

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在方法层面上注解,本来就是作用在参数上的; 这个注解应该是要跟方法相关的吧,本来意义就是不同方法需要检查的参数分组不一样,应该没必要加上参数上面吧。

@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodValidated {
Class<?>[] value() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.utils.ReflectUtils;
import com.alibaba.dubbo.validation.MethodValidated;
import com.alibaba.dubbo.validation.Validator;

import javassist.ClassPool;
Expand Down Expand Up @@ -239,41 +240,55 @@ public void validate(String methodName, Class<?>[] parameterTypes, Object[] argu
}
Set<ConstraintViolation<?>> violations = new HashSet<ConstraintViolation<?>>();
Method method = clazz.getMethod(methodName, parameterTypes);
Class<?>[] methodClasses = null;
if (method.isAnnotationPresent(MethodValidated.class)){
methodClasses = method.getAnnotation(MethodValidated.class).value();
}

Object parameterBean = getMethodParameterBean(clazz, method, arguments);
if (parameterBean != null) {
if (methodClass != null) {
violations.addAll(validator.validate(parameterBean, Default.class, clazz, methodClass));
} else if (methodClasses != null) {
for (int i = 0; i < methodClasses.length; i++) {
violations.addAll(validator.validate(parameterBean, Default.class, clazz, methodClasses[i]));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for循环去掉吧,维护个List<Class<?>> groups,最后groups.toArray(new Class[0])

} else {
violations.addAll(validator.validate(parameterBean, Default.class, clazz));
}
}
for (Object arg : arguments) {
validate(violations, arg, clazz, methodClass);
validate(violations, arg, clazz, methodClass, methodClasses);
}
if (violations.size() > 0) {
logger.error("Failed to validate service: " + clazz.getName() + ", method: " + methodName + ", cause: " + violations);
throw new ConstraintViolationException("Failed to validate service: " + clazz.getName() + ", method: " + methodName + ", cause: " + violations, violations);
}
}

private void validate(Set<ConstraintViolation<?>> violations, Object arg, Class<?> clazz, Class<?> methodClass) {
private void validate(Set<ConstraintViolation<?>> violations, Object arg, Class<?> clazz, Class<?> methodClass, Class<?>[] methodClasses ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可以改成

private void validate(Set<ConstraintViolation<?>> violations, Object arg, Class<?>... groups) 

if (arg != null && !isPrimitives(arg.getClass())) {
if (Object[].class.isInstance(arg)) {
for (Object item : (Object[]) arg) {
validate(violations, item, clazz, methodClass);
validate(violations, item, clazz, methodClass, methodClasses);
}
} else if (Collection.class.isInstance(arg)) {
for (Object item : (Collection<?>) arg) {
validate(violations, item, clazz, methodClass);
validate(violations, item, clazz, methodClass, methodClasses);
}
} else if (Map.class.isInstance(arg)) {
for (Map.Entry<?, ?> entry : ((Map<?, ?>) arg).entrySet()) {
validate(violations, entry.getKey(), clazz, methodClass);
validate(violations, entry.getValue(), clazz, methodClass);
validate(violations, entry.getKey(), clazz, methodClass, methodClasses);
validate(violations, entry.getValue(), clazz, methodClass, methodClasses);
}
} else {
if (methodClass != null) {
violations.addAll(validator.validate(arg, Default.class, clazz, methodClass));
} else {
} else if (methodClasses != null) {
for (int i = 0; i < methodClasses.length; i++) {
violations.addAll(validator.validate(arg, Default.class, clazz, methodClasses[i]));
}
}else {
violations.addAll(validator.validate(arg, Default.class, clazz));
}
}
Expand Down