Commit f5a5104
committed
bug symfony#58255 [Serializer] Fix
This PR was submitted for the 7.1 branch but it was squashed and merged into the 6.4 branch instead.
Discussion
----------
[Serializer] Fix `ObjectNormalizer` gives warnings on normalizing with public static property
| Q | A |
|---------------|-------------|
| Branch? | 6.4 |
| Bug fix? | yes |
| New feature? | no |
| Deprecations? | no |
| Issues | Fix symfony#58221 |
| License | MIT |
The error message has been occurring since version 6.4.11/7.1.2. If the condition is changed to version >=7.1.2, the error message no longer occurs.
The error is thrown with the following ObjectNormalizer configuration:
```php
$class = new class {
public static string $foo = "hallo";
};
$normalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()));
$normalizer->normalize($class);
```
`(\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute))` is true and therefore the entire condition is true.
I moved the condition into a method to improve readability and reduce complexity.
All serializer tests are successful.
### Condition
For better readability here with breaks.
**7.1.1**
https://github.com/symfony/symfony/blob/v7.1.1/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php#L180
```php
if (!isset(self::$isReadableCache[$class.$attribute])) {
self::$isReadableCache[$class.$attribute] =
$this->propertyInfoExtractor->isReadable($class, $attribute)
|| $this->hasAttributeAccessorMethod($class, $attribute);
}
```
**>=7.2.2**
https://github.com/symfony/symfony/blob/v7.1.2/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php#L180
```php
if (!isset(self::$isReadableCache[$class.$attribute])) {
self::$isReadableCache[$class.$attribute] =
(\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute))
|| $this->propertyInfoExtractor->isReadable($class, $attribute)
|| $this->hasAttributeAccessorMethod($class, $attribute);
}
```
**Fixed condition**
```php
if (!isset(self::$isReadableCache[$class.$attribute])) {
self::$isReadableCache[$class.$attribute] =
$this->propertyInfoExtractor->isReadable($class, $attribute)
|| $this->hasAttributeAccessorMethod($class, $attribute)
|| (\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute));
}
```
Commits
-------
bcc38d9 [Serializer] Fix `ObjectNormalizer` gives warnings on normalizing with public static propertyObjectNormalizer gives warnings on normalizing with public static property (André Laugks)File tree
2 files changed
+11
-1
lines changed- src/Symfony/Component/Serializer
- Normalizer
- Tests/Normalizer
2 files changed
+11
-1
lines changedLines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
197 | 197 | | |
198 | 198 | | |
199 | 199 | | |
200 | | - | |
| 200 | + | |
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
927 | 927 | | |
928 | 928 | | |
929 | 929 | | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
930 | 940 | | |
931 | 941 | | |
932 | 942 | | |
| |||
0 commit comments