Skip to content

Commit 27ca11c

Browse files
authored
Merge pull request #3349 from nieqiurong/20241229
Ignore empty text node when parsing XML nodes
2 parents 96a0b8f + 5c757a3 commit 27ca11c

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/main/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,6 +80,9 @@ protected MixedSqlNode parseDynamicTags(XNode node) {
8080
XNode child = node.newXNode(children.item(i));
8181
if (child.getNode().getNodeType() == Node.CDATA_SECTION_NODE || child.getNode().getNodeType() == Node.TEXT_NODE) {
8282
String data = child.getStringBody("");
83+
if (data.trim().isEmpty()) {
84+
continue;
85+
}
8386
TextSqlNode textSqlNode = new TextSqlNode(data);
8487
if (textSqlNode.isDynamic()) {
8588
contents.add(textSqlNode);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2009-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.ibatis.scripting.xmltags;
18+
19+
import static org.assertj.core.api.Assertions.*;
20+
import static org.junit.Assert.assertEquals;
21+
22+
import java.lang.reflect.Field;
23+
import java.util.List;
24+
25+
import org.apache.ibatis.mapping.SqlSource;
26+
import org.apache.ibatis.parsing.XPathParser;
27+
import org.apache.ibatis.session.Configuration;
28+
import org.junit.jupiter.api.Test;
29+
30+
class XMLScriptBuilderTest {
31+
32+
@Test
33+
void shouldEmptyTextNodesRemoved() throws Exception {
34+
String xml = """
35+
<script>
36+
select * from user
37+
<if test="_parameter != null">
38+
where id = 1
39+
</if>
40+
<if test="_parameter == null">
41+
where id > 0
42+
</if>
43+
</script>
44+
""";
45+
SqlSource sqlSource = new XMLScriptBuilder(new Configuration(), new XPathParser(xml).evalNode("/script"))
46+
.parseScriptNode();
47+
48+
Field rootSqlNodeFld = DynamicSqlSource.class.getDeclaredField("rootSqlNode");
49+
rootSqlNodeFld.setAccessible(true);
50+
MixedSqlNode sqlNode = (MixedSqlNode) rootSqlNodeFld.get(sqlSource);
51+
52+
Field contentsFld = MixedSqlNode.class.getDeclaredField("contents");
53+
contentsFld.setAccessible(true);
54+
@SuppressWarnings("unchecked")
55+
List<SqlNode> contents = (List<SqlNode>) contentsFld.get(sqlNode);
56+
57+
assertEquals(3, contents.size());
58+
assertThat(contents.get(0)).isInstanceOf(StaticTextSqlNode.class);
59+
assertThat(contents.get(1)).isInstanceOf(IfSqlNode.class);
60+
assertThat(contents.get(2)).isInstanceOf(IfSqlNode.class);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)