Thanks to visit codestin.com
Credit goes to github.com

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 1 commit 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 <[email protected]>
*
* 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 <[email protected]>
*/
class NoSuchPropertyException extends MappingException
{
}
11 changes: 10 additions & 1 deletion src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a huge fan of testing for the __isset magic method but if we don't add this its hard to support magic getters properly, maybe we should not support them in the first place?

Comments welcome.

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
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);
}
}
33 changes: 33 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,16 @@ 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(string $key): bool {
return $key === 'name';
}

public function __get(string $key): string
{
return match ($key) {
Expand Down Expand Up @@ -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);
}
}
Loading