facile-it / mongodb-bundle

Bundle service integration of official mongodb/mongo-php-library
MIT License
35 stars 16 forks source link

Debug toolbar data collector has infinite recursion when serializing a cyclic object #167

Open mhvis opened 2 months ago

mhvis commented 2 months ago

We encountered this issue while using Symfony Live Components. We were (accidentally as part of a larger object) trying to insert a MountedComponent instance into a MongoDB collection. When the debug toolbar is active, we get an infinite recursion in the MongoQuerySerializer class.

Steps to reproduce:

  1. Have an object with a circular dependency:
class CyclicDependantObject
{
    private CyclicDependantObject $me;

    public function __construct()
    {
        $this->me = $this;
    }
}
  1. Insert it to a collection using a MongoDB Database.
class AController extends AbstractController
{
    #[Route('/endpoint', name: 'endpoint')]
    public function endpoint(#[Autowire(service: 'mongo.connection.symfony')] Database $database): Response
    {
        $database->createCollection('random');

        $collection = $database->selectCollection('random');

        $collection->insertOne(['cyclicObject' => new CyclicDependantObject()]);

        return new Response('Data inserted');
    }
}
ilario-pierbattista commented 2 months ago

hi @mhvis thanks for reporting this.

Adding a maxdepth arg to this function \Facile\MongoDbBundle\DataCollector\MongoQuerySerializer::prepareUnserializableData should probably be enough to stop the endless recursion.

Having a unit test to reproduce this would be great.