Skip to content

Commit 222ebd2

Browse files
Copilotabel533
andcommitted
Fix safeDelete/safeUpdate to validate criteria properly
- Modified OGNL.exampleHasAtLeastOneCriteriaCheck() to check criteria.isValid() - Added unit tests for the OGNL validation method - Fixes issue where invalid criteria (null values or empty) would bypass safe checks Co-authored-by: abel533 <1787798+abel533@users.noreply.github.com>
1 parent 936857a commit 222ebd2

2 files changed

Lines changed: 125 additions & 2 deletions

File tree

core/src/main/java/tk/mybatis/mapper/util/OGNL.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,25 @@ public static boolean exampleHasAtLeastOneCriteriaCheck(Object parameter) {
116116
if (parameter instanceof Example) {
117117
List<Example.Criteria> criteriaList = ((Example) parameter).getOredCriteria();
118118
if (criteriaList != null && criteriaList.size() > 0) {
119-
return true;
119+
for (Example.Criteria criteria : criteriaList) {
120+
if (criteria.isValid()) {
121+
return true;
122+
}
123+
}
120124
}
121125
} else {
122126
Method getter = parameter.getClass().getDeclaredMethod("getOredCriteria");
123127
Object list = getter.invoke(parameter);
124128
if (list != null && list instanceof List && ((List) list).size() > 0) {
125-
return true;
129+
Method isValidGetter = null;
130+
for (Object criteria : (List) list) {
131+
if (isValidGetter == null) {
132+
isValidGetter = criteria.getClass().getDeclaredMethod("isValid");
133+
}
134+
if ((Boolean) isValidGetter.invoke(criteria)) {
135+
return true;
136+
}
137+
}
126138
}
127139
}
128140
} catch (Exception e) {
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2017 abel533@gmail.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package tk.mybatis.mapper.test.util;
26+
27+
import org.junit.Test;
28+
import tk.mybatis.mapper.MapperException;
29+
import tk.mybatis.mapper.entity.Example;
30+
import tk.mybatis.mapper.util.OGNL;
31+
32+
import jakarta.persistence.Table;
33+
34+
/**
35+
* Test OGNL.exampleHasAtLeastOneCriteriaCheck method
36+
* @author liuzh
37+
*/
38+
public class TestOGNLCriteriaCheck {
39+
40+
// Simple test entity class with table annotation
41+
@Table(name = "test_entity")
42+
static class TestEntity {
43+
private Integer id;
44+
private String name;
45+
46+
public Integer getId() {
47+
return id;
48+
}
49+
50+
public void setId(Integer id) {
51+
this.id = id;
52+
}
53+
54+
public String getName() {
55+
return name;
56+
}
57+
58+
public void setName(String name) {
59+
this.name = name;
60+
}
61+
}
62+
63+
/**
64+
* Test with null example - should throw MapperException
65+
*/
66+
@Test(expected = MapperException.class)
67+
public void testNullExample() {
68+
OGNL.exampleHasAtLeastOneCriteriaCheck(null);
69+
}
70+
71+
/**
72+
* Test with empty criteria list - should throw MapperException
73+
*/
74+
@Test(expected = MapperException.class)
75+
public void testEmptyCriteriaList() {
76+
Example example = new Example(TestEntity.class);
77+
OGNL.exampleHasAtLeastOneCriteriaCheck(example);
78+
}
79+
80+
/**
81+
* Test with criteria that has no conditions (invalid) - should throw MapperException
82+
*/
83+
@Test(expected = MapperException.class)
84+
public void testInvalidCriteria() {
85+
Example example = new Example(TestEntity.class);
86+
example.createCriteria(); // Create criteria but don't add any conditions
87+
OGNL.exampleHasAtLeastOneCriteriaCheck(example);
88+
}
89+
90+
/**
91+
* Test with criteria that has null value (invalid) - should throw MapperException
92+
*/
93+
@Test(expected = MapperException.class)
94+
public void testCriteriaWithNullValue() {
95+
Example example = new Example(TestEntity.class);
96+
// When notNull=false (default), null values are ignored and no criteria is added
97+
example.createCriteria().andEqualTo("name", null);
98+
OGNL.exampleHasAtLeastOneCriteriaCheck(example);
99+
}
100+
101+
/**
102+
* Test with multiple criteria where all are invalid - should throw MapperException
103+
*/
104+
@Test(expected = MapperException.class)
105+
public void testMultipleInvalidCriteria() {
106+
Example example = new Example(TestEntity.class);
107+
example.createCriteria().andEqualTo("name", null);
108+
example.or().andEqualTo("name", null);
109+
OGNL.exampleHasAtLeastOneCriteriaCheck(example);
110+
}
111+
}

0 commit comments

Comments
 (0)