Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(export): escape backslashes before apostrophes in LPG strings

kartsan033a3d9979c639
diff --git a/semantica/export/lpg_exporter.py b/semantica/export/lpg_exporter.py
index 0a30608a..4252cb63 100644
--- a/semantica/export/lpg_exporter.py
+++ b/semantica/export/lpg_exporter.py
@@ -347,7 +347,7 @@ class LPGExporter:
def _escape_cypher_string(self, value: str) -> str:
"""Escape special characters in Cypher strings."""
- return value.replace("'", "\\'").replace("\\", "\\\\")
+ return value.replace("\\", "\\\\").replace("'", "\\'")
def _format_cypher_value(self, value: Any) -> str:
"""Format a value for Cypher query."""
diff --git a/tests/export/test_lpg_exporter_values.py b/tests/export/test_lpg_exporter_values.py
index 698de31e..1c15fbd7 100644
--- a/tests/export/test_lpg_exporter_values.py
+++ b/tests/export/test_lpg_exporter_values.py
@@ -36,3 +36,21 @@ def test_export_formats_node_values_and_endpoint_ids(tmp_path, batch_size):
"CREATE (a)-[r:RELATED_TO {id: 'r1'}]->(b);",
"MATCH (a {id: 7}), (b {id: '7'}) CREATE (a)-[r:RELATED_TO {id: 2}]->(b);",
]
+
+
+def test_export_escapes_apostrophes_and_backslashes(tmp_path):
+ path = tmp_path / "graph.cypher"
+ LPGExporter(include_indexes=False).export(
+ {
+ "nodes": [{"id": "o'brien", "name": "O'Brien"}, {"id": "acme"}],
+ "edges": [{"source": "o'brien", "target": "acme", "note": "a\\b"}],
+ },
+ path,
+ )
+
+ text = path.read_text(encoding="utf-8")
+ assert "CREATE (n0:Entity {id: 'o\\'brien', name: 'O\\'Brien'});" in text
+ assert (
+ "MATCH (a {id: 'o\\'brien'}), (b {id: 'acme'}) "
+ "CREATE (a)-[r:RELATED_TO {note: 'a\\\\b'}]->(b);"
+ ) in text