bug #35846 [Serializer] prevent method calls on null values (xabbuh)
This PR was merged into the 3.4 branch.
Discussion
----------
[Serializer] prevent method calls on null values
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | Fix #35824
| License | MIT
| Doc PR |
Commits
-------
847d6dc8f3
prevent method calls on null values
This commit is contained in:
commit
7295d25f32
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Serializer\Encoder;
|
namespace Symfony\Component\Serializer\Encoder;
|
||||||
|
|
||||||
|
use Symfony\Component\Serializer\Exception\BadMethodCallException;
|
||||||
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
|
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -375,7 +376,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
|
|||||||
{
|
{
|
||||||
$append = true;
|
$append = true;
|
||||||
|
|
||||||
if (\is_array($data) || ($data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format))) {
|
if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data, $this->format)))) {
|
||||||
foreach ($data as $key => $data) {
|
foreach ($data as $key => $data) {
|
||||||
//Ah this is the magic @ attribute types.
|
//Ah this is the magic @ attribute types.
|
||||||
if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
|
if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
|
||||||
@ -410,6 +411,10 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (\is_object($data)) {
|
if (\is_object($data)) {
|
||||||
|
if (null === $this->serializer) {
|
||||||
|
throw new BadMethodCallException(sprintf('The serializer needs to be set to allow %s() to be used with object data.', __METHOD__));
|
||||||
|
}
|
||||||
|
|
||||||
$data = $this->serializer->normalize($data, $this->format, $this->context);
|
$data = $this->serializer->normalize($data, $this->format, $this->context);
|
||||||
if (null !== $data && !is_scalar($data)) {
|
if (null !== $data && !is_scalar($data)) {
|
||||||
return $this->buildXml($parentNode, $data, $xmlRootNodeName);
|
return $this->buildXml($parentNode, $data, $xmlRootNodeName);
|
||||||
@ -484,6 +489,10 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
|
|||||||
} elseif ($val instanceof \Traversable) {
|
} elseif ($val instanceof \Traversable) {
|
||||||
$this->buildXml($node, $val);
|
$this->buildXml($node, $val);
|
||||||
} elseif (\is_object($val)) {
|
} elseif (\is_object($val)) {
|
||||||
|
if (null === $this->serializer) {
|
||||||
|
throw new BadMethodCallException(sprintf('The serializer needs to be set to allow %s() to be used with object data.', __METHOD__));
|
||||||
|
}
|
||||||
|
|
||||||
return $this->selectNodeType($node, $this->serializer->normalize($val, $this->format, $this->context));
|
return $this->selectNodeType($node, $this->serializer->normalize($val, $this->format, $this->context));
|
||||||
} elseif (is_numeric($val)) {
|
} elseif (is_numeric($val)) {
|
||||||
return $this->appendText($node, (string) $val);
|
return $this->appendText($node, (string) $val);
|
||||||
|
@ -68,6 +68,10 @@ class ArrayDenormalizer implements DenormalizerInterface, SerializerAwareInterfa
|
|||||||
*/
|
*/
|
||||||
public function supportsDenormalization($data, $type, $format = null/*, array $context = []*/)
|
public function supportsDenormalization($data, $type, $format = null/*, array $context = []*/)
|
||||||
{
|
{
|
||||||
|
if (null === $this->serializer) {
|
||||||
|
throw new BadMethodCallException(sprintf('The serializer needs to be set to allow %s() to be used.', __METHOD__));
|
||||||
|
}
|
||||||
|
|
||||||
$context = \func_num_args() > 3 ? func_get_arg(3) : [];
|
$context = \func_num_args() > 3 ? func_get_arg(3) : [];
|
||||||
|
|
||||||
return '[]' === substr($type, -2)
|
return '[]' === substr($type, -2)
|
||||||
|
Reference in New Issue
Block a user