Skip to content

Commit

Permalink
[SGrPWNEf] Fixes backslashes parsing in csv import / export (#3898)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncordon authored Jan 17, 2024
1 parent 2510425 commit 15590ef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/main/java/apoc/export/csv/CsvEntityLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.RFC4180ParserBuilder;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -99,7 +100,7 @@ public void loadNodes(
final Map<String, Mapping> mapping = getMapping(fields);

final CSVReader csv = new CSVReaderBuilder(reader)
.withCSVParser(new CSVParserBuilder()
.withCSVParser(new RFC4180ParserBuilder()
.withSeparator(clc.getDelimiter())
.withQuoteChar(clc.getQuotationCharacter())
.build())
Expand Down
30 changes: 30 additions & 0 deletions core/src/test/java/apoc/export/csv/ExportCsvTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -244,6 +245,35 @@ public void testCsvRoundTrip() {
db.executeTransactionally(deleteQuery);
}

@Test
public void testCsvBackslashes() {
db.executeTransactionally("CREATE (n:Test {name: 'Test', value: '{\"new\":\"4\\'10\\\\\\\"\"}'})");

String fileName = "test.csv.quotes.csv";
final Map<String, Object> params =
map("file", fileName, "query", "MATCH (n: Test) RETURN n", "config", map("quotes", "always"));

TestUtil.testCall(
db, "CALL apoc.export.csv.all($file, $config)", params, (r) -> assertEquals(fileName, r.get("file")));

final String deleteQuery = "MATCH (n:Test) DETACH DELETE n";
db.executeTransactionally(deleteQuery);

TestUtil.testCall(
db,
"CALL apoc.import.csv([{fileName: $file, labels: ['Test']}],[],{})",
params,
r -> assertEquals(9L, r.get("nodes")));

TestUtil.testResult(db, "MATCH (n:Test) RETURN n.name as name, n.value as value", r -> {
var nodes = r.stream().filter(node -> node.get("name").equals("Test"));
var actual = nodes.map(node -> (String) node.get("value")).collect(Collectors.toSet());
assertEquals(Set.of("{\"new\":\"4'10\\\"\"}"), actual);
});

db.executeTransactionally(deleteQuery);
}

@Test
public void testExportAllCsv() {
String fileName = "all.csv";
Expand Down

0 comments on commit 15590ef

Please sign in to comment.