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

ignore proxy object if not in hierarchy #1413

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/EventDispatcher/Subscriber/DoctrineProxySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down