EventSaucePHP / EventSauce

A pragmatic event sourcing library for PHP with a focus on developer experience.
https://eventsauce.io/
MIT License
817 stars 82 forks source link

PayloadSerializerSupportingObjectMapperAndSerializablePayload does not work recursively #223

Open axlon opened 8 months ago

axlon commented 8 months ago

The PayloadSerializerSupportingObjectMapperAndSerializablePayload only looks at the root type to determine the strategy it should use. Because public methods are serialized by default by the reflection strategy toPayload is getting serialized, but when unserializing fromPayload is never called.

For instance:

class Foo
{
    public function __construct(public Bar $bar) {}
}

class Bar implements SerializablePayload
{
    public function toPayload(): array
    {
        return ['is_bar' => true];
    }

    public static function fromPayload(array $payload): static
    {
        return new static();
    }
}

$strategy = new PayloadSerializerSupportingObjectMapperAndSerializablePayload();
$payload = $strategy->serializePayload(new Foo(new Bar()));
var_dump($payload);

Expected output:

array(1) {
  'bar' =>
  array(1) {
    'is_bar' =>
    bool(true)
  }
}

Actual output:

array(1) {
  'bar' =>
  array(1) {
    'to_payload' =>
    array(1) {
      'is_bar' =>
      bool(true)
    }
  }
}