diff --git a/src/Symfony/Component/ObjectMapper/ObjectMapper.php b/src/Symfony/Component/ObjectMapper/ObjectMapper.php index 69f02fb7f1160..a8d24daf935e4 100644 --- a/src/Symfony/Component/ObjectMapper/ObjectMapper.php +++ b/src/Symfony/Component/ObjectMapper/ObjectMapper.php @@ -146,7 +146,7 @@ public function map(object $source, object|string|null $target = null): object } } - if (!$mappingToObject && $ctorArguments && $constructor) { + if (!$mappingToObject && !$map?->transform && $constructor) { try { $mappedTarget->__construct(...$ctorArguments); } catch (\ReflectionException $e) { diff --git a/src/Symfony/Component/ObjectMapper/Tests/Fixtures/InitializedConstructor/A.php b/src/Symfony/Component/ObjectMapper/Tests/Fixtures/InitializedConstructor/A.php new file mode 100644 index 0000000000000..53edb5ba1e3c4 --- /dev/null +++ b/src/Symfony/Component/ObjectMapper/Tests/Fixtures/InitializedConstructor/A.php @@ -0,0 +1,17 @@ + + * + * 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\InitializedConstructor; + +class A +{ + public array $tags = ['foo', 'bar']; +} diff --git a/src/Symfony/Component/ObjectMapper/Tests/Fixtures/InitializedConstructor/B.php b/src/Symfony/Component/ObjectMapper/Tests/Fixtures/InitializedConstructor/B.php new file mode 100644 index 0000000000000..007418edc2b23 --- /dev/null +++ b/src/Symfony/Component/ObjectMapper/Tests/Fixtures/InitializedConstructor/B.php @@ -0,0 +1,31 @@ + + * + * 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\InitializedConstructor; + +class B +{ + public array $tags; + + public function __construct() + { + $this->tags = []; + } + + public function addTag($tag) + { + $this->tags[] = $tag; + } + + public function removeTag($tag) + { + } +} diff --git a/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php b/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php index 99153c3fbdfc7..6557a40ac1092 100644 --- a/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php +++ b/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php @@ -32,6 +32,8 @@ use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\User; use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\UserProfile; use Symfony\Component\ObjectMapper\Tests\Fixtures\HydrateObject\SourceOnly; +use Symfony\Component\ObjectMapper\Tests\Fixtures\InitializedConstructor\A as InitializedConstructorA; +use Symfony\Component\ObjectMapper\Tests\Fixtures\InitializedConstructor\B as InitializedConstructorB; use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallback\A as InstanceCallbackA; use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallback\B as InstanceCallbackB; use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallbackWithArguments\A as InstanceCallbackWithArgumentsA; @@ -147,6 +149,15 @@ public function testDeeperRecursion() $this->assertInstanceOf(RelationDto::class, $mapped->relation); } + public function testMapWithInitializedConstructor() + { + $a = new InitializedConstructorA(); + $mapper = new ObjectMapper(propertyAccessor: PropertyAccess::createPropertyAccessor()); + $b = $mapper->map($a, InitializedConstructorB::class); + $this->assertInstanceOf(InitializedConstructorB::class, $b); + $this->assertEquals($b->tags, ['foo', 'bar']); + } + public function testMapToWithInstanceHook() { $a = new InstanceCallbackA();
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: