Hi,

a couple months back my code worked fine, where I created a new instance of my class object, which holds a private variable $encoder, that get's assigned on construction:

  function __construct() {
    $this->encoder = \Drupal::service('serializer.encoder.xml');
  }

and from another function in this code I call decode function on what I know for sure to be XML string:

  return $this->encoder->decode($response->getBody()->__toString(), 'xml');

However now what ever I do, I get the following error:

The website encountered an unexpected error. Please try again later.
TypeError: Argument 1 passed to Symfony\Component\Serializer\Encoder\SerializerAwareEncoder::setSerializer() must be an instance of Symfony\Component\Serializer\SerializerInterface, null given, called in /var/www/website/public_html/core/modules/serialization/src/Encoder/XmlEncoder.php on line 41 in Symfony\Component\Serializer\Encoder\SerializerAwareEncoder->setSerializer() (line 29 of vendor/symfony/serializer/Encoder/SerializerAwareEncoder.php).

since the line 41 in the XmlEncoder.php is:

  /**
   * Gets the base encoder instance.
   *
   * @return \Symfony\Component\Serializer\Encoder\XmlEncoder
   *   The base encoder.
   */
  public function getBaseEncoder() {
    if (!isset($this->baseEncoder)) {
      $this->baseEncoder = new BaseXmlEncoder();
      $this->baseEncoder->setSerializer($this->serializer);   <-- line 41
    }

    return $this->baseEncoder;
  }

I assume something might've broken from within the core serialization module itself?

Hopefully this is useful and someone can help out.

Best regards,

Alari

CommentFileSizeAuthor
#4 local_history.patch1.26 KBAlluuu

Comments

Alluuu created an issue. See original summary.

Alluuu’s picture

Issue summary: View changes
Alluuu’s picture

Title: Serializer internally broken » XML serialization module internally broken

I've tested that in the exact same way using the JsonEncoder works every way.

However regardless of whether I want to encode or decode XML the error is the same.

It seems that the serialization module was changed from 8.2.x -> 8.3.x and that's where the issue could be from.

Alluuu’s picture

Version: 8.3.7 » 8.4.0-rc2
Assigned: Unassigned » Alluuu
Status: Active » Needs review
StatusFileSize
new1.26 KB

The issue seems to be from the fact that the class XmlEncoder now extends SerializerAwareEncoder, which is basically:

just this following code:

abstract class SerializerAwareEncoder implements SerializerAwareInterface
{
    protected $serializer;

    /**
     * {@inheritdoc}
     */
    public function setSerializer(SerializerInterface $serializer)
    {
        $this->serializer = $serializer;
    }
}

and on line 41 of XmlEncoder the $serializer variable get's set to itself, expecting something other than null:

$this->baseEncoder->setSerializer($this->serializer);

Because the subclass doesn't have it's own $serializer variable this is essentially:

$this->serializer = $this->serializer

Which won't be reached as the parameter type isn't of expected type.

As this code hasn't changed since, the issue will surely also exist in 8.4.0-rc2 and I'm changing the issue version accordingly.

I've never created a patch before, so I hope this is how it's done.

Status: Needs review » Needs work

The last submitted patch, 4: local_history.patch, failed testing. View results

jhedstrom’s picture

jhedstrom’s picture

Version: 8.4.0-rc2 » 8.5.x-dev
Alluuu’s picture

So what is the resolution, that for the time being the whole xml serialization should remain completely unusable rather than just have a partial error case?

jhedstrom’s picture

@Alluuu you use the serializer service instead, and it has the same method:

$this->serializer->decode($contents, 'xml')

where $this->serializer is an instance of \Symfony\Component\Serializer\SerializerInterface.

So in your example constructor:

  function __construct() {
    $this->encoder = \Drupal::service('serializer');
  }
jhedstrom’s picture

Alluuu’s picture

Thanks for the tip, Serializer itself works :)

wim leers’s picture

Status: Needs work » Closed (works as designed)

Alright, let's continue in #2910682: Mark serializer encoders @internal! :)

Thanks, @jhedstrom!