Skip to content

Commit 35d101c

Browse files
committed
CSV-114 CSVFormat constructor should reject a header array with duplicate entries
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1593076 13f79535-47bb-0310-9956-ffa450edef68
1 parent 692a1e3 commit 35d101c

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The <action> type attribute can be add,update,fix,remove.
4040
<body>
4141

4242
<release version="1.0" date="TBD" description="First release">
43+
<action issue="CSV-114" type="fix" dev="sebb">CSVFormat constructor should reject a header array with duplicate entries</action>
4344
<action issue="CSV-112" type="fix" dev="britter">HeaderMap is inconsistent when it is parsed from an input with duplicate columns names</action>
4445
<action issue="CSV-111" type="fix" dev="ggregory">CSVRecord.toMap() fails if row length shorter than header length</action>
4546
<action issue="CSV-106" type="fix" dev="ggregory">CSVFormat.format allways append null</action>

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public final class CSVFormat implements Serializable {
155155
private final boolean ignoreEmptyLines;
156156
private final String recordSeparator; // for outputs
157157
private final String nullString; // the string to be used for null values
158-
private final String[] header;
158+
private final String[] header; // array of header column names
159159
private final boolean skipHeaderRecord;
160160

161161
/**
@@ -310,7 +310,17 @@ private CSVFormat(final char delimiter, final Character quoteChar,
310310
this.ignoreEmptyLines = ignoreEmptyLines;
311311
this.recordSeparator = recordSeparator;
312312
this.nullString = nullString;
313-
this.header = header == null ? null : header.clone();
313+
if (header == null) {
314+
this.header = null;
315+
} else {
316+
Set<String> dupCheck = new HashSet<String>();
317+
for(String hdr : header) {
318+
if (!dupCheck.add(hdr)) {
319+
throw new IllegalArgumentException("The header contains a duplicate entry: '" + hdr + "' in " + Arrays.toString(header));
320+
}
321+
}
322+
this.header = header.clone();
323+
}
314324
this.skipHeaderRecord = skipHeaderRecord;
315325
}
316326

@@ -658,13 +668,6 @@ void validate() throws IllegalStateException {
658668
throw new IllegalStateException("No quotes mode set but no escape character is set");
659669
}
660670

661-
if (header != null) {
662-
final Set<String> set = new HashSet<String>(header.length);
663-
set.addAll(Arrays.asList(header));
664-
if (set.size() != header.length) {
665-
throw new IllegalStateException("The header contains duplicate names: " + Arrays.toString(header));
666-
}
667-
}
668671
}
669672

670673
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void testDelimiterSameAsEscapeThrowsException() {
6161
CSVFormat.DEFAULT.withDelimiter('!').withEscape('!').validate();
6262
}
6363

64-
@Test(expected = IllegalStateException.class)
64+
@Test(expected = IllegalArgumentException.class)
6565
public void testDuplicateHeaderElements() {
6666
CSVFormat.DEFAULT.withHeader("A", "A").validate();
6767
}

0 commit comments

Comments
 (0)