Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PayloadSerializerSupportingObjectMapperAndSerializablePayload does not work recursively #223

Open
axlon opened this issue Mar 19, 2024 · 0 comments

Comments

@axlon
Copy link
Contributor

axlon commented Mar 19, 2024

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)
    }
  }
}
@frankdejonge frankdejonge transferred this issue from EventSaucePHP/ObjectHydrator Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant