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

Skip to content

Property Access fails on properties with a dot (.) #58100

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

Closed
evs-xsarus opened this issue Aug 27, 2024 · 2 comments
Closed

Property Access fails on properties with a dot (.) #58100

evs-xsarus opened this issue Aug 27, 2024 · 2 comments

Comments

@evs-xsarus
Copy link

Symfony version(s) affected

6.4.8 and 7.1.1

Description

When serializing an object which has properties with a dot in the name, a NoSuchPropertyException is thrown.

How to reproduce

Run this on PHP 8.3

composer.json for 6.4.8

{
    "require": {
        "symfony/property-access": "^6",
        "symfony/serializer": "^6",
        "phpdocumentor/reflection-docblock": "^5.4"
    }
}

composer.json for 7.1.1

{
    "require": {
        "symfony/property-access": "^7.1",
        "symfony/serializer": "^7.1",
        "phpdocumentor/reflection-docblock": "^5.4"
    }
}

test.php

<?php
declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

$stdclass = (object) ['bankAccount.iban' => 'NL16TEST0436169118', 'bankSummary' => ''];

$reflectionExtractor   = new ReflectionExtractor();
$phpDocExtractor       = new PhpDocExtractor();
$propertyTypeExtractor = new PropertyInfoExtractor([$reflectionExtractor], [$phpDocExtractor, $reflectionExtractor], [$phpDocExtractor], [$reflectionExtractor], [$reflectionExtractor]);
$normalizers           = [
    new BackedEnumNormalizer(),
    new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d\TH:i:sP', DateTimeNormalizer::TIMEZONE_KEY => 'Z']),
    new ArrayDenormalizer(),
    new JsonSerializableNormalizer(),
    new ObjectNormalizer(
        null,
        null,
        null,
        $propertyTypeExtractor,
        null,
        null,
        [
            AbstractObjectNormalizer::SKIP_NULL_VALUES       => true,
            AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true,
        ]
    ),
];

$encoders = [
    new JsonEncoder(),
];

$serializer = new Serializer($normalizers, $encoders);
$serializer->serialize($stdclass, 'json');

Possible Solution

No response

Additional Context

Fatal error: Uncaught Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException: Can't get a way to read the property "bankAccount" in class "stdClass". in /sites/property-access/vendor/symfony/property-access/PropertyAccessor.php on line 442

Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException: Can't get a way to read the property "bankAccount" in class "stdClass". in /sites/property-access/vendor/symfony/property-access/PropertyAccessor.php on line 442

Call Stack:
0.0003 980096 1. {main}() /sites/property-access/test.php:0
0.0150 3787888 2. Symfony\Component\Serializer\Serializer->serialize($data = class stdClass { public $bankAccount.iban = 'NL16TEST0436169118'; public $bankSummary = '' }, $format = 'json', $context = ???) /sites/property-access/test.php:47
0.0150 3788264 3. Symfony\Component\Serializer\Serializer->normalize($data = class stdClass { public $bankAccount.iban = 'NL16TEST0436169118'; public $bankSummary = '' }, $format = 'json', $context = []) /sites/property-access/vendor/symfony/serializer/Serializer.php:129
0.0150 3789232 4. Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->normalize($object = class stdClass { public $bankAccount.iban = 'NL16TEST0436169118'; public $bankSummary = '' }, $format = 'json', $context = []) /sites/property-access/vendor/symfony/serializer/Serializer.php:150
0.0150 3790360 5. Symfony\Component\Serializer\Normalizer\ObjectNormalizer->getAttributeValue($object = class stdClass { public $bankAccount.iban = 'NL16TEST0436169118'; public $bankSummary = '' }, $attribute = 'bankAccount.iban', $format = 'json', $context = ['_read_attributes' => TRUE, 'cache_key' => 'ecc5fabac235bf8e878a40312bf42ca4', 'circular_reference_limit_counters' => ['00000000000000160000000000000000' => 1]]) /sites/property-access/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:194
0.0150 3790360 6. Symfony\Component\PropertyAccess\PropertyAccessor->getValue($objectOrArray = class stdClass { public $bankAccount.iban = 'NL16TEST0436169118'; public $bankSummary = '' }, $propertyPath = 'bankAccount.iban') /sites/property-access/vendor/symfony/serializer/Normalizer/ObjectNormalizer.php:134
0.0153 3821504 7. Symfony\Component\PropertyAccess\PropertyAccessor->readPropertiesUntil($zval = [0 => class stdClass { public $bankAccount.iban = 'NL16TEST0436169118'; public $bankSummary = '' }], $propertyPath = class Symfony\Component\PropertyAccess\PropertyPath { private array $elements = [0 => 'bankAccount', 1 => 'iban']; private int $length = 2; private array $isIndex = [0 => FALSE, 1 => FALSE]; private array $isNullSafe = [0 => FALSE, 1 => FALSE]; private string $pathAsString = 'bankAccount.iban' }, $lastIndex = 2, $ignoreInvalidIndices = TRUE) /sites/property-access/vendor/symfony/property-access/PropertyAccessor.php:107
0.0153 3821720 8. Symfony\Component\PropertyAccess\PropertyAccessor->readProperty($zval = [0 => class stdClass { public $bankAccount.iban = 'NL16TEST0436169118'; public $bankSummary = '' }], $property = 'bankAccount', $ignoreInvalidProperty = FALSE, $isNullSafe = FALSE) /sites/property-access/vendor/symfony/property-access/PropertyAccessor.php:309

@evs-xsarus

This comment was marked as resolved.

nicolas-grekas added a commit that referenced this issue Aug 30, 2024
…lexandre-daubois)

This PR was merged into the 5.4 branch.

Discussion
----------

[PropertyAccess] Fix handling property names with a `.`

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #58100
| License       | MIT

If the property path contains a dot, it is considered to be an access to an underlying property. However, some edge cases allow to have dots in property names, especially with `stdClass`.

Minimal reproducer:

```php
$stdclass = (object) ['bankAccount.iban' => 'NL16TEST0436169118', 'bankSummary' => ''];
$accessor = PropertyAccess::createPropertyAccessor();

dump($accessor->getValue($stdclass, 'bankAccount.iban')); // returns "NL16TEST0436169118"

$accessor->setValue($stdclass, 'bankAccount.iban', 'value');
dump($accessor->getValue($stdclass, 'bankAccount.iban')); // returns "value"
```

Commits
-------

d939a16 [PropertyAccess] Fix handling property names with a `.`
@evs-xsarus
Copy link
Author

Works fine now. Thank you for fixing this so quick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
@nicolas-grekas @derrabus @carsonbot @evs-xsarus and others