Skip to content

Symfony Serializer deserialization is failing for a object with Discriminator for the csv encoder #60514

Open
@comma8600

Description

@comma8600

I am having an issue with deserializing discriminated csv data, after some wrong directions believing it to do with the properties. I have created a minimal example by copying the existing discriminator test in the symfony project and changing it to the csv encoder, that shows even the minimal Dummy test classes while being able to be serialized to csv cannot then be deserialized.

I believe this should work as the deserializer was working on an older version of symfony (see comments on the other ticket despite downgrading the serializer component it did not start working again, only when I reverted to the full composer.lock did it start working again)

Ah ignore the above example I've got a much more minimal test where it is failing for:

I've taken the existing symfony/symfony project 6.4 tag tests and added an identical test to the discriminator for json but swapping in the csv formatter:

    private function serializerWithClassDiscriminatorCsv()
    {
        $classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());

        return new Serializer([new ObjectNormalizer($classMetadataFactory, null, null, new ReflectionExtractor(), new ClassDiscriminatorFromClassMetadata($classMetadataFactory))], ['csv' => new CsvEncoder()]);
    }


    public function testDeserializeAndSerializeInterfacedObjectsWithTheClassMetadataDiscriminatorResolverCsv()
    {
        $example = new DummyMessageNumberOne();
        $example->one = 1;

        $csvData = 'type,one,two' . PHP_EOL .'one,1,' . PHP_EOL;

        $serializer = $this->serializerWithClassDiscriminatorCsv();
        $deserialized = $serializer->deserialize($csvData, DummyMessageInterface::class, 'csv');
        $this->assertEquals($example, $deserialized);

        $serialized = $serializer->serialize($deserialized, 'csv');
        $this->assertEquals($csvData, $serialized);
    }

give the result:


There was 1 error:

1) Symfony\Component\Serializer\Tests\SerializerTest::testDeserializeAndSerializeInterfacedObjectsWithTheClassMetadataDiscriminatorResolverCsv
Symfony\Component\Serializer\Exception\NotNormalizableValueException: Type property "type" not found for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface".

/Users/alexdawn/projects/symfony/src/Symfony/Component/Serializer/Exception/NotNormalizableValueException.php:32
/Users/alexdawn/projects/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:855
/Users/alexdawn/projects/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:332
/Users/alexdawn/projects/symfony/src/Symfony/Component/Serializer/Serializer.php:249
/Users/alexdawn/projects/symfony/src/Symfony/Component/Serializer/Serializer.php:154
/Users/alexdawn/projects/symfony/src/Symfony/Component/Serializer/Tests/SerializerTest.php:468

serialization works on the $example object and gives an identical string to the $csvData. It's just going the opposite directions which is failing

Originally posted by @comma8600 in #60214

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