Skip to content

Commit

Permalink
Merge branch '7.0' into 7.1
Browse files Browse the repository at this point in the history
* 7.0:
  [Serializer] Fix merge
  [VarExporter] add `#[Ignore]` to proxy-related methods to prevent them from being serialized
  • Loading branch information
derrabus committed Mar 19, 2024
2 parents fb28944 + a57c20b commit a9133af
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 24 deletions.
Expand Up @@ -28,33 +28,25 @@ public function __construct($freeTrial, $hasSubscribe, $getReady, $isActive)
$this->isActive = $isActive;
}

/**
* @SerializedName("free_trial_method")
*/
#[SerializedName('free_trial_method')]
public function getFreeTrial()
{
return $this->freeTrial;
}

/**
* @SerializedName("has_subscribe_method")
*/
#[SerializedName('has_subscribe_method')]
public function hasSubscribe()
{
return $this->hasSubscribe;
}

/**
* @SerializedName("get_ready_method")
*/
#[SerializedName('get_ready_method')]
public function getReady()
{
return $this->getReady;
}

/**
* @SerializedName("is_active_method")
*/
#[SerializedName('is_active_method')]
public function isActive()
{
return $this->isActive;
Expand Down
Expand Up @@ -15,24 +15,16 @@

class SamePropertyAsMethodWithPropertySerializedNameDummy
{
/**
* @SerializedName("free_trial_property")
*/
#[SerializedName('free_trial_property')]
private $freeTrial;

/**
* @SerializedName("has_subscribe_property")
*/
#[SerializedName('has_subscribe_property')]
private $hasSubscribe;

/**
* @SerializedName("get_ready_property")
*/
#[SerializedName('get_ready_property')]
private $getReady;

/**
* @SerializedName("is_active_property")
*/
#[SerializedName('is_active_property')]
private $isActive;

public function __construct($freeTrial, $hasSubscribe, $getReady, $isActive)
Expand Down
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
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 @@ -64,6 +65,7 @@ public static function createLazyGhost(\Closure $initializer, ?array $skippedPro
*
* @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 @@ -340,6 +342,7 @@ public function __destruct()
}
}

#[Ignore]
private function setLazyObjectAsInitialized(bool $initialized): void
{
if ($state = $this->lazyObjectState ?? null) {
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/VarExporter/LazyProxyTrait.php
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 @@ -60,6 +61,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
@@ -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
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\ProxyHelper;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildMagicClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildStdClass;
Expand All @@ -21,6 +24,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 @@ -263,6 +267,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
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 @@ -278,6 +282,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

0 comments on commit a9133af

Please sign in to comment.