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
							
								
									4b8961ed15
								
							
						
					
					
						commit
						081132ddb7
					
				| 
						 | 
				
			
			@ -4,6 +4,7 @@ namespace Drupal\serialization\Encoder;
 | 
			
		|||
 | 
			
		||||
use Symfony\Component\Serializer\Encoder\EncoderInterface;
 | 
			
		||||
use Symfony\Component\Serializer\Encoder\DecoderInterface;
 | 
			
		||||
use Symfony\Component\Serializer\Encoder\SerializerAwareEncoder;
 | 
			
		||||
use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			@ -12,7 +13,7 @@ use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder;
 | 
			
		|||
 * This acts as a wrapper class for Symfony's XmlEncoder so that it is not
 | 
			
		||||
 * 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -37,6 +38,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface {
 | 
			
		|||
  public function getBaseEncoder() {
 | 
			
		||||
    if (!isset($this->baseEncoder)) {
 | 
			
		||||
      $this->baseEncoder = new BaseXmlEncoder();
 | 
			
		||||
      $this->baseEncoder->setSerializer($this->serializer);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return $this->baseEncoder;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,6 +3,9 @@
 | 
			
		|||
namespace Drupal\Tests\serialization\Unit\Encoder;
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			@ -31,7 +34,7 @@ class XmlEncoderTest extends UnitTestCase {
 | 
			
		|||
  protected $testArray = ['test' => 'test'];
 | 
			
		||||
 | 
			
		||||
  protected function setUp() {
 | 
			
		||||
    $this->baseEncoder = $this->getMock('Symfony\Component\Serializer\Encoder\XmlEncoder');
 | 
			
		||||
    $this->baseEncoder = $this->getMock(BaseXmlEncoder::class);
 | 
			
		||||
    $this->encoder = new XmlEncoder();
 | 
			
		||||
    $this->encoder->setBaseEncoder($this->baseEncoder);
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			@ -76,4 +79,26 @@ class XmlEncoderTest extends UnitTestCase {
 | 
			
		|||
    $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';
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue