Skip to content

[Serializer] Xml serializer converts false attribute to null #47002

@xorock

Description

@xorock

Symfony version(s) affected

6.1

Description

When serializing a class to an XML string, an attribute with a value of false is lost and replaced with null.

How to reproduce

$dummy = new class {
            #[\Symfony\Component\Serializer\Annotation\SerializedName('@test')]
            public bool $foo = false;
            public bool $bar = false;
        };

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

        $serializer = new Symfony\Component\Serializer\Serializer([
            new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter)
        ], [
            new \Symfony\Component\Serializer\Encoder\XmlEncoder()
    ]);
        var_dump($serializer->serialize($dummy, 'xml'));

Possible Solution

No response

Additional Context

Generated XML is now wrong:

<?xml version="1.0"?>
<response test=""><bar>0</bar></response>

Should be:

<?xml version="1.0"?>
<response test="0"><bar>0</bar></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