Skip to content

Commit 94ec07b

Browse files
committed
[ObjectMapper] mapping of nested classes with promoted read-only properties
1 parent a11d7f0 commit 94ec07b

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

src/Symfony/Component/ObjectMapper/ObjectMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private function doMap(object $source, object|string|null $target, \WeakMap $obj
173173
}
174174
}
175175

176-
if ($mappingToObject && $ctorArguments) {
176+
if ($mappingToObject && $rootCall && $ctorArguments) {
177177
foreach ($ctorArguments as $property => $value) {
178178
if ($this->propertyIsMappable($refl, $property) && $this->propertyIsMappable($targetRefl, $property)) {
179179
$mapToProperties[$property] = $value;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\ReadOnlyPromotedProperty;
4+
5+
use Symfony\Component\ObjectMapper\Attribute\Map;
6+
7+
#[Map(target: ReadOnlyPromotedPropertyAMapped::class)]
8+
final class ReadOnlyPromotedPropertyA
9+
{
10+
public function __construct(
11+
public ReadOnlyPromotedPropertyB $b,
12+
public string $var1,
13+
) {
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\ReadOnlyPromotedProperty;
4+
5+
use Symfony\Component\ObjectMapper\Attribute\Map;
6+
7+
final class ReadOnlyPromotedPropertyAMapped
8+
{
9+
public function __construct(
10+
public ReadOnlyPromotedPropertyBMapped $b,
11+
public string $var1,
12+
) {
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\ReadOnlyPromotedProperty;
4+
5+
use Symfony\Component\ObjectMapper\Attribute\Map;
6+
7+
#[Map(target: ReadOnlyPromotedPropertyBMapped::class)]
8+
final class ReadOnlyPromotedPropertyB
9+
{
10+
public function __construct(
11+
public string $var2,
12+
) {
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\ReadOnlyPromotedProperty;
4+
5+
use Symfony\Component\ObjectMapper\Attribute\Map;
6+
7+
final class ReadOnlyPromotedPropertyBMapped
8+
{
9+
public function __construct(
10+
public string $var2,
11+
) {
12+
}
13+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Target as PromotedConstructorTarget;
7272
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructorWithMetadata\Source as PromotedConstructorWithMetadataSource;
7373
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructorWithMetadata\Target as PromotedConstructorWithMetadataTarget;
74+
use Symfony\Component\ObjectMapper\Tests\Fixtures\ReadOnlyPromotedProperty\ReadOnlyPromotedPropertyA;
75+
use Symfony\Component\ObjectMapper\Tests\Fixtures\ReadOnlyPromotedProperty\ReadOnlyPromotedPropertyAMapped;
76+
use Symfony\Component\ObjectMapper\Tests\Fixtures\ReadOnlyPromotedProperty\ReadOnlyPromotedPropertyB;
77+
use Symfony\Component\ObjectMapper\Tests\Fixtures\ReadOnlyPromotedProperty\ReadOnlyPromotedPropertyBMapped;
7478
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\AB;
7579
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\Dto;
7680
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLoadedValue\LoadedValueService;
@@ -632,4 +636,22 @@ public function testMapEmbeddedProperties()
632636
$this->assertSame('12345', $user->address->zipcode);
633637
$this->assertSame('Test City', $user->address->city);
634638
}
639+
640+
public function testBugReportLazyLoadingPromotedReadonlyProperty()
641+
{
642+
$source = new ReadOnlyPromotedPropertyA(
643+
b: new ReadOnlyPromotedPropertyB(
644+
var2: 'bar',
645+
),
646+
var1: 'foo',
647+
);
648+
649+
$mapper = new ObjectMapper();
650+
$out = $mapper->map($source);
651+
652+
$this->assertInstanceOf(ReadOnlyPromotedPropertyAMapped::class, $out);
653+
$this->assertInstanceOf(ReadOnlyPromotedPropertyBMapped::class, $out->b);
654+
$this->assertSame('foo', $out->var1);
655+
$this->assertSame('bar', $out->b->var2);
656+
}
635657
}

0 commit comments

Comments
 (0)