Skip to content

Commit 1b855d8

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

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ObjectNormalizer extends AbstractObjectNormalizer
3838

3939
protected $propertyAccessor;
4040
protected $propertyInfoExtractor;
41+
private $writeInfoExtractor;
4142

4243
private readonly \Closure $objectClassResolver;
4344

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

5455
$this->objectClassResolver = ($objectClassResolver ?? static fn ($class) => \is_object($class) ? $class::class : $class)(...);
55-
$this->propertyInfoExtractor = $propertyInfoExtractor ?: new ReflectionExtractor();
56+
$this->propertyInfoExtractor = $propertyInfoExtractor;
57+
$this->writeInfoExtractor = new ReflectionExtractor();
5658
}
5759

5860
public function getSupportedTypes(?string $format): array
@@ -189,8 +191,15 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
189191
return $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
190192
}
191193

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

196205
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