Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ObjectNormalizer extends AbstractObjectNormalizer

protected $propertyAccessor;
protected $propertyInfoExtractor;
private $writeInfoExtractor;

private $objectClassResolver;

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

$this->propertyInfoExtractor = $propertyInfoExtractor ?: new ReflectionExtractor();
$this->writeInfoExtractor = new ReflectionExtractor();
}

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

return $this->propertyInfoExtractor->isWritable($class, $attribute)
|| ($writeInfo = $this->propertyInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType();
if ($this->propertyInfoExtractor->isWritable($class, $attribute)) {
return true;
}

if (($writeInfo = $this->writeInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType()) {
return true;
}

return false;
}

private function hasAttributeAccessorMethod(string $class, string $attribute): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,22 @@ public function testConstructorWithObjectDenormalize()
$this->assertEquals('bar', $obj->bar);
}

public function testConstructorWithObjectDenormalizeUsingPropertyInfoExtractor()
{
$serializer = $this->createMock(ObjectSerializerNormalizer::class);
$normalizer = new ObjectNormalizer(null, null, null, null, null, null, [], new PropertyInfoExtractor());
$normalizer->setSerializer($serializer);

$data = new \stdClass();
$data->foo = 'foo';
$data->bar = 'bar';
$data->baz = true;
$data->fooBar = 'foobar';
$obj = $normalizer->denormalize($data, ObjectConstructorDummy::class, 'any');
$this->assertEquals('foo', $obj->getFoo());
$this->assertEquals('bar', $obj->bar);
}

public function testConstructorWithObjectTypeHintDenormalize()
{
$data = [
Expand Down