From 83b83237ffa1c6946b57955747dfcb61fadc2c7d Mon Sep 17 00:00:00 2001 From: Martin Poirier Theoret Date: Tue, 24 May 2022 21:50:12 -0400 Subject: [PATCH] ignore proxy object if not in hierarchy --- .../Subscriber/DoctrineProxySubscriber.php | 14 +++++++------- .../Subscriber/DoctrineProxySubscriberTest.php | 11 +++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/EventDispatcher/Subscriber/DoctrineProxySubscriber.php b/src/EventDispatcher/Subscriber/DoctrineProxySubscriber.php index b39ded887..47c661bcc 100644 --- a/src/EventDispatcher/Subscriber/DoctrineProxySubscriber.php +++ b/src/EventDispatcher/Subscriber/DoctrineProxySubscriber.php @@ -37,10 +37,9 @@ public function onPreSerialize(PreSerializeEvent $event): void $object = $event->getObject(); $type = $event->getType(); - // If the set type name is not an actual class, but a faked type for which a custom handler exists, we do not - // modify it with this subscriber. Also, we forgo autoloading here as an instance of this type is already created, - // so it must be loaded if its a real class. - $virtualType = !class_exists($type['name'], false); + // If the set type name is not an actual class, but a faked type for which a custom handler exists or + // object is not an instance of that class we do not modify it with this subscriber. + $virtualType = !class_exists($type['name']) || !$object instanceof $type['name']; if ( $object instanceof PersistentCollection @@ -85,12 +84,13 @@ public function onPreSerialize(PreSerializeEvent $event): void public function onPreSerializeTypedProxy(PreSerializeEvent $event, string $eventName, string $class, string $format, EventDispatcherInterface $dispatcher): void { $type = $event->getType(); - // is a virtual type? then there is no need to change the event name - if (!class_exists($type['name'], false)) { + $object = $event->getObject(); + + // is a virtual type or not an instance of? then there is no need to change the event name + if (!class_exists($type['name']) || !$object instanceof $type['name']) { return; } - $object = $event->getObject(); if ($object instanceof Proxy) { $parentClassName = get_parent_class($object); diff --git a/tests/Serializer/EventDispatcher/Subscriber/DoctrineProxySubscriberTest.php b/tests/Serializer/EventDispatcher/Subscriber/DoctrineProxySubscriberTest.php index 040632dd4..f308a8533 100644 --- a/tests/Serializer/EventDispatcher/Subscriber/DoctrineProxySubscriberTest.php +++ b/tests/Serializer/EventDispatcher/Subscriber/DoctrineProxySubscriberTest.php @@ -76,6 +76,17 @@ public function testProxyLoadingCanBeSkippedForVirtualTypes() self::assertFalse($obj->__isInitialized()); } + public function testProxyLoadingCanBeSkippedForClassNotInHierarchy() + { + $subscriber = new DoctrineProxySubscriber(true); + + $event = $this->createEvent($obj = new SimpleObjectProxy('a', 'b'), ['name' => 'stdClass', 'params' => []]); + $subscriber->onPreSerialize($event); + + self::assertEquals(['name' => 'stdClass', 'params' => []], $event->getType()); + self::assertFalse($obj->__isInitialized()); + } + public function testProxyLoadingCanBeSkippedByExclusionStrategy() { $subscriber = new DoctrineProxySubscriber(false, false);