[
https://issues.apache.org/jira/browse/CSV-268?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18061739#comment-18061739
]
Makarand Hinge commented on CSV-268:
------------------------------------
Thank you [~ggregory] for the clarification question.
I think when both an explicit header (enum or strings) and
withFirstRecordAsHeader() are used together, we should define a clear contract.
My suggestion would be:
• If both are provided, the first record from the file should be validated
against the explicitly configured header.
• If they differ in name, order, or column count, an IllegalArgumentException
should be thrown.
This would prevent silent misalignment issues while keeping the behavior
deterministic.
Alternatively, we could disallow combining both configurations entirely if that
better aligns with the intended API design.
> CSVFormat.withFirstRecordAsHeader() resets previously set headers
> -----------------------------------------------------------------
>
> Key: CSV-268
> URL: https://issues.apache.org/jira/browse/CSV-268
> Project: Commons CSV
> Issue Type: Bug
> Affects Versions: 1.8
> Reporter: Auke te Winkel
> Priority: Minor
>
> I migrated from 1.7 to 1.8, which breaks the following code:
> {code:java}
> CSVFormat csvFormat = CSVFormat.EXCEL
> .withDelimiter(delimiter)
> .withHeader(csvHeadersEnumClass)
> .withFirstRecordAsHeader()
> .withIgnoreHeaderCase(true)
> .withAllowMissingColumnNames(false)
> .withTrim();
> {code}
> This now results in a CSVFormat with skipHeaderRecord = true but with the
> header array emtpy.
>
> This is caused by the fact that .withFirstRecordAsHeader() now resets the
> previously set header String[] to empty. This issue did not occur in 1.7.
> The Javadoc of this method is ambiguous as to what it should do, as it states
> {code:java}
> @return A new CSVFormat that is equal to this but using the first record as
> header.
> {code}
> which is not true, as the returned format is not equal to this (header is
> reset). However, it also states
> {code:java}
> <p>
> * Calling this method is equivalent to calling:
> * </p>
> *
> * <pre>
> * CSVFormat format = aFormat.withHeader().withSkipHeaderRecord();
> * </pre>
> {code}
> which is correct.
> Switching the statements, i.e.
> {code:java}
> CSVFormat csvFormat = CSVFormat.EXCEL
> .withDelimiter(delimiter)
> .withFirstRecordAsHeader()
> .withHeader(csvHeadersEnumClass)
> .withIgnoreHeaderCase(true)
> .withAllowMissingColumnNames(false)
> .withTrim();
> {code}
> yields a different CSVFormat with skipHeaderRecord = true, but also a
> non-empty header array containing the names of the header enum class.
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)