spatie / laravel-event-projector

Event sourcing for Artisans 📽

Home Page:https://docs.spatie.be/laravel-event-projector

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong DTO's deserialization

endihunter opened this issue · comments

Using symfony serializer leads to a serialization/deserialization issue:
I have complex Dto's with nested objects (also dto) which are serialized to a json (field: event_properties), but on deserialization - only top level obect is restored to a DTO:

Example:

<?php

namespace App\DTO;

use JMS\Serializer\Annotation as Serializer;

class OrderDto
{
    /**
     * @var string
     * @Serializer\Type("string")
     */
    public $orderId;

    /**
     * @var int
     * @Serializer\Type("int")
     */
    public $storeId;

    /**
     * @var string
     * @Serializer\Type("string")
     */
    public $shippingAddress;

    /**
     * @var string
     * @Serializer\Type("string")
     */
    public $comment;

    /**
     * @var OrderProductDto[]
     * @Serializer\Type("array<App\DTO\OrderProductDto>")
     */
    public $products = [];
}

class OrderProductDto
{
    /**
     * @var string
     *
     * @Serializer\Type("string")
     */
    public $sku;

    /**
     * @var int
     *
     * @Serializer\Type("int")
     */
    public $quantity;
}

on deserialisation, the $products is just a multidimensional array, instead of OrderProductDto[]

hope it's clear enough...

Great job on this package!

If you have problems with serializing and deserializing events I suggest you create a custom serializer that can handle your events.

@endihunter Have you found a way to create a custom serializer to handle object serialisation/deserialization?

@freekmurze Do you maybe have an example of a custom serializer?