[Serializer] Fix bugs reported in b5990be491 (commitcomment-12301266)
This commit is contained in:
parent
5d841a2b03
commit
65e9f268fe
@ -291,7 +291,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
|||||||
*
|
*
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
*/
|
*/
|
||||||
protected function instantiateObject(array $data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
|
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
|
||||||
{
|
{
|
||||||
if (
|
if (
|
||||||
isset($context['object_to_populate']) &&
|
isset($context['object_to_populate']) &&
|
||||||
|
@ -102,6 +102,7 @@ class GetSetMethodNormalizer extends AbstractNormalizer
|
|||||||
$reflectionClass = new \ReflectionClass($class);
|
$reflectionClass = new \ReflectionClass($class);
|
||||||
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
|
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
|
||||||
|
|
||||||
|
$classMethods = get_class_methods($object);
|
||||||
foreach ($normalizedData as $attribute => $value) {
|
foreach ($normalizedData as $attribute => $value) {
|
||||||
if ($this->nameConverter) {
|
if ($this->nameConverter) {
|
||||||
$attribute = $this->nameConverter->denormalize($attribute);
|
$attribute = $this->nameConverter->denormalize($attribute);
|
||||||
@ -113,7 +114,7 @@ class GetSetMethodNormalizer extends AbstractNormalizer
|
|||||||
if ($allowed && !$ignored) {
|
if ($allowed && !$ignored) {
|
||||||
$setter = 'set'.ucfirst($attribute);
|
$setter = 'set'.ucfirst($attribute);
|
||||||
|
|
||||||
if (method_exists($object, $setter)) {
|
if (in_array($setter, $classMethods)) {
|
||||||
$object->$setter($value);
|
$object->$setter($value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -228,6 +228,12 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('bar', $obj->getBar());
|
$this->assertEquals('bar', $obj->getBar());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testConstructorWArgWithPrivateMutator()
|
||||||
|
{
|
||||||
|
$obj = $this->normalizer->denormalize(array('foo' => 'bar'), __NAMESPACE__.'\ObjectConstructorArgsWithPrivateMutatorDummy', 'any');
|
||||||
|
$this->assertEquals('bar', $obj->getFoo());
|
||||||
|
}
|
||||||
|
|
||||||
public function testGroupsNormalize()
|
public function testGroupsNormalize()
|
||||||
{
|
{
|
||||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||||
@ -511,8 +517,8 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testDenormalizeNonExistingAttribute()
|
public function testDenormalizeNonExistingAttribute()
|
||||||
{
|
{
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
new PropertyDummy(),
|
new GetSetDummy(),
|
||||||
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\PropertyDummy')
|
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\GetSetDummy')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -520,6 +526,12 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testPrivateSetter()
|
||||||
|
{
|
||||||
|
$obj = $this->normalizer->denormalize(array('foo' => 'foobar'), __NAMESPACE__.'\ObjectWithPrivateSetterDummy');
|
||||||
|
$this->assertEquals('bar', $obj->getFoo());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class GetSetDummy
|
class GetSetDummy
|
||||||
@ -726,3 +738,37 @@ class GetCamelizedDummy
|
|||||||
return $this->bar_foo;
|
return $this->bar_foo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ObjectConstructorArgsWithPrivateMutatorDummy
|
||||||
|
{
|
||||||
|
private $foo;
|
||||||
|
|
||||||
|
public function __construct($foo)
|
||||||
|
{
|
||||||
|
$this->setFoo($foo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFoo()
|
||||||
|
{
|
||||||
|
return $this->foo;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setFoo($foo)
|
||||||
|
{
|
||||||
|
$this->foo = $foo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ObjectWithPrivateSetterDummy
|
||||||
|
{
|
||||||
|
private $foo = 'bar';
|
||||||
|
|
||||||
|
public function getFoo()
|
||||||
|
{
|
||||||
|
return $this->foo;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setFoo($foo)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user