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
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | local_history.patch | 1.26 KB | Alluuu |
Comments
Comment #2
Alluuu commentedComment #3
Alluuu commentedI'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.
Comment #4
Alluuu commentedThe issue seems to be from the fact that the class XmlEncoder now extends SerializerAwareEncoder, which is basically:
just this following code:
and on line 41 of XmlEncoder the $serializer variable get's set to itself, expecting something other than null:
Because the subclass doesn't have it's own $serializer variable this is essentially:
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.
Comment #6
jhedstromThis was introduced in #2685097: 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, and a similar issue reported in #2891850: Fatal error on serialization module, and I think this is works as designed, however, it does render the
serializer.encoder.xmlservice unusable, so this seems like some documentation, or an@internaltag is needed...Comment #7
jhedstromComment #8
Alluuu commentedSo 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?
Comment #9
jhedstrom@Alluuu you use the
serializerservice instead, and it has the same method:where
$this->serializeris an instance of\Symfony\Component\Serializer\SerializerInterface.So in your example constructor:
Comment #10
jhedstromI've added #2910682: Mark serializer encoders @internal.
Comment #11
Alluuu commentedThanks for the tip, Serializer itself works :)
Comment #12
wim leersAlright, let's continue in #2910682: Mark serializer encoders @internal! :)
Thanks, @jhedstrom!