Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Fix: Report Xml warning/error instead of silently returning a wrong xml #54346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function encode(mixed $data, string $format, array $context = []): string
$encoderIgnoredNodeTypes = $context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES];
$ignorePiNode = \in_array(\XML_PI_NODE, $encoderIgnoredNodeTypes, true);
if ($data instanceof \DOMDocument) {
return $data->saveXML($ignorePiNode ? $data->documentElement : null);
return $this->saveXml($data, $ignorePiNode ? $data->documentElement : null);
}

$xmlRootNodeName = $context[self::ROOT_NODE_NAME] ?? $this->defaultContext[self::ROOT_NODE_NAME];
Expand All @@ -97,7 +97,7 @@ public function encode(mixed $data, string $format, array $context = []): string
$this->appendNode($dom, $data, $format, $context, $xmlRootNodeName);
}

return $dom->saveXML($ignorePiNode ? $dom->documentElement : null, $context[self::SAVE_OPTIONS] ?? $this->defaultContext[self::SAVE_OPTIONS]);
return $this->saveXml($dom, $ignorePiNode ? $dom->documentElement : null, $context[self::SAVE_OPTIONS] ?? $this->defaultContext[self::SAVE_OPTIONS]);
}

public function decode(string $data, string $format, array $context = []): mixed
Expand Down Expand Up @@ -498,4 +498,23 @@ private function createDomDocument(array $context): \DOMDocument

return $document;
}

/**
* @throws NotEncodableValueException
*/
private function saveXml(\DOMDocument $document, ?\DOMNode $node = null, ?int $options = null): string
{
$prevErrorHandler = set_error_handler(static function ($type, $message, $file, $line, $context = []) use (&$prevErrorHandler) {
if (\E_ERROR === $type || \E_WARNING === $type) {
throw new NotEncodableValueException($message);
}

return $prevErrorHandler ? $prevErrorHandler($type, $message, $file, $line, $context) : false;
});
try {
return $document->saveXML($node, $options);
} finally {
restore_error_handler();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,12 @@ public function testEncodeTraversableWhenNormalizable()
$this->assertEquals($expected, $serializer->serialize(new NormalizableTraversableDummy(), 'xml'));
}

public function testEncodeException()
{
$this->expectException(NotEncodableValueException::class);
$this->encoder->encode('Invalid character: '.\chr(7), 'xml');
}

public function testDecode()
{
$source = $this->getXmlSource();
Expand Down