Skip to content

[Serializer] CsvEncoder : enclosure must be a character #48440

Closed
@Oipnet

Description

@Oipnet

Symfony version(s) affected

6.2.0

Description

When you try tu use empty enclosure the serialization fail

How to reproduce

`require DIR.'/../vendor/autoload.php';

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

$encoders = [new JsonEncoder(), new CsvEncoder()];
$normalizers = [new ObjectNormalizer($classMetadataFactory)];

$serializer = new Serializer($normalizers, $encoders);

$context = [CsvEncoder::ENCLOSURE_KEY => ''];

$json = $serializer->serialize(
new Foo(bar: 'bar value', baz: 'baz value', buzz: 'buz value'),
format: 'csv',
context: $context
);`

Possible Solution

When we serialize we can check if the enclosure is empty.
If the enclosure is empty call the method fputcsv with the default enclosure
use str_replace to remplace the default enclosure by empty string
write the file with the string changed

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