8000 [Serializer] `csv_headers` not working as expected · Issue #55048 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[Serializer] csv_headers not working as expected #55048
Open
@Seb33300

Description

@Seb33300

Symfony version(s) affected

7.0.7

Description

The CsvEncoder has an option to specify the CSV headers, but it doesn't look to work as expected.

When specifying header, the CSV is generated with new columns added.

How to reproduce

$contextBuilder = (new CsvEncoderContextBuilder())
    ->withContext((new ObjectNormalizerContextBuilder())->withAttributes(['firstName', 'lastName']))
    ->withHeaders(['Prénom', 'Nom'])
    ->withDelimiter(';')
;

$csv = $serializer->serialize([array of entities], 'csv', $contextBuilder->toArray());

Result:

Prénom;Nom;firstName;lastName
;;Entity first name;Entity last name

Expected result:

Prénom;Nom
Entity first name;Entity last name

Possible Solution

Probably something wrong here:

$headers = array_merge(array_values($headers), array_diff($this->extractHeaders($data), $headers));

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0