Skip to content

Commit 5ada3bc

Browse files
Merge branch '7.4' into 8.0
* 7.4: [ObjectMapper] mapping of nested classes with promoted read-only properties
2 parents 660a224 + bee0f44 commit 5ada3bc

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
@@ -70,6 +70,10 @@
7070
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Target as PromotedConstructorTarget;
7171
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructorWithMetadata\Source as PromotedConstructorWithMetadataSource;
7272
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructorWithMetadata\Target as PromotedConstructorWithMetadataTarget;
73+
use Symfony\Component\ObjectMapper\Tests\Fixtures\ReadOnlyPromotedProperty\ReadOnlyPromotedPropertyA;
74+
use Symfony\Component\ObjectMapper\Tests\Fixtures\ReadOnlyPromotedProperty\ReadOnlyPromotedPropertyAMapped;
75+
use Symfony\Component\ObjectMapper\Tests\Fixtures\ReadOnlyPromotedProperty\ReadOnlyPromotedPropertyB;
76+
use Symfony\Component\ObjectMapper\Tests\Fixtures\ReadOnlyPromotedProperty\ReadOnlyPromotedPropertyBMapped;
7377
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\AB;
7478
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\Dto;
7579
use Symfony\Component\ObjectMapper\Tests\Fixtures\ServiceLoadedValue\LoadedValueService;
@@ -629,4 +633,22 @@ public function testMapEmbeddedProperties()
629633
$this->assertSame('12345', $user->address->zipcode);
630634
$this->assertSame('Test City', $user->address->city);
631635
}
636+
637+
public function testBugReportLazyLoadingPromotedReadonlyProperty()
638+
{
639+
$source = new ReadOnlyPromotedPropertyA(
640+
b: new ReadOnlyPromotedPropertyB(
641+
var2: 'bar',
642+
),
643+
var1: 'foo',
644+
);
645+
646+
$mapper = new ObjectMapper();
647+
$out = $mapper->map($source);
648+
649+
$this->assertInstanceOf(ReadOnlyPromotedPropertyAMapped::class, $out);
650+
$this->assertInstanceOf(ReadOnlyPromotedPropertyBMapped::class, $out->b);
651+
$this->assertSame('foo', $out->var1);
652+
$this->assertSame('bar', $out->b->var2);
653+
}
632654
}

0 commit comments

Comments
 (0)