Skip to content

[ObjectMapper] skip reading uninitialized values #61233

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
Jul 26, 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
5 changes: 5 additions & 0 deletions src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public function map(object $source, object|string|null $target = null): object
}

if (!$mappings && $targetRefl->hasProperty($propertyName)) {
$sourceProperty = $refl->getProperty($propertyName);
if ($refl->isInstance($source) && !$sourceProperty->isInitialized($source)) {
continue;
}

$value = $this->getSourceValue($source, $mappedTarget, $this->getRawValue($source, $propertyName), $this->objectMap);
$this->storeValue($propertyName, $mapToProperties, $ctorArguments, $value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\PartialInput;

class FinalInput
{
public string $uuid;
public string $name;
public ?string $email = null;
public ?string $website = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\PartialInput;

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: FinalInput::class)]
class PartialInput
{
public string $uuid;
public string $name;
public ?string $email;
public ?string $website;
}
45 changes: 45 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\A as MultipleTargetsA;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\C as MultipleTargetsC;
use Symfony\Component\ObjectMapper\Tests\Fixtures\MyProxy;
use Symfony\Component\ObjectMapper\Tests\Fixtures\PartialInput\FinalInput;
use Symfony\Component\ObjectMapper\Tests\Fixtures\PartialInput\PartialInput;
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Source as PromotedConstructorSource;
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Target as PromotedConstructorTarget;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\AB;
Expand Down Expand Up @@ -402,4 +404,47 @@ public function testMapInitializesNativePhp84LazyObject()
$this->assertSame('test', $d->name);
$this->assertTrue($initialized);
}

/**
* @dataProvider validPartialInputProvider
*/
public function testMapPartially(PartialInput $actual, FinalInput $expected)
{
$mapper = new ObjectMapper();
$this->assertEquals($expected, $mapper->map($actual));
}

public static function validPartialInputProvider(): iterable
{
$p = new PartialInput();
$p->uuid = '6a9eb6dd-c4dc-4746-bb99-f6bad716acb2';
$p->website = 'https://updated.website.com';

$f = new FinalInput();
$f->uuid = $p->uuid;
$f->website = $p->website;

yield [$p, $f];

$p = new PartialInput();
$p->uuid = '6a9eb6dd-c4dc-4746-bb99-f6bad716acb2';
$p->website = null;

$f = new FinalInput();
$f->uuid = $p->uuid;

yield [$p, $f];

$p = new PartialInput();
$p->uuid = '6a9eb6dd-c4dc-4746-bb99-f6bad716acb2';
$p->website = 'https://updated.website.com';
$p->email = 'updated@email.com';

$f = new FinalInput();
$f->uuid = $p->uuid;
$f->website = $p->website;
$f->email = $p->email;

yield [$p, $f];
}
}
Loading