Skip to content

Commit a00793f

Browse files
bug symfony#61151 [ObjectMapper] update promoted properties w/ an object as target (soyuka)
This PR was merged into the 7.3 branch. Discussion ---------- [ObjectMapper] update promoted properties w/ an object as target | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | License | MIT When using promoted properties properties would not be mapped when the target was an object, this test case was failing: ```php public function testUpdateObjectWithConstructorPromotedProperties(ObjectMapperInterface $mapper) { $a = new PromotedConstructorSource(1, 'foo'); $b = new PromotedConstructorTarget(1, 'bar'); $v = $mapper->map($a, $b); $this->assertSame($v->name, 'foo'); } ``` Commits ------- 759df62 [ObjectMapper] update promoted properties w/ an object as target
2 parents cbb7b0f + 759df62 commit a00793f

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/Symfony/Component/ObjectMapper/ObjectMapper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ public function map(object $source, object|string|null $target = null): object
156156
}
157157
}
158158

159+
if ($mappingToObject && $ctorArguments) {
160+
foreach ($ctorArguments as $property => $value) {
161+
if ($targetRefl->hasProperty($property) && $targetRefl->getProperty($property)->isPublic()) {
162+
$mapToProperties[$property] = $value;
163+
}
164+
}
165+
}
166+
159167
foreach ($mapToProperties as $property => $value) {
160168
$this->propertyAccessor ? $this->propertyAccessor->setValue($mappedTarget, $property, $value) : ($mappedTarget->{$property} = $value);
161169
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\PromotedConstructor;
13+
14+
class Source
15+
{
16+
public function __construct(
17+
public int $id,
18+
public string $name,
19+
) {
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\PromotedConstructor;
13+
14+
class Target
15+
{
16+
public function __construct(
17+
public int $id,
18+
public string $name,
19+
) {
20+
}
21+
}

src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\ObjectMapper\Metadata\ObjectMapperMetadataFactoryInterface;
2121
use Symfony\Component\ObjectMapper\Metadata\ReflectionObjectMapperMetadataFactory;
2222
use Symfony\Component\ObjectMapper\ObjectMapper;
23+
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
2324
use Symfony\Component\ObjectMapper\Tests\Fixtures\A;
2425
use Symfony\Component\ObjectMapper\Tests\Fixtures\B;
2526
use Symfony\Component\ObjectMapper\Tests\Fixtures\C;
@@ -51,6 +52,8 @@
5152
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargetProperty\C as MultipleTargetPropertyC;
5253
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\A as MultipleTargetsA;
5354
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\C as MultipleTargetsC;
55+
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Source as PromotedConstructorSource;
56+
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Target as PromotedConstructorTarget;
5457
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\AB;
5558
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\Dto;
5659
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLocator\A as ServiceLocatorA;
@@ -345,4 +348,24 @@ public function testDefaultValueStdClassWithPropertyInfo()
345348
$this->assertSame('abc', $b->id);
346349
$this->assertNull($b->optional);
347350
}
351+
352+
/**
353+
* @dataProvider objectMapperProvider
354+
*/
355+
public function testUpdateObjectWithConstructorPromotedProperties(ObjectMapperInterface $mapper)
356+
{
357+
$a = new PromotedConstructorSource(1, 'foo');
358+
$b = new PromotedConstructorTarget(1, 'bar');
359+
$v = $mapper->map($a, $b);
360+
$this->assertSame($v->name, 'foo');
361+
}
362+
363+
/**
364+
* @return iterable<array{0: ObjectMapperInterface}>
365+
*/
366+
public static function objectMapperProvider(): iterable
367+
{
368+
yield [new ObjectMapper()];
369+
yield [new ObjectMapper(new ReflectionObjectMapperMetadataFactory(), PropertyAccess::createPropertyAccessor())];
370+
}
348371
}

0 commit comments

Comments
 (0)