Skip to content

Commit 52126b2

Browse files
committed
separate the property info and write info extractors
1 parent 27bf4a8 commit 52126b2

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\PropertyAccess\PropertyAccess;
1616
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1717
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
18+
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
1819
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
1920
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
2021
use Symfony\Component\PropertyInfo\PropertyWriteInfo;
@@ -38,6 +39,7 @@ class ObjectNormalizer extends AbstractObjectNormalizer
3839

3940
protected $propertyAccessor;
4041
protected $propertyInfoExtractor;
42+
private $writeInfoExtractor;
4143

4244
private readonly \Closure $objectClassResolver;
4345

@@ -52,7 +54,8 @@ public function __construct(?ClassMetadataFactoryInterface $classMetadataFactory
5254
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
5355

5456
$this->objectClassResolver = ($objectClassResolver ?? static fn ($class) => \is_object($class) ? $class::class : $class)(...);
55-
$this->propertyInfoExtractor = $propertyInfoExtractor ?: new ReflectionExtractor();
57+
$this->propertyInfoExtractor = $propertyInfoExtractor ?? new ReflectionExtractor();
58+
$this->writeInfoExtractor = new ReflectionExtractor();
5659
}
5760

5861
public function getSupportedTypes(?string $format): array
@@ -189,8 +192,15 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
189192
return $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
190193
}
191194

192-
return $this->propertyInfoExtractor->isWritable($class, $attribute)
193-
|| ($writeInfo = $this->propertyInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType();
195+
if ($this->propertyInfoExtractor->isWritable($class, $attribute)) {
196+
return true;
197+
}
198+
199+
if (($writeInfo = $this->writeInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType()) {
200+
return true;
201+
}
202+
203+
return false;
194204
}
195205

196206
private function hasAttributeAccessorMethod(string $class, string $attribute): bool

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,22 @@ public function testConstructorWithObjectDenormalize()
266266
$this->assertEquals('bar', $obj->bar);
267267
}
268268

269+
public function testConstructorWithObjectDenormalizeUsingPropertyInfoExtractor()
270+
{
271+
$serializer = $this->createMock(ObjectSerializerNormalizer::class);
272+
$normalizer = new ObjectNormalizer(null, null, null, null, null, null, [], new PropertyInfoExtractor());
273+
$normalizer->setSerializer($serializer);
274+
275+
$data = new \stdClass();
276+
$data->foo = 'foo';
277+
$data->bar = 'bar';
278+
$data->baz = true;
279+
$data->fooBar = 'foobar';
280+
$obj = $normalizer->denormalize($data, ObjectConstructorDummy::class, 'any');
281+
$this->assertEquals('foo', $obj->getFoo());
282+
$this->assertEquals('bar', $obj->bar);
283+
}
284+
269285
public function testConstructorWithObjectTypeHintDenormalize()
270286
{
271287
$data = [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy