bug #9391 [Serializer] Fixed the error handling when decoding invalid XML to avoid a Warning (stof)

This PR was merged into the 2.2 branch.

Discussion
----------

[Serializer] Fixed the error handling when decoding invalid XML to avoid a Warning

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Without the exception being thrown here, the call to ``simplexml_import_dom`` throws a warning saying ``Invalid Nodetype to import``.
This has been reported by @Tobion after we refactored FOSRestBundle to use this XmlEncoder instead of a duplication of a previous version of the class: https://github.com/FriendsOfSymfony/FOSRestBundle/pull/583/files#r7221045

Commits
-------

b2550b9 Fixed the error handling when decoding invalid XML to avoid a Warning
This commit is contained in:
Fabien Potencier 2013-10-28 14:03:08 +01:00
commit ef695655f5
2 changed files with 14 additions and 0 deletions

View File

@ -76,6 +76,10 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
if ($error = libxml_get_last_error()) {
throw new UnexpectedValueException($error->message);
}
foreach ($dom->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
throw new UnexpectedValueException('Document types are not allowed.');

View File

@ -20,6 +20,8 @@ use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
class XmlEncoderTest extends \PHPUnit_Framework_TestCase
{
private $encoder;
protected function setUp()
{
$this->encoder = new XmlEncoder;
@ -301,6 +303,14 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function testDecodeInvalidXml()
{
$this->encoder->decode('<?xml version="1.0"?><invalid><xml>', 'xml');
}
public function testPreventsComplexExternalEntities()
{
$oldCwd = getcwd();