Skip to content

Commit 5a40ddf

Browse files
committed
[ObjectMapper] skip reading uninitialized values
1 parent ba445f4 commit 5a40ddf

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

src/Symfony/Component/ObjectMapper/ObjectMapper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ public function map(object $source, object|string|null $target = null): object
144144
}
145145

146146
if (!$mappings && $targetRefl->hasProperty($propertyName)) {
147+
$sourceProperty = $refl->getProperty($propertyName);
148+
if ($refl->isInstance($source) && !$sourceProperty->isInitialized($source)) {
149+
continue;
150+
}
151+
147152
$value = $this->getSourceValue($source, $mappedTarget, $this->getRawValue($source, $propertyName), $this->objectMap);
148153
$this->storeValue($propertyName, $mapToProperties, $ctorArguments, $value);
149154
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\PartialInput;
4+
5+
class FinalInput
6+
{
7+
public string $uuid;
8+
public string $name;
9+
public ?string $email = null;
10+
public ?string $website = null;
11+
}
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\PartialInput;
4+
5+
use Symfony\Component\ObjectMapper\Attribute\Map;
6+
7+
#[Map(target: FinalInput::class)]
8+
class PartialInput
9+
{
10+
public string $uuid;
11+
public string $name;
12+
public ?string $email;
13+
public ?string $website;
14+
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\A as MultipleTargetsA;
5555
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\C as MultipleTargetsC;
5656
use Symfony\Component\ObjectMapper\Tests\Fixtures\MyProxy;
57+
use Symfony\Component\ObjectMapper\Tests\Fixtures\PartialInput\FinalInput;
58+
use Symfony\Component\ObjectMapper\Tests\Fixtures\PartialInput\PartialInput;
5759
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Source as PromotedConstructorSource;
5860
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Target as PromotedConstructorTarget;
5961
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\AB;
@@ -402,4 +404,46 @@ public function testMapInitializesNativePhp84LazyObject()
402404
$this->assertSame('test', $d->name);
403405
$this->assertTrue($initialized);
404406
}
407+
408+
/**
409+
* @dataProvider validPartialInputProvider
410+
*/
411+
public function testMapPartially(PartialInput $actual, FinalInput $expected)
412+
{
413+
$mapper = new ObjectMapper();
414+
$this->assertEquals($expected, $mapper->map($actual));
415+
}
416+
417+
public static function validPartialInputProvider(): iterable {
418+
$p = new PartialInput;
419+
$p->uuid = '6a9eb6dd-c4dc-4746-bb99-f6bad716acb2';
420+
$p->website = 'https://updated.website.com';
421+
422+
$f = new FinalInput();
423+
$f->uuid = $p->uuid;
424+
$f->website = $p->website;
425+
426+
yield [$p, $f];
427+
428+
$p = new PartialInput;
429+
$p->uuid = '6a9eb6dd-c4dc-4746-bb99-f6bad716acb2';
430+
$p->website = null;
431+
432+
$f = new FinalInput();
433+
$f->uuid = $p->uuid;
434+
435+
yield [$p, $f];
436+
437+
$p = new PartialInput;
438+
$p->uuid = '6a9eb6dd-c4dc-4746-bb99-f6bad716acb2';
439+
$p->website = 'https://updated.website.com';
440+
$p->email = 'updated@email.com';
441+
442+
$f = new FinalInput();
443+
$f->uuid = $p->uuid;
444+
$f->website = $p->website;
445+
$f->email = $p->email;
446+
447+
yield [$p, $f];
448+
}
405449
}

0 commit comments

Comments
 (0)
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