Skip to content

Commit 1050ea6

Browse files
committed
Merge branch '2.8'
* 2.8: [Yaml] throw a ParseException on invalid data type [TwigBridge] type-dependent path discovery Resources as string have the same problem Introduce failing test case when a SplFileInfo object is passed to the extract() method in the TwigExtractor. #15331 add infos about deprecated classes to UPGRADE-3.0 [Asset] removed unused private property. [Twig+FrameworkBundle] Fix forward compat with Form 2.8 [2.6] Static Code Analysis for Components [Security/Http] Fix test relying on a private property [Serializer] Fix bugs reported in symfony/symfony@b5990be#commitcomment-12301266 [Form] Fix not-BC test assertion [Security] Moved Simple{Form,Pre}AuthenticatorInterfaces to Security\Http [Security] removed useless else condition in SwitchUserListener class. [travis] Tests deps=low with PHP 5.6 Implement resettable containers [Console] Fix console output with closed stdout
2 parents dbd6b99 + 6a7f6ea commit 1050ea6

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

Normalizer/AbstractNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ protected function prepareForDenormalization($data)
242242
*
243243
* @throws RuntimeException
244244
*/
245-
protected function instantiateObject(array $data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
245+
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
246246
{
247247
if (
248248
isset($context['object_to_populate']) &&

Normalizer/GetSetMethodNormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
102102
$reflectionClass = new \ReflectionClass($class);
103103
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
104104

105+
$classMethods = get_class_methods($object);
105106
foreach ($normalizedData as $attribute => $value) {
106107
if ($this->nameConverter) {
107108
$attribute = $this->nameConverter->denormalize($attribute);
@@ -113,7 +114,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
113114
if ($allowed && !$ignored) {
114115
$setter = 'set'.ucfirst($attribute);
115116

116-
if (method_exists($object, $setter)) {
117+
if (in_array($setter, $classMethods)) {
117118
$object->$setter($value);
118119
}
119120
}

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ public function testConstructorWithObjectDenormalize()
156156
$this->assertEquals('bar', $obj->getBar());
157157
}
158158

159+
public function testConstructorWArgWithPrivateMutator()
160+
{
161+
$obj = $this->normalizer->denormalize(array('foo' => 'bar'), __NAMESPACE__.'\ObjectConstructorArgsWithPrivateMutatorDummy', 'any');
162+
$this->assertEquals('bar', $obj->getFoo());
163+
}
164+
159165
public function testGroupsNormalize()
160166
{
161167
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
@@ -439,15 +445,21 @@ public function testObjectToPopulate()
439445
public function testDenormalizeNonExistingAttribute()
440446
{
441447
$this->assertEquals(
442-
new PropertyDummy(),
443-
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\PropertyDummy')
448+
new GetSetDummy(),
449+
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\GetSetDummy')
444450
);
445451
}
446452

447453
public function testNoTraversableSupport()
448454
{
449455
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
450456
}
457+
458+
public function testPrivateSetter()
459+
{
460+
$obj = $this->normalizer->denormalize(array('foo' => 'foobar'), __NAMESPACE__.'\ObjectWithPrivateSetterDummy');
461+
$this->assertEquals('bar', $obj->getFoo());
462+
}
451463
}
452464

453465
class GetSetDummy
@@ -654,3 +666,37 @@ public function getBar_foo()
654666
return $this->bar_foo;
655667
}
656668
}
669+
670+
class ObjectConstructorArgsWithPrivateMutatorDummy
671+
{
672+
private $foo;
673+
674+
public function __construct($foo)
675+
{
676+
$this->setFoo($foo);
677+
}
678+
679+
public function getFoo()
680+
{
681+
return $this->foo;
682+
}
683+
684+
private function setFoo($foo)
685+
{
686+
$this->foo = $foo;
687+
}
688+
}
689+
690+
class ObjectWithPrivateSetterDummy
691+
{
692+
private $foo = 'bar';
693+
694+
public function getFoo()
695+
{
696+
return $this->foo;
697+
}
698+
699+
private function setFoo($foo)
700+
{
701+
}
702+
}

0 commit comments

Comments
 (0)