Skip to content

Commit ed9a69d

Browse files
committed
[ObjectMapper] Correctly manage constructor initialization
1 parent 04c970c commit ed9a69d

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

ObjectMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function map(object $source, object|string|null $target = null): object
146146
}
147147
}
148148

149-
if (!$mappingToObject && $ctorArguments && $constructor) {
149+
if (!$mappingToObject && !$map?->transform && $constructor) {
150150
try {
151151
$mappedTarget->__construct(...$ctorArguments);
152152
} catch (\ReflectionException $e) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* 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\ObjectMapper\Tests\Fixtures\InitializedConstructor;
13+
14+
class A
15+
{
16+
public array $tags = ['foo', 'bar'];
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* 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\ObjectMapper\Tests\Fixtures\InitializedConstructor;
13+
14+
class B
15+
{
16+
public array $tags;
17+
18+
public function __construct()
19+
{
20+
$this->tags = [];
21+
}
22+
23+
public function addTag($tag)
24+
{
25+
$this->tags[] = $tag;
26+
}
27+
28+
public function removeTag($tag)
29+
{
30+
}
31+
}

Tests/ObjectMapperTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\User;
3333
use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\UserProfile;
3434
use Symfony\Component\ObjectMapper\Tests\Fixtures\HydrateObject\SourceOnly;
35+
use Symfony\Component\ObjectMapper\Tests\Fixtures\InitializedConstructor\A as InitializedConstructorA;
36+
use Symfony\Component\ObjectMapper\Tests\Fixtures\InitializedConstructor\B as InitializedConstructorB;
3537
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallback\A as InstanceCallbackA;
3638
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallback\B as InstanceCallbackB;
3739
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallbackWithArguments\A as InstanceCallbackWithArgumentsA;
@@ -147,6 +149,15 @@ public function testDeeperRecursion()
147149
$this->assertInstanceOf(RelationDto::class, $mapped->relation);
148150
}
149151

152+
public function testMapWithInitializedConstructor()
153+
{
154+
$a = new InitializedConstructorA();
155+
$mapper = new ObjectMapper(propertyAccessor: PropertyAccess::createPropertyAccessor());
156+
$b = $mapper->map($a, InitializedConstructorB::class);
157+
$this->assertInstanceOf(InitializedConstructorB::class, $b);
158+
$this->assertEquals($b->tags, ['foo', 'bar']);
159+
}
160+
150161
public function testMapToWithInstanceHook()
151162
{
152163
$a = new InstanceCallbackA();

0 commit comments

Comments
 (0)