Skip to content

Commit 662182b

Browse files
authored
Fix 51 校验子类时会忽略掉父类的字段 (#52)
2 parents 6e24a6f + f333b03 commit 662182b

File tree

13 files changed

+98
-18
lines changed

13 files changed

+98
-18
lines changed

document/web-docs/docs/guide/getting-started.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Latest Version:
1515
<dependency>
1616
<groupId>cn.sticki</groupId>
1717
<artifactId>spel-validator-javax</artifactId>
18-
<version>0.4.0-beta</version>
18+
<version>0.4.1-beta</version>
1919
</dependency>
2020
```
2121

@@ -25,7 +25,7 @@ Latest Version:
2525
<dependency>
2626
<groupId>cn.sticki</groupId>
2727
<artifactId>spel-validator-jakarta</artifactId>
28-
<version>0.4.0-beta</version>
28+
<version>0.4.1-beta</version>
2929
</dependency>
3030
```
3131

document/web-docs/docs/guide/user-guide.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Latest Version:
3131
<dependency>
3232
<groupId>cn.sticki</groupId>
3333
<artifactId>spel-validator-jakarta</artifactId>
34-
<version>0.4.0-beta</version>
34+
<version>0.4.1-beta</version>
3535
</dependency>
3636
</dependencys>
3737
```

document/web-docs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spel-validator",
3-
"version": "0.4.0-beta",
3+
"version": "0.4.1-beta",
44
"description": "一个强大的 Java 参数校验包,基于 SpEL 实现,扩展自 jakarta.validation-api 包,几乎支持所有场景下的参数校验。",
55
"license": "Apache-2.0",
66
"type": "module",

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<groupId>cn.sticki</groupId>
99
<artifactId>spel-validator-root</artifactId>
10-
<version>0.4.0-beta</version>
10+
<version>0.4.1-beta</version>
1111
<packaging>pom</packaging>
1212

1313
<modules>
@@ -65,7 +65,7 @@
6565
<maven.compiler.target>8</maven.compiler.target>
6666
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
6767

68-
<spel-validator.version>0.4.0-beta</spel-validator.version>
68+
<spel-validator.version>0.4.1-beta</spel-validator.version>
6969
<spring.version>5.3.31</spring.version>
7070
<javax-el.version>3.0.0</javax-el.version>
7171
<jakarta-el.version>4.0.2</jakarta-el.version>

spel-validator-constrain/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>cn.sticki</groupId>
88
<artifactId>spel-validator-root</artifactId>
9-
<version>0.4.0-beta</version>
9+
<version>0.4.1-beta</version>
1010
</parent>
1111

1212
<artifactId>spel-validator-constrain</artifactId>

spel-validator-core/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>cn.sticki</groupId>
88
<artifactId>spel-validator-root</artifactId>
9-
<version>0.4.0-beta</version>
9+
<version>0.4.1-beta</version>
1010
</parent>
1111

1212
<artifactId>spel-validator-core</artifactId>

spel-validator-core/src/main/java/cn/sticki/spel/validator/core/SpelValidExecutor.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,18 @@ public static ObjectValidResult validateObject(@NotNull Object verifiedObject, @
127127
*/
128128
@NotNull
129129
private static List<Field> getSpelConstraintFields(@NotNull Class<?> clazz) {
130-
// 获取类的字段
131130
return FIELD_CACHE.computeIfAbsent(clazz, aClass -> {
132131
List<Field> list = new ArrayList<>();
133-
Field[] fields = aClass.getDeclaredFields();
134-
for (Field field : fields) {
135-
field.setAccessible(true);
136-
if (!getSpelConstraintAnnotations(field).isEmpty()) {
137-
list.add(field);
132+
while (aClass != null) {
133+
Field[] fields = aClass.getDeclaredFields();
134+
for (Field field : fields) {
135+
field.setAccessible(true);
136+
if (!getSpelConstraintAnnotations(field).isEmpty()) {
137+
list.add(field);
138+
}
138139
}
140+
// 获取父类,继续处理
141+
aClass = aClass.getSuperclass();
139142
}
140143
return Collections.unmodifiableList(list);
141144
});

spel-validator-jakarta/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>cn.sticki</groupId>
88
<artifactId>spel-validator-root</artifactId>
9-
<version>0.4.0-beta</version>
9+
<version>0.4.1-beta</version>
1010
</parent>
1111

1212
<artifactId>spel-validator-jakarta</artifactId>

spel-validator-jakarta/src/test/java/cn/sticki/spel/validator/jakarta/ConstrainTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.sticki.spel.validator.jakarta;
22

33
import cn.sticki.spel.validator.jakarta.bean.ExampleTestBean;
4+
import cn.sticki.spel.validator.jakarta.bean.ParentClassTestBean;
45
import cn.sticki.spel.validator.jakarta.bean.SpelValidTestBean;
56
import org.junit.jupiter.api.Assertions;
67
import org.junit.jupiter.api.Test;
@@ -32,4 +33,9 @@ void testSpelValid() {
3233
Assertions.assertTrue(verified);
3334
}
3435

36+
@Test
37+
void testParentClass() {
38+
boolean verified = JakartaSpelValidator.check(ParentClassTestBean.paramTestCase());
39+
Assertions.assertTrue(verified);
40+
}
3541
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cn.sticki.spel.validator.jakarta.bean;
2+
3+
import cn.sticki.spel.validator.constrain.SpelNotNull;
4+
import cn.sticki.spel.validator.jakarta.SpelValid;
5+
import cn.sticki.spel.validator.test.util.ID;
6+
import cn.sticki.spel.validator.test.util.VerifyFailedField;
7+
import cn.sticki.spel.validator.test.util.VerifyObject;
8+
import lombok.Data;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
/**
14+
* 测试继承类
15+
*
16+
* @author 阿杆
17+
* @since 2025/1/23
18+
*/
19+
public class ParentClassTestBean {
20+
21+
@Data
22+
@SpelValid
23+
public static class Parent implements ID {
24+
25+
private int id;
26+
27+
@SpelNotNull(message = "parentField不能为空")
28+
private String parentField;
29+
30+
}
31+
32+
@Data
33+
@SpelValid
34+
public static class Child extends Parent implements ID {
35+
36+
@SpelNotNull(message = "childField不能为空")
37+
private String childField;
38+
39+
}
40+
41+
public static List<VerifyObject> paramTestCase() {
42+
ArrayList<VerifyObject> result = new ArrayList<>();
43+
44+
// 父类测试
45+
Parent parent1 = new Parent();
46+
parent1.setId(1);
47+
parent1.setParentField(null);
48+
result.add(VerifyObject.of(parent1, VerifyFailedField.of(Parent::getParentField)));
49+
50+
Parent parent2 = new Parent();
51+
parent2.setId(2);
52+
parent2.setParentField("123");
53+
result.add(VerifyObject.of(parent2));
54+
55+
// 子类测试
56+
Child child1 = new Child();
57+
child1.setId(1);
58+
child1.setParentField(null);
59+
child1.setChildField(null);
60+
result.add(VerifyObject.of(child1, VerifyFailedField.of(Child::getParentField, Child::getChildField)));
61+
62+
Child child2 = new Child();
63+
child2.setId(2);
64+
child2.setParentField("123");
65+
child2.setChildField("123");
66+
result.add(VerifyObject.of(child2));
67+
68+
return result;
69+
}
70+
71+
}

spel-validator-javax/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>cn.sticki</groupId>
88
<artifactId>spel-validator-root</artifactId>
9-
<version>0.4.0-beta</version>
9+
<version>0.4.1-beta</version>
1010
</parent>
1111

1212
<artifactId>spel-validator-javax</artifactId>

spel-validator-test-report/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>cn.sticki</groupId>
88
<artifactId>spel-validator-root</artifactId>
9-
<version>0.4.0-beta</version>
9+
<version>0.4.1-beta</version>
1010
</parent>
1111

1212
<artifactId>spel-validator-test-report</artifactId>

spel-validator-test/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>cn.sticki</groupId>
88
<artifactId>spel-validator-root</artifactId>
9-
<version>0.4.0-beta</version>
9+
<version>0.4.1-beta</version>
1010
</parent>
1111

1212
<artifactId>spel-validator-test</artifactId>

0 commit comments

Comments
 (0)