Skip to content
Open
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
7 changes: 3 additions & 4 deletions backend/apps/data_training/curd/data_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,10 @@ def to_xml_string(_dict: list[dict] | dict, root: str = 'sql-examples') -> str:
end_index = pretty_xml.find('>') + 1
pretty_xml = pretty_xml[end_index:].lstrip()

# 替换所有 XML 转义字符
# 只还原不会破坏 XML 结构的引号转义。'<'、'>'、'&' 必须保留:
# 术语描述或示例 SQL 里出现 '&' 或 '<' 时,还原成原字符会让整段内容不再是
# 合法 XML,而这段文本要作为模板片段交给模型。
escape_map = {
'&lt;': '<',
'&gt;': '>',
'&amp;': '&',
'&quot;': '"',
'&apos;': "'"
}
Expand Down
7 changes: 3 additions & 4 deletions backend/apps/terminology/curd/terminology.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,11 +926,10 @@ def to_xml_string(_dict: list[dict] | dict, root: str = 'terminologies') -> str:
end_index = pretty_xml.find('>') + 1
pretty_xml = pretty_xml[end_index:].lstrip()

# 替换所有 XML 转义字符
# 只还原不会破坏 XML 结构的引号转义。'&lt;'、'&gt;'、'&amp;' 必须保留:
# 术语描述或示例 SQL 里出现 '&' 或 '<' 时,还原成原字符会让整段内容不再是
# 合法 XML,而这段文本要作为模板片段交给模型。
escape_map = {
'&lt;': '<',
'&gt;': '>',
'&amp;': '&',
'&quot;': '"',
'&apos;': "'"
}
Expand Down
93 changes: 93 additions & 0 deletions backend/tests/test_xml_escaping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""Terminology and example XML must stay well-formed for real text.
Both ``to_xml_string`` helpers build the document with ElementTree and then
restored ``&lt;``/``&gt;``/``&amp;`` to raw characters, so a description such as
``投入 < 10% & 增长 > 5%`` produced output that no longer parses. That string is
inserted into a prompt template, so broken markup reached the model. Only the
quote entities are safe to restore; the structural ones must stay escaped.
Load the functions through AST like the other isolated tests: no driver, ORM or
web stack is imported.
"""

import ast
import xml.etree.ElementTree as ET
from pathlib import Path
from xml.dom.minidom import parseString

import pytest

BACKEND = Path(__file__).resolve().parents[1]


def _load(path: Path, names: set):
tree = ast.parse(path.read_text(encoding="utf-8"))
module = ast.Module(
body=[node for node in tree.body if getattr(node, "name", None) in names],
type_ignores=[],
)
namespace = {"parseString": parseString, "ET": ET}
exec(compile(module, str(path), "exec"), namespace)
return namespace


@pytest.fixture(scope="module")
def xml_helpers():
base = _load(BACKEND / "common/utils/dict_to_xml.py", {"_add_element", "dict_to_xml"})
terminology = _load(
BACKEND / "apps/terminology/curd/terminology.py", {"to_xml_string"}
)
training = _load(
BACKEND / "apps/data_training/curd/data_training.py", {"to_xml_string"}
)
for namespace in (terminology, training):
namespace["dict_to_xml"] = base["dict_to_xml"]
return {
"dict_to_xml": base["dict_to_xml"],
"ET": ET,
"terminology": terminology["to_xml_string"],
"training": training["to_xml_string"],
}


@pytest.mark.parametrize(
"text",
[
"R&D budget",
"投入 < 10% & 增长 > 5%",
"a <b> tag and an & ampersand",
],
)
def test_terminology_text_with_structural_characters_stays_parseable(xml_helpers, text):
output = xml_helpers["terminology"](
{"words": ["GDP"], "description": text}, "terminology"
)

document = parseString(output)
description = document.getElementsByTagName("description")[0].firstChild.data
assert description == text


@pytest.mark.parametrize("text", ["select a < b and c & d", "where x = 'A&B'"])
def test_example_sql_with_structural_characters_stays_parseable(xml_helpers, text):
output = xml_helpers["training"]({"sql-examples": [{"question": "q", "sql": text}]})

parseString(output)


def test_quotes_are_still_restored_for_readability(xml_helpers):
output = xml_helpers["terminology"](
{"words": ["GDP"], "description": 'a "quoted" phrase'}, "terminology"
)

document = parseString(output)
assert document.getElementsByTagName("description")[0].firstChild.data == 'a "quoted" phrase'


def test_plain_text_keeps_the_existing_shape(xml_helpers):
output = xml_helpers["terminology"]({"words": ["GDP", "国内生产总值"], "description": "d"})

document = parseString(output)
assert document.documentElement.tagName == "terminologies"
words = [node.firstChild.data for node in document.getElementsByTagName("word")]
assert words == ["GDP", "国内生产总值"]
Loading