macsharker6
macsharker6

Reputation: 61

Duplicate Header Names Apache Commons-csv (withAllowDuplicateHeaderNames doesn't work)

I am using the last version of common-csv library, such as in my pom.xml I have this dependency:

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-csv</artifactId>
  <version>1.7</version>
</dependency>

This library is used in order to write a simple CSV file in a java application. In particular use cases the column name headers of the csv file can be duplicated. I found an interesting property of the CSVFormat class that must be useful in this case but, in every solution described below, the program terminates with error such as:

Exception in thread "main" java.lang.IllegalArgumentException: 
The header contains a duplicate entry: 'VV' in [CC, VV, VV]
  at org.apache.commons.csv.CSVFormat.validate(CSVFormat.java:1676)
  at org.apache.commons.csv.CSVFormat.<init>(CSVFormat.java:793)
  at org.apache.commons.csv.CSVFormat.withHeader(CSVFormat.java:1986)

The code written is:

public static void main(String[] args){
    CSVFormat formatCsv = CSVFormat.DEFAULT.withAllowDuplicateHeaderNames()
                                           .withHeader("CC","VV","VV");
    System.out.println(formatCsv);
}

I have already tried 4 situations:

CSVFormat formatCsv = CSVFormat.DEFAULT.withAllowDuplicateHeaderNames()
                                       .withHeader(headers);

CSVFormat formatCsv = CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(true)
                                       .withHeader(headers);

CSVFormat formatCsv = CSVFormat.DEFAULT.withHeader(headers)
                                       .withAllowDuplicateHeaderNames();

CSVFormat formatCsv = CSVFormat.DEFAULT.withHeader(headers)
                                       .withAllowDuplicateHeaderNames(true);

Is there a bug for the withAllowDuplicateHeaderNames property? It is very difficult to rewrite the code source of the library to change the validation method of CSVFormat.class

Upvotes: 1

Views: 1764

Answers (1)

centic
centic

Reputation: 15882

According to the sources of version 1.7, withAllowDuplicateHeaderNames() currently only affects headers read from the data itself, not headers that you specify via withHeader(), the headers that you specify are currently always checked for duplicates.

This was fixed in version 1.8, see also CSV-241 and PR #43.

A workaround if you are using an older version would be to to filter out those duplicates from the header-collection which you provide.

Upvotes: 3

Related Questions