8000 bug #54714 [Serializer] convert empty CSV header names into numeric k… · symfony/symfony@b3baa36 · GitHub
[go: up one dir, main page]

Skip to content

Commit b3baa36

Browse files
committed
bug #54714 [Serializer] convert empty CSV header names into numeric keys (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
2 parents c1ca4ca + 93ee57b commit b3baa36

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/Symfony/Component/Serializer/Encoder/CsvEncoder.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,24 @@ public function decode(string $data, string $format, array $context = [])
181181
$depth = $headerCount[$i];
182182
$arr = &$item;
183183
for ($j = 0; $j < $depth; ++$j) {
184+
$headerName = $headers[$i][$j];
185+
186+
if ('' === $headerName) {
187+
$headerName = $i;
188+
}
189+
184190
// Handle nested arrays
185191
if ($j === ($depth - 1)) {
186-
$arr[$headers[$i][$j]] = $cols[$i];
192+
$arr[$headerName] = $cols[$i];
187193

188194
continue;
189195
}
190196

191-
if (!isset($arr[$headers[$i][$j]])) {
192-
$arr[$headers[$i][$j]] = [];
197+
if (!isset($arr[$headerName])) {
198+
$arr[$headerName] = [];
193199
}
194200

195-
$arr = &$arr[$headers[$i][$j]];
201+
$arr = &$arr[$headerName];
196202
}
197203
}
198204

src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,13 @@ public function testDecodeEmptyData()
218218
{
219219
$data = $this->encoder->decode("\n\n", 'csv');
220220

221-
$this->assertSame([['' => null]], $data);
221+
$this->assertSame([[0 => null]], $data);
222+
}
223+
224+
public function testMultipleEmptyHeaderNamesWithSeparator()
225+
{
226+
$this->encoder->decode(',.
227+
,', 'csv');
222228
}
223229

224230
public function testEncodeVariableStructure()

0 commit comments

Comments
 (0)
0