Skip to content

Commit 5770cc0

Browse files
authored
Merge pull request #412 from ikfid/mongo_db_csv_empty_first_column
MongoDB CSV empty first column parsing fix
2 parents 2abebaa + df7c340 commit 5770cc0

2 files changed

Lines changed: 47 additions & 12 deletions

File tree

src/main/java/org/apache/commons/csv/Lexer.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -334,18 +334,7 @@ private Token parseEncapsulatedToken(final Token token) throws IOException {
334334
while (true) {
335335
c = reader.read();
336336

337-
if (isEscape(c)) {
338-
if (isEscapeDelimiter()) {
339-
token.content.append(delimiter);
340-
} else {
341-
final int unescaped = readEscape();
342-
if (unescaped == EOF) { // unexpected char after escape
343-
token.content.append((char) c).append((char) reader.getLastChar());
344-
} else {
345-
token.content.append((char) unescaped);
346-
}
347-
}
348-
} else if (isQuoteChar(c)) {
337+
if (isQuoteChar(c)) {
349338
if (isQuoteChar(reader.lookAhead())) {
350339
// double or escaped encapsulator -> add single encapsulator to token
351340
c = reader.read();
@@ -376,6 +365,17 @@ private Token parseEncapsulatedToken(final Token token) throws IOException {
376365
}
377366
}
378367
}
368+
} else if (isEscape(c)) {
369+
if (isEscapeDelimiter()) {
370+
token.content.append(delimiter);
371+
} else {
372+
final int unescaped = readEscape();
373+
if (unescaped == EOF) { // unexpected char after escape
374+
token.content.append((char) c).append((char) reader.getLastChar());
375+
} else {
376+
token.content.append((char) unescaped);
377+
}
378+
}
379379
} else if (isEndOfFile(c)) {
380380
if (lenientEof) {
381381
token.type = Token.Type.EOF;

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,41 @@ public void testTrim() throws Exception {
15621562
assertEquals(3, record.size());
15631563
}}
15641564

1565+
@Test
1566+
public void testParsingPrintedEmptyFirstColumn() throws Exception {
1567+
String[][] lines = new String[][] {
1568+
{"a", "b"},
1569+
{"", "x"}
1570+
};
1571+
Exception firstException = null;
1572+
for (CSVFormat.Predefined format : CSVFormat.Predefined.values()) {
1573+
try {
1574+
StringWriter buf = new StringWriter();
1575+
try (CSVPrinter printer = new CSVPrinter(buf, format.getFormat())) {
1576+
for (String[] line : lines) {
1577+
printer.printRecord((Object[]) line);
1578+
}
1579+
}
1580+
try (CSVParser csvRecords = new CSVParser(new StringReader(buf.toString()), format.getFormat())) {
1581+
for (String[] line : lines) {
1582+
assertArrayEquals(line, csvRecords.nextRecord().values());
1583+
}
1584+
assertNull(csvRecords.nextRecord());
1585+
}
1586+
} catch (Exception | Error e) {
1587+
Exception detailedException = new RuntimeException("format: " + format, e);
1588+
if (firstException == null) {
1589+
firstException = detailedException;
1590+
} else {
1591+
firstException.addSuppressed(detailedException);
1592+
}
1593+
}
1594+
}
1595+
1596+
if (firstException != null)
1597+
throw firstException;
1598+
}
1599+
15651600
private void validateLineNumbers(final String lineSeparator) throws IOException {
15661601
try (final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) {
15671602
assertEquals(0, parser.getCurrentLineNumber());

0 commit comments

Comments
 (0)