From 88a4a597060d9ba4f9cb03fab1ecf6084123973b Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 20 Jun 2025 15:53:18 +0200 Subject: [PATCH 1/2] [ObjectMapper] handle non existing property errors --- .../Exception/NoSuchPropertyException.php | 21 ++++++++++++ .../Component/ObjectMapper/ObjectMapper.php | 11 ++++++- .../DefaultValueStdClass/TargetDto.php | 20 +++++++++++ .../ObjectMapper/Tests/ObjectMapperTest.php | 33 +++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/ObjectMapper/Exception/NoSuchPropertyException.php create mode 100644 src/Symfony/Component/ObjectMapper/Tests/Fixtures/DefaultValueStdClass/TargetDto.php diff --git a/src/Symfony/Component/ObjectMapper/Exception/NoSuchPropertyException.php b/src/Symfony/Component/ObjectMapper/Exception/NoSuchPropertyException.php new file mode 100644 index 0000000000000..3b5df303dcc1f --- /dev/null +++ b/src/Symfony/Component/ObjectMapper/Exception/NoSuchPropertyException.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\ObjectMapper\Exception; + +/** + * Thrown when a property cannot be found. + * + * @author Antoine Bluchet + */ +class NoSuchPropertyException extends MappingException +{ +} diff --git a/src/Symfony/Component/ObjectMapper/ObjectMapper.php b/src/Symfony/Component/ObjectMapper/ObjectMapper.php index 69f02fb7f1160..dc493c0e6a4c8 100644 --- a/src/Symfony/Component/ObjectMapper/ObjectMapper.php +++ b/src/Symfony/Component/ObjectMapper/ObjectMapper.php @@ -14,6 +14,7 @@ use Psr\Container\ContainerInterface; use Symfony\Component\ObjectMapper\Exception\MappingException; use Symfony\Component\ObjectMapper\Exception\MappingTransformException; +use Symfony\Component\ObjectMapper\Exception\NoSuchPropertyException; use Symfony\Component\ObjectMapper\Metadata\Mapping; use Symfony\Component\ObjectMapper\Metadata\ObjectMapperMetadataFactoryInterface; use Symfony\Component\ObjectMapper\Metadata\ReflectionObjectMapperMetadataFactory; @@ -167,7 +168,15 @@ public function map(object $source, object|string|null $target = null): object private function getRawValue(object $source, string $propertyName): mixed { - return $this->propertyAccessor ? $this->propertyAccessor->getValue($source, $propertyName) : $source->{$propertyName}; + if ($this->propertyAccessor) { + return $this->propertyAccessor->getValue($source, $propertyName); + } + + if (!property_exists($source, $propertyName) && !(method_exists($source, '__isset') && true === $source->__isset($propertyName))) { + throw new NoSuchPropertyException(sprintf('The property "%s" does not exist on "%s".', $propertyName, get_debug_type($source))); + } + + return $source->{$propertyName}; } private function getSourceValue(object $source, object $target, mixed $value, \SplObjectStorage $objectMap, ?Mapping $mapping = null): mixed diff --git a/src/Symfony/Component/ObjectMapper/Tests/Fixtures/DefaultValueStdClass/TargetDto.php b/src/Symfony/Component/ObjectMapper/Tests/Fixtures/DefaultValueStdClass/TargetDto.php new file mode 100644 index 0000000000000..d4aa289184d50 --- /dev/null +++ b/src/Symfony/Component/ObjectMapper/Tests/Fixtures/DefaultValueStdClass/TargetDto.php @@ -0,0 +1,20 @@ +map($a, SourceOnly::class); $this->assertInstanceOf(SourceOnly::class, $mapped); $this->assertSame('test', $mapped->mappedName); + } + public function testSourceOnlyWithMagicMethods() + { + $mapper = new ObjectMapper(); $a = new class { + public function __isset(string $key): bool { + return $key === 'name'; + } + public function __get(string $key): string { return match ($key) { @@ -303,4 +313,27 @@ public function testMultipleTargetMapProperty() $this->assertEquals('donotmap', $c->foo); $this->assertEquals('foo', $c->doesNotExistInTargetB); } + + public function testDefaultValueStdClass() + { + $this->expectException(NoSuchPropertyException::class); + $u = new \stdClass(); + $u->id = 'abc'; + $mapper = new ObjectMapper(); + $b = $mapper->map($u, TargetDto::class); + $this->assertInstanceOf(TargetDto::class, $b); + $this->assertEquals('abc', $b->id); + $this->assertEquals(null, $b->optional); + } + + public function testDefaultValueStdClassWithPropertyInfo() + { + $u = new \stdClass(); + $u->id = 'abc'; + $mapper = new ObjectMapper(propertyAccessor: PropertyAccess::createPropertyAccessorBuilder()->disableExceptionOnInvalidPropertyPath()->getPropertyAccessor()); + $b = $mapper->map($u, TargetDto::class); + $this->assertInstanceOf(TargetDto::class, $b); + $this->assertEquals('abc', $b->id); + $this->assertEquals(null, $b->optional); + } } From 1391eaadb48d8e13279b712d004ed51275b201ba Mon Sep 17 00:00:00 2001 From: soyuka Date: Thu, 26 Jun 2025 14:50:34 +0200 Subject: [PATCH 2/2] review --- src/Symfony/Component/ObjectMapper/ObjectMapper.php | 9 +++++++-- .../ObjectMapper/ObjectMapperInterface.php | 2 ++ .../Fixtures/DefaultValueStdClass/TargetDto.php | 6 +++--- .../ObjectMapper/Tests/ObjectMapperTest.php | 13 +++++++------ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/ObjectMapper/ObjectMapper.php b/src/Symfony/Component/ObjectMapper/ObjectMapper.php index dc493c0e6a4c8..654ae047c59f3 100644 --- a/src/Symfony/Component/ObjectMapper/ObjectMapper.php +++ b/src/Symfony/Component/ObjectMapper/ObjectMapper.php @@ -18,6 +18,7 @@ use Symfony\Component\ObjectMapper\Metadata\Mapping; use Symfony\Component\ObjectMapper\Metadata\ObjectMapperMetadataFactoryInterface; use Symfony\Component\ObjectMapper\Metadata\ReflectionObjectMapperMetadataFactory; +use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException as PropertyAccessorNoSuchPropertyException; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; /** @@ -169,10 +170,14 @@ public function map(object $source, object|string|null $target = null): object private function getRawValue(object $source, string $propertyName): mixed { if ($this->propertyAccessor) { - return $this->propertyAccessor->getValue($source, $propertyName); + try { + return $this->propertyAccessor->getValue($source, $propertyName); + } catch (PropertyAccessorNoSuchPropertyException $e) { + throw new NoSuchPropertyException($e->getMessage(), $e->getCode(), $e); + } } - if (!property_exists($source, $propertyName) && !(method_exists($source, '__isset') && true === $source->__isset($propertyName))) { + if (!property_exists($source, $propertyName) && !isset($source->{$propertyName})) { throw new NoSuchPropertyException(sprintf('The property "%s" does not exist on "%s".', $propertyName, get_debug_type($source))); } diff --git a/src/Symfony/Component/ObjectMapper/ObjectMapperInterface.php b/src/Symfony/Component/ObjectMapper/ObjectMapperInterface.php index 0df5a0fbfddbd..9eb3bc5d5af0b 100644 --- a/src/Symfony/Component/ObjectMapper/ObjectMapperInterface.php +++ b/src/Symfony/Component/ObjectMapper/ObjectMapperInterface.php @@ -13,6 +13,7 @@ use Symfony\Component\ObjectMapper\Exception\MappingException; use Symfony\Component\ObjectMapper\Exception\MappingTransformException; +use Symfony\Component\ObjectMapper\Exception\NoSuchPropertyException; /** * Object to object mapper. @@ -33,6 +34,7 @@ interface ObjectMapperInterface * * @throws MappingException When the mapping configuration is wrong * @throws MappingTransformException When a transformation on an object does not return an object + * @throws NoSuchPropertyException When a property does not exist */ public function map(object $source, object|string|null $target = null): object; } diff --git a/src/Symfony/Component/ObjectMapper/Tests/Fixtures/DefaultValueStdClass/TargetDto.php b/src/Symfony/Component/ObjectMapper/Tests/Fixtures/DefaultValueStdClass/TargetDto.php index d4aa289184d50..e595c103a4e35 100644 --- a/src/Symfony/Component/ObjectMapper/Tests/Fixtures/DefaultValueStdClass/TargetDto.php +++ b/src/Symfony/Component/ObjectMapper/Tests/Fixtures/DefaultValueStdClass/TargetDto.php @@ -10,11 +10,11 @@ public function __construct( public string $id, #[Map(source: 'optional', if: [self::class, 'isDefined'])] public ?string $optional = null, - ) - { + ) { } - public static function isDefined($source): bool { + public static function isDefined($source): bool + { return isset($source); } } diff --git a/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php b/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php index 9e0a61104420b..3bde43d072a6c 100644 --- a/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php +++ b/src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php @@ -244,8 +244,9 @@ public function testSourceOnlyWithMagicMethods() { $mapper = new ObjectMapper(); $a = new class { - public function __isset(string $key): bool { - return $key === 'name'; + public function __isset($key): bool + { + return 'name' === $key; } public function __get(string $key): string @@ -322,8 +323,8 @@ public function testDefaultValueStdClass() $mapper = new ObjectMapper(); $b = $mapper->map($u, TargetDto::class); $this->assertInstanceOf(TargetDto::class, $b); - $this->assertEquals('abc', $b->id); - $this->assertEquals(null, $b->optional); + $this->assertSame('abc', $b->id); + $this->assertNull($b->optional); } public function testDefaultValueStdClassWithPropertyInfo() @@ -333,7 +334,7 @@ public function testDefaultValueStdClassWithPropertyInfo() $mapper = new ObjectMapper(propertyAccessor: PropertyAccess::createPropertyAccessorBuilder()->disableExceptionOnInvalidPropertyPath()->getPropertyAccessor()); $b = $mapper->map($u, TargetDto::class); $this->assertInstanceOf(TargetDto::class, $b); - $this->assertEquals('abc', $b->id); - $this->assertEquals(null, $b->optional); + $this->assertSame('abc', $b->id); + $this->assertNull($b->optional); } } 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