Skip to content

Commit c3fca99

Browse files
committed
Unknown element inside <choose> should throw exception
When using XML script in annotation (e.g. `@Select("<script>...</script>")`), the XML parser's validation is disabled. There are other error cases (e.g. invalid attributes) which are undetectable with the current implementation, but it is not our goal to implement full-featured XML validation logic, IMHO.
1 parent 5f8927d commit c3fca99

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ private void handleWhenOtherwiseNodes(XNode chooseSqlNode, List<SqlNode> ifSqlNo
247247
handler.handleNode(child, ifSqlNodes);
248248
} else if (handler instanceof OtherwiseHandler) {
249249
handler.handleNode(child, defaultSqlNodes);
250+
} else {
251+
throw new BuilderException("Unknown element <" + nodeName + "> in SQL statement.");
250252
}
251253
}
252254
}

src/test/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilderTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package org.apache.ibatis.scripting.xmltags;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2021

22+
import org.apache.ibatis.builder.BuilderException;
2123
import org.apache.ibatis.mapping.SqlSource;
2224
import org.apache.ibatis.parsing.XPathParser;
2325
import org.apache.ibatis.session.Configuration;
@@ -42,4 +44,19 @@ void shouldWhereInsertWhitespace() throws Exception {
4244
.containsPattern("(?m)^\\s*select \\* from user\\s+WHERE\\s+id = 1\\s+and id > 0\\s*$");
4345
}
4446

47+
@Test
48+
void shouldThrowIfUnknownElementFound() {
49+
String xml = """
50+
<script>
51+
select * from user
52+
<choose>
53+
<when test="1==1">and id = 1</when>
54+
<otherwize>and id > 0</otherwize>
55+
</choose>
56+
</script>
57+
""";
58+
XMLScriptBuilder parser = new XMLScriptBuilder(new Configuration(), new XPathParser(xml).evalNode("/script"));
59+
assertThatThrownBy(parser::parseScriptNode).isInstanceOf(BuilderException.class)
60+
.hasMessage("Unknown element <otherwize> in SQL statement.");
61+
}
4562
}

0 commit comments

Comments
 (0)