Skip to content

[Serializer] Use object class resolver when extracting attributes #30711

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

Merged
Merged
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
Expand Up @@ -32,6 +32,8 @@ class ObjectNormalizer extends AbstractObjectNormalizer

private $discriminatorCache = [];

private $objectClassResolver;

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [])
{
if (!\class_exists(PropertyAccess::class)) {
Expand All @@ -41,6 +43,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext);

$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
$this->objectClassResolver = $objectClassResolver;
}

/**
Expand All @@ -60,7 +63,9 @@ protected function extractAttributes($object, $format = null, array $context = [
$attributes = [];

// methods
$reflClass = new \ReflectionClass($object);
$class = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object);
$reflClass = new \ReflectionClass($class);

foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
if (
0 !== $reflMethod->getNumberOfRequiredParameters() ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,34 @@ public function denormalize($propertyName, string $class = null, string $format
$normalizer = new ObjectNormalizer(null, $nameConverter);
$this->assertArrayHasKey('foo-Symfony\Component\Serializer\Tests\Normalizer\ObjectDummy-json-bar', $normalizer->normalize(new ObjectDummy(), 'json', ['foo' => 'bar']));
}

public function testObjectClassResolver()
{
$classResolver = function ($object) {
return ObjectDummy::class;
};

$normalizer = new ObjectNormalizer(null, null, null, null, null, $classResolver);

$obj = new ProxyObjectDummy();
$obj->setFoo('foo');
$obj->bar = 'bar';
$obj->setBaz(true);
$obj->setCamelCase('camelcase');
$obj->unwantedProperty = 'notwanted';

$this->assertEquals(
[
'foo' => 'foo',
'bar' => 'bar',
'baz' => true,
'fooBar' => 'foobar',
'camelCase' => 'camelcase',
'object' => null,
],
$normalizer->normalize($obj, 'any')
);
}
}

class ObjectDummy
Expand Down Expand Up @@ -1064,6 +1092,11 @@ public function getObject()
}
}

class ProxyObjectDummy extends ObjectDummy
{
public $unwantedProperty;
}

class ObjectConstructorDummy
{
protected $foo;
Expand Down