Skip to content

Commit

Permalink
Removed unescaping of double quotes in CSV export & added tests in pr…
Browse files Browse the repository at this point in the history
…inter to ensure proper escape of multiple adjacent double quotes
  • Loading branch information
xiazcy committed Nov 18, 2022
1 parent 3960457 commit 32677c4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,7 @@ public static String escapeSeparators(Object value, String separator) {
}

public static String escapeDoubleQuotes(Object value) {
String temp = value.toString().replace("\"\"", "\"");
return temp.replace("\"", "\"\"");
return value.toString().replace("\"", "\"\"");
}

public String typeDescription() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@
import com.amazonaws.services.neptune.propertygraph.schema.DataType;
import com.amazonaws.services.neptune.propertygraph.schema.LabelSchema;
import com.amazonaws.services.neptune.propertygraph.schema.PropertySchema;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.junit.Test;

import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -112,4 +118,58 @@ public void shouldUseEmptySeparatorToSeparateMultipleValues() throws Exception {
stringWriter.toString());
}

@Test
public void shouldEscapeDoubleQuotesProperlyAfterPrintPropertiesToCSVAndRewrite() throws Exception {

String original2Quotes = "{\"hobby\" : \"watching \"Flash\"\"}";
String original3Quotes = "{\"hobby\" : \"watching \"The \"Flash\"\"\"}";

StringWriter stringWriter = new StringWriter();

PropertySchema propertySchema1 = new PropertySchema("property1", false, DataType.String, false);
PropertySchema propertySchema2 = new PropertySchema("property2", false, DataType.String, false);

LabelSchema labelSchema = new LabelSchema(new Label("Entity"));
labelSchema.put("property1", propertySchema1);
labelSchema.put("property2", propertySchema2);

HashMap<String, List<String>> props = new HashMap<String, List<String>>() {{
put("property1", Collections.singletonList(original2Quotes));
put("property2", Collections.singletonList(original3Quotes));
}};

CsvPropertyGraphPrinter printer = new CsvPropertyGraphPrinter(
new PrintOutputWriter("outputId", stringWriter),
labelSchema,
new PrinterOptions(CsvPrinterOptions.builder().build()));

printer.printProperties(props);

// all double quotes should be escaped when printer prints
assertEquals("\"{\"\"hobby\"\" : \"\"watching \"\"Flash\"\"\"\"}\"," +
"\"{\"\"hobby\"\" : \"\"watching \"\"The \"\"Flash\"\"\"\"\"\"}\"",
stringWriter.toString());

// using CSVFormat to read in printed items (same library used by RewriteCSV)
String[] filePropertyHeaders = labelSchema.propertySchemas().stream()
.map(p -> p.property().toString())
.collect(Collectors.toList())
.toArray(new String[]{});
CSVFormat format = CSVFormat.RFC4180.withHeader(filePropertyHeaders);
Reader in = new StringReader(stringWriter.toString());
Iterable<CSVRecord> records = format.parse(in);

for (CSVRecord record : records) {
// what CSVFormat read in from printed CSV should be the original value
assertEquals(original2Quotes, record.get("property1"));
assertEquals(original3Quotes, record.get("property2"));

// double quotes should all be properly escaped again when we format for rewrite
assertEquals("\"{\"\"hobby\"\" : \"\"watching \"\"Flash\"\"\"\"}\"",
DataType.String.format(record.get("property1")));
assertEquals("\"{\"\"hobby\"\" : \"\"watching \"\"The \"\"Flash\"\"\"\"\"\"}\"",
DataType.String.format(record.get("property2")));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ public void shouldEscapeDoubleQuotes() {
}

@Test
public void shouldNotDoubleEscapeDoubleQuotesThatHaveAlreadyBeenEscaped() {
public void shouldEscapeTwoDoubleQuotes() {
String result = DataType.String.format("One \"\"two\"\" three");
assertEquals("\"One \"\"two\"\" three\"", result);
assertEquals("\"One \"\"\"\"two\"\"\"\" three\"", result);
}

@Test
public void shouldEscapeThreeDoubleQuotes() {
String result = DataType.String.format("One \"\"\"two\"\"\" three");
assertEquals("\"One \"\"\"\"\"\"two\"\"\"\"\"\" three\"", result);
}

@Test
Expand Down

0 comments on commit 32677c4

Please sign in to comment.