8000 [ObjectMapper] Fix class-level transformation has wrong source class by mttsch · Pull Request #60831 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[ObjectMapper] Fix class-level transformation has wrong source class #60831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function map(object $source, object|string|null $target = null): object

$mappedTarget = $mappingToObject ? $target : $targetRefl->newInstanceWithoutConstructor();
if ($map && $map->transform) {
$mappedTarget = $this->applyTransforms($map, $mappedTarget, $mappedTarget, null);
$mappedTarget = $this->applyTransforms($map, $mappedTarget, $source, null);

if (!\is_object($mappedTarget)) {
throw new MappingTransformException(\sprintf('Cannot map "%s" to a non-object target of type "%s".', get_debug_type($source), get_debug_type($mappedTarget)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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\InstanceCallbackWithArguments;

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: B::class, transform: [B::class, 'newInstance'])]
class A
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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\InstanceCallbackWithArguments;

class B
{
public mixed $transformValue;
public object $transformSource;

public static function newInstance(mixed $value, object $source): self
{
$b = new self();
$b->transformValue = $value;
$b->transformSource = $source;

return $b;
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use Symfony\Component\ObjectMapper\Tests\Fixtures\HydrateObject\SourceOnly;
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;
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallbackWithArguments\B as InstanceCallbackWithArgumentsB;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\AToBMapper;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\MapStructMapperMetadataFactory;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\Source;
Expand Down Expand Up @@ -155,6 +157,16 @@ public function testMapToWithInstanceHook()
$this->assertSame($b->name, 'test');
}

public function testMapToWithInstanceHookWithArguments()
{
$a = new InstanceCallbackWithArgumentsA();
$mapper = new ObjectMapper();
$b = $mapper->map($a);
$this->assertInstanceOf(InstanceCallbackWithArgumentsB::class, $b);
$this->assertSame($a, $b->transformSource);
$this->assertInstanceOf(InstanceCallbackWithArgumentsB::class, $b->transformValue);
}

public function testMapStruct()
{
$a = new Source('a', 'b', 'c');
Expand Down
Loading
0