Commit 081132dd authored by alexpott's avatar alexpott

Issue #2685097 by damiankloip, Wim Leers: Fatal error: Call to a member...

Issue #2685097 by damiankloip, Wim Leers: Fatal error: Call to a member function normalize() on a non-object in XmlEncoder when encoding into xml and there are embedded objects in the response
parent 4b8961ed
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
use Symfony\Component\Serializer\Encoder\EncoderInterface; use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface; use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\SerializerAwareEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder; use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder;
/** /**
...@@ -12,7 +13,7 @@ ...@@ -12,7 +13,7 @@
* This acts as a wrapper class for Symfony's XmlEncoder so that it is not * This acts as a wrapper class for Symfony's XmlEncoder so that it is not
* implementing NormalizationAwareInterface, and can be normalized externally. * implementing NormalizationAwareInterface, and can be normalized externally.
*/ */
class XmlEncoder implements EncoderInterface, DecoderInterface { class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface {
/** /**
* The formats that this Encoder supports. * The formats that this Encoder supports.
...@@ -37,6 +38,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface { ...@@ -37,6 +38,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface {
public function getBaseEncoder() { public function getBaseEncoder() {
if (!isset($this->baseEncoder)) { if (!isset($this->baseEncoder)) {
$this->baseEncoder = new BaseXmlEncoder(); $this->baseEncoder = new BaseXmlEncoder();
$this->baseEncoder->setSerializer($this->serializer);
} }
return $this->baseEncoder; return $this->baseEncoder;
......
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
namespace Drupal\Tests\serialization\Unit\Encoder; namespace Drupal\Tests\serialization\Unit\Encoder;
use Drupal\serialization\Encoder\XmlEncoder; use Drupal\serialization\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
/** /**
...@@ -31,7 +34,7 @@ class XmlEncoderTest extends UnitTestCase { ...@@ -31,7 +34,7 @@ class XmlEncoderTest extends UnitTestCase {
protected $testArray = ['test' => 'test']; protected $testArray = ['test' => 'test'];
protected function setUp() { protected function setUp() {
$this->baseEncoder = $this->getMock('Symfony\Component\Serializer\Encoder\XmlEncoder'); $this->baseEncoder = $this->getMock(BaseXmlEncoder::class);
$this->encoder = new XmlEncoder(); $this->encoder = new XmlEncoder();
$this->encoder->setBaseEncoder($this->baseEncoder); $this->encoder->setBaseEncoder($this->baseEncoder);
} }
...@@ -76,4 +79,26 @@ public function testDecode() { ...@@ -76,4 +79,26 @@ public function testDecode() {
$this->assertEquals($this->testArray, $this->encoder->decode('test', 'test')); $this->assertEquals($this->testArray, $this->encoder->decode('test', 'test'));
} }
/**
* @covers ::getBaseEncoder
*/
public function testDefaultEncoderHasSerializer() {
// The serializer should be set on the Drupal encoder, which should then
// set it on our default encoder.
$encoder = new XmlEncoder();
$serialzer = new Serializer([new GetSetMethodNormalizer()]);
$encoder->setSerializer($serialzer);
$base_encoder = $encoder->getBaseEncoder();
$this->assertInstanceOf(BaseXmlEncoder::class, $base_encoder);
// Test the encoder.
$base_encoder->encode(['a' => new TestObject()], 'xml');
}
}
class TestObject {
public function getA() {
return 'A';
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment