Skip to content

Commit 93ee57b

Browse files
committed
convert empty CSV header names into numeric keys
1 parent 7e5d7dc commit 93ee57b

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

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

Lines changed: 10 additions & 4 deletions
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

Lines changed: 7 additions & 1 deletion
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)