Skip to content

Commit

Permalink
bug #54714 [Serializer] convert empty CSV header names into numeric k…
Browse files Browse the repository at this point in the history
…eys (xabbuh)

This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] convert empty CSV header names into numeric keys

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #54709
| License       | MIT

Commits
-------

93ee57b convert empty CSV header names into numeric keys
  • Loading branch information
fabpot committed May 1, 2024
2 parents c1ca4ca + 93ee57b commit b3baa36
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
Expand Up @@ -181,18 +181,24 @@ public function decode(string $data, string $format, array $context = [])
$depth = $headerCount[$i];
$arr = &$item;
for ($j = 0; $j < $depth; ++$j) {
$headerName = $headers[$i][$j];

if ('' === $headerName) {
$headerName = $i;
}

// Handle nested arrays
if ($j === ($depth - 1)) {
$arr[$headers[$i][$j]] = $cols[$i];
$arr[$headerName] = $cols[$i];

continue;
}

if (!isset($arr[$headers[$i][$j]])) {
$arr[$headers[$i][$j]] = [];
if (!isset($arr[$headerName])) {
$arr[$headerName] = [];
}

$arr = &$arr[$headers[$i][$j]];
$arr = &$arr[$headerName];
}
}

Expand Down
Expand Up @@ -218,7 +218,13 @@ public function testDecodeEmptyData()
{
$data = $this->encoder->decode("\n\n", 'csv');

$this->assertSame([['' => null]], $data);
$this->assertSame([[0 => null]], $data);
}

public function testMultipleEmptyHeaderNamesWithSeparator()
{
$this->encoder->decode(',.
,', 'csv');
}

public function testEncodeVariableStructure()
Expand Down

0 comments on commit b3baa36

Please sign in to comment.