Skip to content

[ObjectMapper] handle non existing property errors #60856

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

Open
wants to merge 2 commits into
base: 7.3
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <soyuka@gmail.com>
*/
class NoSuchPropertyException extends MappingException
{
}
16 changes: 15 additions & 1 deletion src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
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;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException as PropertyAccessorNoSuchPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

/**
Expand Down Expand Up @@ -167,7 +169,19 @@ 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) {
try {
return $this->propertyAccessor->getValue($source, $propertyName);
} catch (PropertyAccessorNoSuchPropertyException $e) {
throw new NoSuchPropertyException($e->getMessage(), $e->getCode(), $e);
}
}

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)));
}

return $source->{$propertyName};
}

private function getSourceValue(object $source, object $target, mixed $value, \SplObjectStorage $objectMap, ?Mapping $mapping = null): mixed
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/ObjectMapper/ObjectMapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

use Symfony\Component\ObjectMapper\Attribute\Map;

class TargetDto
{
public function __construct(
public string $id,
#[Map(source: 'optional', if: [self::class, 'isDefined'])]
public ?string $optional = null,
) {
}

public static function isDefined($source): bool
{
return isset($source);
}
}
34 changes: 34 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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;
Expand All @@ -28,6 +29,7 @@
use Symfony\Component\ObjectMapper\Tests\Fixtures\DeeperRecursion\RecursiveDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\DeeperRecursion\Relation;
use Symfony\Component\ObjectMapper\Tests\Fixtures\DeeperRecursion\RelationDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultValueStdClass\TargetDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\TargetUser;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\User;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\UserProfile;
Expand Down Expand Up @@ -236,8 +238,17 @@ public function testSourceOnly()
$mapped = $mapper->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($key): bool
{
return 'name' === $key;
}

public function __get(string $key): string
{
return match ($key) {
Expand Down Expand Up @@ -303,4 +314,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->assertSame('abc', $b->id);
$this->assertNull($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->assertSame('abc', $b->id);
$this->assertNull($b->optional);
}
}
Loading
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