Symfony Serializer Component AbstractNormalizer::CALLBACKS denormalize

I am trying to use Serialize with callbacks like in https://symfony.com/doc/current/components/serializer.html#using-callbacks-to-serialize-properties-with-object-instances . But it doesn’t seem to go into the callback. I can ignore attributes with ‘IGNORED_ATTRIBUTES’ just fine, just CALLBACKS not working. What could I be doing wrong?

$dateCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
            dump('foo');
            return 'faa';
        };        
$defaultContext = [
            AbstractNormalizer::CALLBACKS => [
                'order_date' => $dateCallback,
            ]
        ];        
$normalizer = new ObjectNormalizer(null, null, null, null, null, null,  $defaultContext);
$serializer = new Serializer([$normalizer], []);
$order = $serializer->denormalize($data, Orderform::class, 'array');

The data is a simple array.

$data = ['order_date' => '2020-07-07',
'order_number' => '123'];

I would expect the $dateCallback to be called. But it doesn’t seem to do that. The Orderform entity is getting populated but not with the value I would expect from the callback.
I tried making all this with json and xml too, since array doesn’t show up in the documentation (but it works except for the callback)

Symfony 4.4

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *