Description
Symfony version(s) affected: 5.2.0
Description
Attempting to serialize an array which casted to object, if any of these properties contains "." (dot)
PropertyAccess resolvePath(getPropertyPath) behaves weird
symfony/src/Symfony/Component/PropertyAccess/PropertyPath.php
Lines 88 to 90 in 0758e59
How to reproduce
protected function execute(InputInterface $input, OutputInterface $output): int
{
$arr = [
"No. of Rows" => "somedata"
];
$obj = (object) $arr;
dump($obj->{"No. of Rows"}); // some data output
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
->getPropertyAccessor();
// instead of throwing an exception the following code returns null
// in case if we decide to use disableExceptionOnInvalidPropertyPath
$value = $propertyAccessor->getValue($obj, 'No. of Rows'); //exception
dump($value);
return Command::SUCCESS;
}
Exception will be thrown, which we can handle by using disableExceptionOnInvalidPropertyPath
if we are using PropertyAccessor directly,
but not in case when we are using ObjectNormalizer / Serializer e.g $this->json($object)
propertyPath
resolve it as two elements
elements = {array} [2]
0 = "No"
1 = " Of Rows"
and of course an attempt to access the object with any of these properties
e.g
$obj->{"No"}
$obj->{" Of Rows"}
makes no sense, cause $zval does not know anything about these properties, but knows about No. of Rows
Possible Solution
Process this case correctly :D
Additional context
The example above was reproduced with simple test command in CLI environment