Skip to content

[ObjectMapper] initialize lazy objects #61146

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
Jul 21, 2025
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
[ObjectMapper] initialize lazy objects
  • Loading branch information
soyuka committed Jul 21, 2025
commit 3b15ab70d0b5e7adb433c40ad49873903c11d00c
7 changes: 7 additions & 0 deletions src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\ObjectMapper\Metadata\ReflectionObjectMapperMetadataFactory;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException as PropertyAccessorNoSuchPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\VarExporter\LazyObjectInterface;

/**
* Object to object mapper.
Expand Down Expand Up @@ -315,6 +316,12 @@
throw new MappingException($e->getMessage(), $e->getCode(), $e);
}

if ($source instanceof LazyObjectInterface) {
$source->initializeLazyObject();
} elseif (\PHP_VERSION_ID >= 80400 && $refl->isUninitializedLazyObject($source)) {

Check failure on line 321 in src/Symfony/Component/ObjectMapper/ObjectMapper.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/ObjectMapper/ObjectMapper.php:321:54: UndefinedMethod: Method ReflectionClass::isUninitializedLazyObject does not exist (see https://psalm.dev/022)

Check failure on line 321 in src/Symfony/Component/ObjectMapper/ObjectMapper.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/ObjectMapper/ObjectMapper.php:321:54: UndefinedMethod: Method ReflectionClass::isUninitializedLazyObject does not exist (see https://psalm.dev/022)
$refl->initializeLazyObject($source);

Check failure on line 322 in src/Symfony/Component/ObjectMapper/ObjectMapper.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/ObjectMapper/ObjectMapper.php:322:20: UndefinedMethod: Method ReflectionClass::initializeLazyObject does not exist (see https://psalm.dev/022)

Check failure on line 322 in src/Symfony/Component/ObjectMapper/ObjectMapper.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/ObjectMapper/ObjectMapper.php:322:20: UndefinedMethod: Method ReflectionClass::initializeLazyObject does not exist (see https://psalm.dev/022)
}

if ($metadata) {
return $refl;
}
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/Fixtures/LazyFoo.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\ObjectMapper\Tests\Fixtures;

use Symfony\Component\VarExporter\LazyGhostTrait;
use Symfony\Component\VarExporter\LazyObjectInterface;

class LazyFoo extends \stdClass implements LazyObjectInterface
{
use LazyGhostTrait;

public string $name = 'foo';
}
17 changes: 17 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/Fixtures/MyProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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\ObjectMapper\Tests\Fixtures;

class MyProxy
{
public string $name;
}
34 changes: 34 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallback\B as InstanceCallbackB;
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallbackWithArguments\A as InstanceCallbackWithArgumentsA;
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallbackWithArguments\B as InstanceCallbackWithArgumentsB;
use Symfony\Component\ObjectMapper\Tests\Fixtures\LazyFoo;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\AToBMapper;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\MapStructMapperMetadataFactory;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\Source;
Expand All @@ -52,6 +53,7 @@
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargetProperty\C as MultipleTargetPropertyC;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\A as MultipleTargetsA;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\C as MultipleTargetsC;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MyProxy;
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Source as PromotedConstructorSource;
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Target as PromotedConstructorTarget;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\AB;
Expand Down Expand Up @@ -368,4 +370,36 @@ public static function objectMapperProvider(): iterable
yield [new ObjectMapper()];
yield [new ObjectMapper(new ReflectionObjectMapperMetadataFactory(), PropertyAccess::createPropertyAccessor())];
}

public function testMapInitializesLazyObject()
{
$lazy = new LazyFoo();
$mapper = new ObjectMapper();
$mapper->map($lazy, \stdClass::class);
$this->assertTrue($lazy->isLazyObjectInitialized());
}

/**
* @requires PHP 8.4
*/
public function testMapInitializesNativePhp84LazyObject()
{
$initialized = false;
$initializer = function () use (&$initialized) {
$initialized = true;

$p = new MyProxy();
$p->name = 'test';

return $p;
};

$r = new \ReflectionClass(MyProxy::class);
$lazyObj = $r->newLazyProxy($initializer);
$this->assertFalse($initialized);
$mapper = new ObjectMapper();
$d = $mapper->map($lazyObj, MyProxy::class);
$this->assertSame('test', $d->name);
$this->assertTrue($initialized);
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/ObjectMapper/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"psr/container": "^2.0"
},
"require-dev": {
"symfony/property-access": "^7.2"
"symfony/property-access": "^7.2",
"symfony/var-exporter": "^7.2"
},
"autoload": {
"psr-4": {
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy