diff --git a/src/Symfony/Component/ObjectMapper/ObjectMapper.php b/src/Symfony/Component/ObjectMapper/ObjectMapper.php index 7624a05f7bfe0..d78bc3ce8d216 100644 --- a/src/Symfony/Component/ObjectMapper/ObjectMapper.php +++ b/src/Symfony/Component/ObjectMapper/ObjectMapper.php @@ -122,12 +122,12 @@ public function map(object $source, object|string|null $target = null): object $sourcePropertyName = $mapping->source; } - $value = $this->getRawValue($source, $sourcePropertyName); - if (($if = $mapping->if) && ($fn = $this->getCallable($if, $this->conditionCallableLocator)) && !$this->call($fn, $value, $source, $mappedTarget)) { + if (false === $if = $mapping->if) { continue; } - if (false === $if) { + $value = $this->getRawValue($source, $sourcePropertyName); + if ($if && ($fn = $this->getCallable($if, $this->conditionCallableLocator)) && !$this->call($fn, $value, $source, $mappedTarget)) { continue; } diff --git a/src/Symfony/Component/ObjectMapper/Tests/Fixtures/HydrateObject/SourceOnly.php b/src/Symfony/Component/ObjectMapper/Tests/Fixtures/HydrateObject/SourceOnly.php index 9e3127b80d965..c062427c6e8d0 100644 --- a/src/Symfony/Component/ObjectMapper/Tests/Fixtures/HydrateObject/SourceOnly.php +++ b/src/Symfony/Component/ObjectMapper/Tests/Fixtures/HydrateObject/SourceOnly.php @@ -15,7 +15,9 @@ class SourceOnly { - public function __construct(#[Map(source: 'name')] public string $mappedName) - { + public function __construct( + #[Map(source: 'name')] public string $mappedName, + #[Map(if: false)] public ?string $mappedDescription = null + ) { } }