8000 [VarExporter] add `#[Ignore]` to proxy-related methods to prevent the… · symfony/symfony@7c2e848 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c2e848

Browse files
priyadinicolas-grekas
authored andcommitted
[VarExporter] add #[Ignore] to proxy-related methods to prevent them from being serialized
1 parent f9621d3 commit 7c2e848

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

src/Symfony/Component/VarExporter/Internal/LazyObjectTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111

1212
namespace Symfony\Component\VarExporter\Internal;
1313

14+
use Symfony\Component\Serializer\Attribute\Ignore;
15+
1416
if (\PHP_VERSION_ID >= 80300) {
1517
/**
1618
* @internal
1719
*/
1820
trait LazyObjectTrait
1921
{
22+
#[Ignore]
2023
private readonly LazyObjectState $lazyObjectState;
2124
}
2225
} else {
@@ -25,6 +28,7 @@ trait LazyObjectTrait
2528
*/
2629
trait LazyObjectTrait
2730
{
31+
#[Ignore]
2832
private LazyObjectState $lazyObjectState;
2933
}
3034
}

src/Symfony/Component/VarExporter/LazyGhostTrait.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\VarExporter;
1313

14+
use Symfony\Component\Serializer\Attribute\Ignore;
1415
use Symfony\Component\VarExporter\Internal\Hydrator;
1516
use Symfony\Component\VarExporter\Internal\LazyObjectRegistry as Registry;
1617
use Symfony\Component\VarExporter\Internal\LazyObjectState;
@@ -61,6 +62,7 @@ public static function createLazyGhost(\Closure|array $initializer, ?array $skip
6162
*
6263
* @param $partial Whether partially initialized objects should be considered as initialized
6364
*/
65+
#[Ignore]
6466
public function isLazyObjectInitialized(bool $partial = false): bool
6567
{
6668
if (!$state = $this->lazyObjectState ?? null) {
@@ -389,6 +391,7 @@ public function __destruct()
389391
}
390392
}
391393

394+
#[Ignore]
392395
private function setLazyObjectAsInitialized(bool $initialized): void
393396
{
394397
$state = $this->lazyObjectState ?? null;

src/Symfony/Component/VarExporter/LazyProxyTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\VarExporter;
1313

14+
use Symfony\Component\Serializer\Attribute\Ignore;
1415
use Symfony\Component\VarExporter\Hydrator as PublicHydrator;
1516
use 8000 Symfony\Component\VarExporter\Internal\Hydrator;
1617
use Symfony\Component\VarExporter\Internal\LazyObjectRegistry as Registry;
@@ -50,6 +51,7 @@ public static function createLazyProxy(\Closure $initializer, ?object $instance
5051
*
5152
* @param $partial Whether partially initialized objects should be considered as initialized
5253
*/
54+
#[Ignore]
5355
public function isLazyObjectInitialized(bool $partial = false): bool
5456
{
5557
return !isset($this->lazyObjectState) || isset($this->lazyObjectState->realInstance) || Registry::$noInitializerState === $this->lazyObjectState->initializer;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8< 628C span class="diff-text-marker">+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarExporter\Tests\Fixtures;
13+
14+
class SimpleObject
15+
{
16+
public function getMethod(): string
17+
{
18+
return 'method';
19+
}
20+
21+
public string $property = 'property';
22+
}

src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
namespace Symfony\Component\VarExporter\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
16+
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
17+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1518
use Symfony\Component\VarExporter\Internal\LazyObjectState;
1619
use Symfony\Component\VarExporter\ProxyHelper;
1720
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildMagicClass;
@@ -22,6 +25,7 @@
2225
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\MagicClass;
2326
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ReadOnlyClass;
2427
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\TestClass;
28+
use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject;
2529

2630
class LazyGhostTraitTest extends TestCase
2731
{
@@ -461,6 +465,19 @@ public function testAccessingUninializedPropertyWithLazyGhost()
461465
$object->property;
462466
}
463467

468+
public function testNormalization()
469+
{
470+
$object = $this->createLazyGhost(SimpleObject::class, function ($instance) {});
471+
472+
$loader = new AttributeLoader();
473+
$metadataFactory = new ClassMetadataFactory($loader);
474+
$serializer = new ObjectNormalizer($metadataFactory);
475+
476+
$output = $serializer->normalize($object);
477+
478+
$this->assertSame(['property' => 'property', 'method' => 'method'], $output);
479+
}
480+
464481
/**
465482
* @template T
466483
*

src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
namespace Symfony\Component\VarExporter\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
16+
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
17+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1518
use Symfony\Component\VarExporter\Exception\LogicException;
1619
use Symfony\Component\VarExporter\LazyProxyTrait;
1720
use Symfony\Component\VarExporter\ProxyHelper;
@@ -22,6 +25,7 @@
2225
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestOverwritePropClass;
2326
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestUnserializeClass;
2427
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestWakeupClass;
28+
use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject;
2529

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

288+
public function testNormalization()
289+
{
290+
$object = $this->createLazyProxy(SimpleObject::class, fn () => new SimpleObject());
291+
292+
$loader = new AttributeLoader();
293+
$metadataFactory = new ClassMetadataFactory($loader);
294+
$serializer = new ObjectNormalizer($metadataFactory);
295+
296+
$output = $serializer->normalize($object);
297+
298+
$this->assertSame(['property' => 'property', 'method' => 'method'], $output);
299+
}
300+
284301
/**
285302
* @template T
286303
*

0 commit comments

Comments
 (0)
0