Skip to content

Commit 847d6dc

Browse files
committed
prevent method calls on null values
1 parent f75be00 commit 847d6dc

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Serializer\Encoder;
1313

14+
use Symfony\Component\Serializer\Exception\BadMethodCallException;
1415
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
1516

1617
/**
@@ -375,7 +376,7 @@ private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
375376
{
376377
$append = true;
377378

378-
if (\is_array($data) || ($data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format))) {
379+
if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data, $this->format)))) {
379380
foreach ($data as $key => $data) {
380381
//Ah this is the magic @ attribute types.
381382
if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
@@ -410,6 +411,10 @@ private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
410411
}
411412

412413
if (\is_object($data)) {
414+
if (null === $this->serializer) {
415+
throw new BadMethodCallException(sprintf('The serializer needs to be set to allow %s() to be used with object data.', __METHOD__));
416+
}
417+
413418
$data = $this->serializer->normalize($data, $this->format, $this->context);
414419
if (null !== $data && !is_scalar($data)) {
415420
return $this->buildXml($parentNode, $data, $xmlRootNodeName);
@@ -484,6 +489,10 @@ private function selectNodeType(\DOMNode $node, $val)
484489
} elseif ($val instanceof \Traversable) {
485490
$this->buildXml($node, $val);
486491
} elseif (\is_object($val)) {
492+
if (null === $this->serializer) {
493+
throw new BadMethodCallException(sprintf('The serializer needs to be set to allow %s() to be used with object data.', __METHOD__));
494+
}
495+
487496
return $this->selectNodeType($node, $this->serializer->normalize($val, $this->format, $this->context));
488497
} elseif (is_numeric($val)) {
489498
return $this->appendText($node, (string) $val);

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

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public function denormalize($data, $type, $format = null, array $context = [])
6868
*/
6969
public function supportsDenormalization($data, $type, $format = null/*, array $context = []*/)
7070
{
71+
if (null === $this->serializer) {
72+
throw new BadMethodCallException(sprintf('The serializer needs to be set to allow %s() to be used.', __METHOD__));
73+
}
74+
7175
$context = \func_num_args() > 3 ? func_get_arg(3) : [];
7276

7377
return '[]' === substr($type, -2)

0 commit comments

Comments
 (0)