1

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

barbsan
  • 3,418
  • 11
  • 21
  • 28
  • 1
    withAllowDuplicateHeaderNames() currently only affects headers read from the data itself, not headers that you specify via withHeader(), those are always checked for duplicates. But you should be able to filter out those duplicates from the header-collection which you provide easily, or? – centic Sep 08 '19 at 14:36
  • @centic I suggest making an Answer of your Comment, so this Question can be marked resolved. – Basil Bourque Sep 08 '19 at 17:32

1 Answers1

3

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.

centic
  • 15,565
  • 9
  • 68
  • 125
  • Thanks for your answer. When you write "A workaround for now would be to to filter out those duplicates from the header-collection which you provide." what do you mean? Regards. – macsharker6 Sep 09 '19 at 10:17
  • When you say you call it with ".withHeader(headers);", the "headers" is some sort of collection that you provide. It should be possible to filter duplicates from this collection before you give it to withHeader(), or? – centic Sep 11 '19 at 07:51
  • Hi, I have not to filter duplicated but the headers must be written in the order of the items of the headers list. – macsharker6 Sep 11 '19 at 13:00
  • Thanks for the answer. The update `1.8` has the fix for `withAllowDuplicateHeaderNames` – swapab Aug 14 '20 at 11:09
  • Thanks for the note, I have adjusted the answer for actual release of version 1.8 now – centic Aug 15 '20 at 07:04