api-platform / docs

API Platform documentation
https://api-platform.com/docs/
164 stars 1.06k forks source link

Decorating the built in data persister results in endless loop. #1491

Closed NotionCommotion closed 2 years ago

NotionCommotion commented 2 years ago

Per https://api-platform.com/docs/core/data-persisters/#decorating-the-built-in-data-persisters, ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface is used. When I attempted, I received an error and was advised to use ApiPlatform\Core\DataPersister\DataPersisterInterface. When doing so, all works at first, but return $this->decorated->supports($data, $context) results in an endless loop. Perhaps just change to return $data instanceof BlogPost;? If this is more than just a documentation issue, please either let me know so I will submit a bug issue or move it. Thanks!

namespace App\DataPersister;

use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
use App\Entity\User;
use Symfony\Component\Mailer\MailerInterface;

final class UserDataPersister implements ContextAwareDataPersisterInterface
{
    private $decorated;
    private $mailer;

    public function __construct(ContextAwareDataPersisterInterface $decorated, MailerInterface $mailer)
    {
        $this->decorated = $decorated;
        $this->mailer = $mailer;
    }

    public function supports($data, array $context = []): bool
    {
        return $this->decorated->supports($data, $context);
    }

    // ...
}
NotionCommotion commented 2 years ago

User error