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

Skip to content

Commit 64f020f

Browse files
committed
separate the property info and write info extractors
1 parent f77f78e commit 64f020f

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ObjectNormalizer extends AbstractObjectNormalizer
3636

3737
protected $propertyAccessor;
3838
protected $propertyInfoExtractor;
39+
private $writeInfoExtractor;
3940

4041
private $objectClassResolver;
4142

@@ -54,6 +55,7 @@ public function __construct(?ClassMetadataFactoryInterface $classMetadataFactory
5455
};
5556

5657
$this->propertyInfoExtractor = $propertyInfoExtractor ?: new ReflectionExtractor();
58+
$this->writeInfoExtractor = new ReflectionExtractor();
5759
}
5860

5961
/**
@@ -195,8 +197,15 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
195197
return $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
196198
}
197199

198-
return $this->propertyInfoExtractor->isWritable($class, $attribute)
199-
|| ($writeInfo = $this->propertyInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType();
200+
if ($this->propertyInfoExtractor->isWritable($class, $attribute)) {
201+
return true;
202+
}
203+
204+
if (($writeInfo = $this->writeInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType()) {
205+
return true;
206+
}
207+
208+
return false;
200209
}
201210

202211
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
@@ -274,6 +274,22 @@ public function testConstructorWithObjectDenormalize()
274274
$this->assertEquals('bar', $obj->bar);
275275
}
276276

277+
public function testConstructorWithObjectDenormalizeUsingPropertyInfoExtractor()
278+
{
279+
$serializer = $this->createMock(ObjectSerializerNormalizer::class);
280+
$normalizer = new ObjectNormalizer(null, null, null, null, null, null, [], new PropertyInfoExtractor());
281+
$normalizer->setSerializer($serializer);
282+
283+
$data = new \stdClass();
284+
$data->foo = 'foo';
285+
$data->bar = 'bar';
286+
$data->baz = true;
287+
$data->fooBar = 'foobar';
288+
$obj = $normalizer->denormalize($data, ObjectConstructorDummy::class, 'any');
289+
$this->assertEquals('foo', $obj->getFoo());
290+
$this->assertEquals('bar', $obj->bar);
291+
}
292+
277293
public function testConstructorWithObjectTypeHintDenormalize()
278294
{
279295
$data = [

0 commit comments

Comments
 (0)