Repository: dag-hammarskjold-library/dlx

Issue:
Double quotes in CSV serialization not properly escaped
Bug here https://github.com/dag-hammarskjold-library/dlx/blob/917d2361e5f6c11a0a4a70917fb13d07d4fec1e9/dlx/util.py#L120

Hints from issue comments:

Solution patch:
diff --git a/dlx/util.py b/dlx/util.py
index 940bc291..37de9789 100644
--- a/dlx/util.py
+++ b/dlx/util.py
@@ -114,10 +114,10 @@ def serialize(self, *, separator):
         
             for field in self.header:
                 if value := record.get(field):
-                    if separator in value or value[0] == '"' or value[-1] == '"':
+                    if separator in value or '"' in value: # value[0] == '"' or value[-1] == '"':
                         # handle the separator or if the value starts or ends with double quote per CSV specification
                         # https://www.ietf.org/rfc/rfc4180.txt
-                        value.replace('"', '""')
+                        value = value.replace('"', '""')
                         value = f'"{value}"'
                     
                     row.append(value)


Test patch:
diff --git a/tests/test_marcset.py b/tests/test_marcset.py
index 1c31c7da..21bac695 100644
--- a/tests/test_marcset.py
+++ b/tests/test_marcset.py
@@ -170,6 +170,13 @@ def test_to_csv(db):
     #[bib.set('001', None, str(bib.id)) for bib in bibset.records]
     assert bibset.to_csv(write_id=True) == '1.001,1.245$a,1.245$b,1.245$c,1.520$a,2.520$a,1.650$a,1.710$a\n1,This,is the,title,Description,Repeated subfield,Header,Another header\n2,Another,is the,title,,,Header,'
 
+    # comma and quote handling
+    bibs = BibSet()
+    bibs.records += [Bib().set('245', 'a', 'A title, with a comma').set('245', 'b', 'subtitle'), Bib().set('245', 'a', 'A "title, or name" with double quotes in the middle').set('245', 'b', 'subtitle')]
+    print(bibs.to_csv(write_id=False))
+
+    assert bibs.to_csv(write_id=False) == '1.245$a,1.245$b\n"A title, with a comma",subtitle\n"A ""title, or name"" with double quotes in the middle",subtitle'
+
 def test_from_aggregation(db, bibs):
     from dlx.marc import BibSet, Query