Skip to content

Commit 4b900bd

Browse files
committed
Fix for "The header contains duplicate names: " +
Arrays.toString(header) is thrown when the header column is an empty String.
1 parent 2480dff commit 4b900bd

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
package org.apache.commons.csv;
1919

20-
import static org.apache.commons.csv.Token.Type.TOKEN;
21-
2220
import java.io.Closeable;
2321
import java.io.File;
2422
import java.io.FileReader;
@@ -37,6 +35,8 @@
3735
import java.util.Map;
3836
import java.util.NoSuchElementException;
3937

38+
import static org.apache.commons.csv.Token.Type.TOKEN;
39+
4040
/**
4141
* Parses CSV files according to the specified format.
4242
*
@@ -377,6 +377,10 @@ private Map<String, Integer> initializeHeader() throws IOException {
377377
// build the name to index mappings
378378
if (header != null) {
379379
for (int i = 0; i < header.length; i++) {
380+
// skip the header when it is empty
381+
if(header[i] == null || "".equals(header[i])) {
382+
continue;
383+
}
380384
if (hdrMap.containsKey(header[i])) {
381385
throw new IllegalArgumentException("The header contains duplicate names: " +
382386
Arrays.toString(header));

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,10 @@
1717

1818
package org.apache.commons.csv;
1919

20-
import static org.apache.commons.csv.Constants.CR;
21-
import static org.apache.commons.csv.Constants.CRLF;
22-
import static org.apache.commons.csv.Constants.LF;
23-
import static org.junit.Assert.assertArrayEquals;
24-
import static org.junit.Assert.assertEquals;
25-
import static org.junit.Assert.assertFalse;
26-
import static org.junit.Assert.assertNotNull;
27-
import static org.junit.Assert.assertNull;
28-
import static org.junit.Assert.assertTrue;
29-
import static org.junit.Assert.fail;
20+
import org.apache.commons.io.input.BOMInputStream;
21+
import org.junit.Assert;
22+
import org.junit.Ignore;
23+
import org.junit.Test;
3024

3125
import java.io.File;
3226
import java.io.IOException;
@@ -45,10 +39,8 @@
4539
import java.util.Map;
4640
import java.util.NoSuchElementException;
4741

48-
import org.apache.commons.io.input.BOMInputStream;
49-
import org.junit.Assert;
50-
import org.junit.Ignore;
51-
import org.junit.Test;
42+
import static org.apache.commons.csv.Constants.*;
43+
import static org.junit.Assert.*;
5244

5345
/**
5446
* CSVParserTest
@@ -497,6 +489,11 @@ public void testDuplicateHeaderEntries() throws Exception {
497489
CSVParser.parse("a,b,a\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader(new String[]{}));
498490
}
499491

492+
@Test()
493+
public void testDuplicateHeaderEntriesWithEmptyHeaders() throws Exception {
494+
CSVParser.parse("a,,c,d,,\n1,2,3,4,5,6\nx,y,z,v,u,w", CSVFormat.DEFAULT.withHeader(new String[]{}));
495+
}
496+
500497
@Test
501498
public void testGetLine() throws IOException {
502499
final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));

0 commit comments

Comments
 (0)