Skip to content

[Serializer] Skip empty lines when decoding multi-column CSV documents - #67752

Merged
nicolas-grekas merged 1 commit into
symfony:6.4from
0xABCD01:fix/csv-encoder-blank-lines
Nov 1, 2026
Merged

nicolas-grekas merged 1 commit into
symfony:6.4from
0xABCD01:fix/csv-encoder-blank-lines

Conversation

@0xABCD01

@0xABCD01 0xABCD01 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor
Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #67751
License MIT

CsvEncoder::decode() turned the [null] that fgetcsv() returns for an empty line into a row. In a multi-column document, this gave phantom rows like ['a' => null], and an empty line before the header row garbled the whole result. Go's encoding/csv and Python's csv.DictReader skip empty lines between records.

This PR skips empty lines when the first non-empty line of the document has several fields. In a single-column document, an empty line is a record with an empty value. The encoder writes one for each '' or null value, so these lines are still decoded as rows.

Merge-up notes

Merge-up to 8.1: the while (... fgetcsv(...)) line in decode() conflicts. Resolve it to foreach ($this->readRows($handle, $delimiter, $enclosure) as $cols) {, then remove the $escapeChar argument of readRows() and pass '' to its two fgetcsv() calls.

Empty lines are skipped when the first non-empty line of the document has several fields. In a single-column document, an empty line is a record with an empty value, so it is still decoded as one.
@nicolas-grekas
nicolas-grekas force-pushed the fix/csv-encoder-blank-lines branch from fe23431 to 48306bb Compare November 1, 2026 18:45
@nicolas-grekas nicolas-grekas changed the title [Serializer] Skip empty lines when decoding CSV [Serializer] Skip empty lines when decoding multi-column CSV documents Nov 1, 2026
@nicolas-grekas

Copy link
Copy Markdown
Member

Thanks for the PR. In a single-column CSV, an empty line is a record with an empty value. encode([['name' => null], ['name' => 'alice']]) gives "name\n\nalice\n", which this PR decoded as [['name' => 'alice']]. I pushed a change so that empty lines are skipped only when the first non-empty line has several fields.

@nicolas-grekas
nicolas-grekas merged commit ff8c6b2 into symfony:6.4 Nov 1, 2026
15 checks passed
This was referenced Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants