Skip to content

Commit e437e04

Browse files
committed
fix reflection type
1 parent 3fe9802 commit e437e04

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,14 @@ protected function instantiateComplexObject(array &$data, $class, array &$contex
332332
}
333333
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
334334
$parameterData = $data[$key];
335-
if (null !== $constructorParameter->getClass()) {
336-
$parameterData = $this->serializer->deserialize($parameterData, $constructorParameter->getClass()->getName(), $format, $context);
335+
if (null !== $constructorParameter->getType()) {
336+
try {
337+
$parameterClass = $constructorParameter->getClass()->getName();
338+
} catch (\ReflectionException $e) {
339+
throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e);
340+
}
341+
342+
$parameterData = $this->serializer->deserialize($parameterData, $parameterClass, $format, $context);
337343
}
338344

339345
// Don't run set for a parameter passed to the constructor

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,27 @@ public function testConstructorWithObjectTypeHintDenormalize()
180180
$this->assertEquals('rab', $obj->getInner()->bar);
181181
}
182182

183+
/**
184+
* @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
185+
* @expectedExceptionMessage Could not determine the class of the parameter "unknown".
186+
*/
187+
public function testConstructorWithUnknownObjectTypeHintDenormalize()
188+
{
189+
$data = array(
190+
'id' => 10,
191+
'unknown' => array(
192+
'foo' => 'oof',
193+
'bar' => 'rab',
194+
),
195+
);
196+
197+
$normalizer = new ObjectNormalizer();
198+
$serializer = new DenormalizerDecoratorSerializer($normalizer);
199+
$normalizer->setSerializer($serializer);
200+
201+
$normalizer->denormalize($data, DummyWithConstructorInexistingObject::class);
202+
}
203+
183204
public function testGroupsNormalize()
184205
{
185206
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
@@ -827,3 +848,10 @@ public function getInner()
827848
return $this->inner;
828849
}
829850
}
851+
852+
class DummyWithConstructorInexistingObject
853+
{
854+
public function __construct($id, Unknown $unknown)
855+
{
856+
}
857+
}

0 commit comments

Comments
 (0)