From b7bd80ff1a711e82462b3b1bf3d0fa7deb0cd9ef Mon Sep 17 00:00:00 2001 From: soyuka Date: Thu, 24 Jul 2025 21:43:02 +0200 Subject: [PATCH] [ObjectMapper] skip reading uninitialized values --- .../Component/ObjectMapper/ObjectMapper.php | 5 +++ .../Fixtures/PartialInput/FinalInput.php | 11 +++++ .../Fixtures/PartialInput/PartialInput.php | 14 ++++++ .../ObjectMapper/Tests/ObjectMapperTest.php | 45 +++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 src/Symfony/Component/ObjectMapper/Tests/Fixtures/PartialInput/FinalInput.php create mode 100644 src/Symfony/Component/ObjectMapper/Tests/Fixtures/PartialInput/PartialInput.php diff --git a/src/Symfony/Component/ObjectMapper/ObjectMapper.php b/src/Symfony/Component/ObjectMapper/ObjectMapper.php index 87f0a821a71d..64840948aa45 100644 --- a/src/Symfony/Component/ObjectMapper/ObjectMapper.php +++ b/src/Symfony/Component/ObjectMapper/ObjectMapper.php @@ -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); } diff --git a/src/Symfony/Component/ObjectMapper/Tests/Fixtures/PartialInput/FinalInput.php b/src/Symfony/Component/ObjectMapper/Tests/Fixtures/PartialInput/FinalInput.php new file mode 100644 index 000000000000..2aeab4c5bcab --- /dev/null +++ b/src/Symfony/Component/ObjectMapper/Tests/Fixtures/PartialInput/FinalInput.php @@ -0,0 +1,11 @@ +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]; + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy