Skip to content

[VarExporter] add #[Ignore] to proxy-related methods to prevent them from being serialized #54224

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

Merged
merged 1 commit into from
Mar 19, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@

namespace Symfony\Component\VarExporter\Internal;

use Symfony\Component\Serializer\Attribute\Ignore;

if (\PHP_VERSION_ID >= 80300) {
/**
* @internal
*/
trait LazyObjectTrait
{
#[Ignore]
private readonly LazyObjectState $lazyObjectState;
}
} else {
Expand All @@ -25,6 +28,7 @@ trait LazyObjectTrait
*/
trait LazyObjectTrait
{
#[Ignore]
private LazyObjectState $lazyObjectState;
}
}
3 changes: 3 additions & 0 deletions src/Symfony/Component/VarExporter/LazyGhostTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\VarExporter;

use Symfony\Component\Serializer\Attribute\Ignore;
use Symfony\Component\VarExporter\Internal\Hydrator;
use Symfony\Component\VarExporter\Internal\LazyObjectRegistry as Registry;
use Symfony\Component\VarExporter\Internal\LazyObjectState;
Expand Down Expand Up @@ -61,6 +62,7 @@ public static function createLazyGhost(\Closure|array $initializer, ?array $skip
*
* @param $partial Whether partially initialized objects should be considered as initialized
*/
#[Ignore]
public function isLazyObjectInitialized(bool $partial = false): bool
{
if (!$state = $this->lazyObjectState ?? null) {
Expand Down Expand Up @@ -389,6 +391,7 @@ public function __destruct()
}
}

#[Ignore]
private function setLazyObjectAsInitialized(bool $initialized): void
{
$state = $this->lazyObjectState ?? null;
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/VarExporter/LazyProxyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\VarExporter;

use Symfony\Component\Serializer\Attribute\Ignore;
use Symfony\Component\VarExporter\Hydrator as PublicHydrator;
use Symfony\Component\VarExporter\Internal\Hydrator;
use Symfony\Component\VarExporter\Internal\LazyObjectRegistry as Registry;
Expand Down Expand Up @@ -50,6 +51,7 @@ public static function createLazyProxy(\Closure $initializer, ?object $instance
*
* @param $partial Whether partially initialized objects should be considered as initialized
*/
#[Ignore]
public function isLazyObjectInitialized(bool $partial = false): bool
{
return !isset($this->lazyObjectState) || isset($this->lazyObjectState->realInstance) || Registry::$noInitializerState === $this->lazyObjectState->initializer;
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/Fixtures/SimpleObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarExporter\Tests\Fixtures;

class SimpleObject
{
public function getMethod(): string
{
return 'method';
}

public string $property = 'property';
}
17 changes: 17 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
namespace Symfony\Component\VarExporter\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\VarExporter\Internal\LazyObjectState;
use Symfony\Component\VarExporter\ProxyHelper;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildMagicClass;
Expand All @@ -22,6 +25,7 @@
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\MagicClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ReadOnlyClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\TestClass;
use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject;

class LazyGhostTraitTest extends TestCase
{
Expand Down Expand Up @@ -461,6 +465,19 @@ public function testAccessingUninializedPropertyWithLazyGhost()
$object->property;
}

public function testNormalization()
{
$object = $this->createLazyGhost(SimpleObject::class, function ($instance) {});

$loader = new AttributeLoader();
$metadataFactory = new ClassMetadataFactory($loader);
$serializer = new ObjectNormalizer($metadataFactory);

$output = $serializer->normalize($object);

$this->assertSame(['property' => 'property', 'method' => 'method'], $output);
}

/**
* @template T
*
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
namespace Symfony\Component\VarExporter\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\VarExporter\Exception\LogicException;
use Symfony\Component\VarExporter\LazyProxyTrait;
use Symfony\Component\VarExporter\ProxyHelper;
Expand All @@ -22,6 +25,7 @@
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestOverwritePropClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestUnserializeClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestWakeupClass;
use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject;

class LazyProxyTraitTest extends TestCase
{
Expand Down Expand Up @@ -281,6 +285,19 @@ public function __construct()
$this->assertSame(['foo' => 123], (array) $obj->getDep());
}

public function testNormalization()
{
$object = $this->createLazyProxy(SimpleObject::class, fn () => new SimpleObject());

$loader = new AttributeLoader();
$metadataFactory = new ClassMetadataFactory($loader);
$serializer = new ObjectNormalizer($metadataFactory);

$output = $serializer->normalize($object);

$this->assertSame(['property' => 'property', 'method' => 'method'], $output);
}

/**
* @template T
*
Expand Down