Skip to content

Commit f5a5104

Browse files
bug symfony#58255 [Serializer] Fix ObjectNormalizer gives warnings on normalizing with public static property (André Laugks)
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 property
2 parents 2de4387 + bcc38d9 commit f5a5104

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
197197

198198
if ($context['_read_attributes'] ?? true) {
199199
if (!isset(self::$isReadableCache[$class.$attribute])) {
200-
self::$isReadableCache[$class.$attribute] = (\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute)) || $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
200+
self::$isReadableCache[$class.$attribute] = $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute) || (\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute));
201201
}
202202

203203
return self::$isReadableCache[$class.$attribute];

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,16 @@ public function testDenormalizeWithPropertyPath()
927927

928928
$this->assertEquals($expected, $obj);
929929
}
930+
931+
public function testObjectNormalizerWithAttributeLoaderAndObjectHasStaticProperty()
932+
{
933+
$class = new class {
934+
public static string $foo;
935+
};
936+
937+
$normalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()));
938+
$this->assertSame([], $normalizer->normalize($class));
939+
}
930940
}
931941

932942
class ProxyObjectDummy extends ObjectDummy

0 commit comments

Comments
 (0)