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

Fix 51 校验子类时会忽略掉父类的字段 #52

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions document/web-docs/docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Latest Version:
<dependency>
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-javax</artifactId>
<version>0.4.0-beta</version>
<version>0.4.1-beta</version>
</dependency>
```

Expand All @@ -25,7 +25,7 @@ Latest Version:
<dependency>
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-jakarta</artifactId>
<version>0.4.0-beta</version>
<version>0.4.1-beta</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion document/web-docs/docs/guide/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Latest Version:
<dependency>
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-jakarta</artifactId>
<version>0.4.0-beta</version>
<version>0.4.1-beta</version>
</dependency>
</dependencys>
```
Expand Down
2 changes: 1 addition & 1 deletion document/web-docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spel-validator",
"version": "0.4.0-beta",
"version": "0.4.1-beta",
"description": "一个强大的 Java 参数校验包,基于 SpEL 实现,扩展自 jakarta.validation-api 包,几乎支持所有场景下的参数校验。",
"license": "Apache-2.0",
"type": "module",
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>cn.sticki</groupId>
<artifactId>spel-validator-root</artifactId>
<version>0.4.0-beta</version>
<version>0.4.1-beta</version>
<packaging>pom</packaging>

<modules>
Expand Down Expand Up @@ -65,7 +65,7 @@
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<spel-validator.version>0.4.0-beta</spel-validator.version>
<spel-validator.version>0.4.1-beta</spel-validator.version>
<spring.version>5.3.31</spring.version>
<javax-el.version>3.0.0</javax-el.version>
<jakarta-el.version>4.0.2</jakarta-el.version>
Expand Down
2 changes: 1 addition & 1 deletion spel-validator-constrain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-root</artifactId>
<version>0.4.0-beta</version>
<version>0.4.1-beta</version>
</parent>

<artifactId>spel-validator-constrain</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spel-validator-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-root</artifactId>
<version>0.4.0-beta</version>
<version>0.4.1-beta</version>
</parent>

<artifactId>spel-validator-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,18 @@ public static ObjectValidResult validateObject(@NotNull Object verifiedObject, @
*/
@NotNull
private static List<Field> getSpelConstraintFields(@NotNull Class<?> clazz) {
// 获取类的字段
return FIELD_CACHE.computeIfAbsent(clazz, aClass -> {
List<Field> list = new ArrayList<>();
Field[] fields = aClass.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (!getSpelConstraintAnnotations(field).isEmpty()) {
list.add(field);
while (aClass != null) {
Field[] fields = aClass.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (!getSpelConstraintAnnotations(field).isEmpty()) {
list.add(field);
}
}
// 获取父类,继续处理
aClass = aClass.getSuperclass();
}
return Collections.unmodifiableList(list);
});
Expand Down
2 changes: 1 addition & 1 deletion spel-validator-jakarta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-root</artifactId>
<version>0.4.0-beta</version>
<version>0.4.1-beta</version>
</parent>

<artifactId>spel-validator-jakarta</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.sticki.spel.validator.jakarta;

import cn.sticki.spel.validator.jakarta.bean.ExampleTestBean;
import cn.sticki.spel.validator.jakarta.bean.ParentClassTestBean;
import cn.sticki.spel.validator.jakarta.bean.SpelValidTestBean;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -32,4 +33,9 @@ void testSpelValid() {
Assertions.assertTrue(verified);
}

@Test
void testParentClass() {
boolean verified = JakartaSpelValidator.check(ParentClassTestBean.paramTestCase());
Assertions.assertTrue(verified);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cn.sticki.spel.validator.jakarta.bean;

import cn.sticki.spel.validator.constrain.SpelNotNull;
import cn.sticki.spel.validator.jakarta.SpelValid;
import cn.sticki.spel.validator.test.util.ID;
import cn.sticki.spel.validator.test.util.VerifyFailedField;
import cn.sticki.spel.validator.test.util.VerifyObject;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
* 测试继承类
*
* @author 阿杆
* @since 2025/1/23
*/
public class ParentClassTestBean {

@Data
@SpelValid
public static class Parent implements ID {

private int id;

@SpelNotNull(message = "parentField不能为空")
private String parentField;

}

@Data
@SpelValid
public static class Child extends Parent implements ID {

@SpelNotNull(message = "childField不能为空")
private String childField;

}

public static List<VerifyObject> paramTestCase() {
ArrayList<VerifyObject> result = new ArrayList<>();

// 父类测试
Parent parent1 = new Parent();
parent1.setId(1);
parent1.setParentField(null);
result.add(VerifyObject.of(parent1, VerifyFailedField.of(Parent::getParentField)));

Parent parent2 = new Parent();
parent2.setId(2);
parent2.setParentField("123");
result.add(VerifyObject.of(parent2));

// 子类测试
Child child1 = new Child();
child1.setId(1);
child1.setParentField(null);
child1.setChildField(null);
result.add(VerifyObject.of(child1, VerifyFailedField.of(Child::getParentField, Child::getChildField)));

Child child2 = new Child();
child2.setId(2);
child2.setParentField("123");
child2.setChildField("123");
result.add(VerifyObject.of(child2));

return result;
}

}
2 changes: 1 addition & 1 deletion spel-validator-javax/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-root</artifactId>
<version>0.4.0-beta</version>
<version>0.4.1-beta</version>
</parent>

<artifactId>spel-validator-javax</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spel-validator-test-report/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-root</artifactId>
<version>0.4.0-beta</version>
<version>0.4.1-beta</version>
</parent>

<artifactId>spel-validator-test-report</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spel-validator-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-root</artifactId>
<version>0.4.0-beta</version>
<version>0.4.1-beta</version>
</parent>

<artifactId>spel-validator-test</artifactId>
Expand Down
Loading