Skip to content

Incorrect behavior of PropertyAccessor when dynamic property contains dot #41845

Closed
@kefzce

Description

@kefzce

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

// first element is evaluated differently - no leading dot for properties
$pattern = '/^(([^\.\[]++)|\[([^\]]++)\])(.*)/';

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions