Implement XmlSerializer end element handling workaround and tests#126765
Implement XmlSerializer end element handling workaround and tests#126765StephenMolloy wants to merge 1 commit intodotnet:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves XmlSerializer behavior when deserializing XML fragments over streaming transports by avoiding an extra read after completing a top-level end element (configurable via an AppContext switch), and adds tests to validate fragment deserialization behavior with the workaround enabled/disabled.
Changes:
- Added
UseXmlSerializerReadEndElementWorkaroundAppContext switch (defaulttrue) to control end-element read behavior. - Updated
XmlSerializationReader.ReadEndElement()andXmlSerializerentry points to avoid unnecessary reads after a top-level end element while still supporting multi-root fragment workflows. - Added fragment/streaming-focused tests plus a custom stream and improved AppContext-switch test scoping.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs | Introduces the new AppContext switch (default enabled). |
| src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationReader.cs | Implements the end-element handling workaround in ReadEndElement(). |
| src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs | Advances past top-level end elements when starting Deserialize / CanDeserialize to enable multi-fragment reading. |
| src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs | Adds streaming/fragment tests and supporting test infrastructure (custom stream + switch scope improvements). |
| XmlSerializer serializer = new XmlSerializer(typeof(SimpleType)); | ||
| byte[] data = Encoding.UTF8.GetBytes(payload); | ||
|
|
||
| // Default should be true | ||
| //const string switchName = "Switch.System.Xml.UseXmlSerializerReadEndElementWorkaround"; | ||
| //using var switchScope = new XmlSerializerAppContextSwitchScope(switchName, true); |
There was a problem hiding this comment.
Remove the commented-out AppContext switch setup and make the test explicitly set the switch value (e.g., within an XmlSerializerAppContextSwitchScope). As written, the test both carries dead commented code and implicitly relies on the process-wide default/environment switch value, which can make the test less deterministic.
| XmlSerializer serializer = new XmlSerializer(typeof(SimpleType)); | |
| byte[] data = Encoding.UTF8.GetBytes(payload); | |
| // Default should be true | |
| //const string switchName = "Switch.System.Xml.UseXmlSerializerReadEndElementWorkaround"; | |
| //using var switchScope = new XmlSerializerAppContextSwitchScope(switchName, true); | |
| const string switchName = "Switch.System.Xml.UseXmlSerializerReadEndElementWorkaround"; | |
| XmlSerializer serializer = new XmlSerializer(typeof(SimpleType)); | |
| byte[] data = Encoding.UTF8.GetBytes(payload); | |
| using var switchScope = new XmlSerializerAppContextSwitchScope(switchName, true); |
| internal sealed class BlockingAfterBufferStream : Stream | ||
| { | ||
| private readonly byte[] _buffer; | ||
| private int _position; | ||
|
|
||
| public BlockingAfterBufferStream(byte[] buffer) | ||
| { | ||
| _buffer = buffer; | ||
| } |
There was a problem hiding this comment.
BlockingAfterBufferStream does not actually block; once the buffer is exhausted it throws TimeoutException. Consider renaming it to reflect the behavior (timeout/throwing) or implementing actual blocking semantics to match the current name, to avoid misleading future readers of the test infrastructure.
This pull request improves handling of XML fragments over streaming transports, particularly when deserializing multiple root elements. The changes add a workaround (enabled by default) to avoid unnecessary reads after a top-level end element, preventing timeouts or exceptions when more data is not immediately available. An AppContext opt-out is also included. The PR also adds comprehensive tests and supporting infrastructure to validate and control this behavior.
XML Serializer Compatibility and Behavior Improvements:
UseXmlSerializerReadEndElementWorkaroundswitch (default: true) toLocalAppContextSwitchesto control workaround behavior for reading end elements in XML fragments.XmlSerializationReader.ReadEndElement()to respect the new switch, avoiding extra reads after a top-level end element when enabled, which prevents blocking or timeouts in streaming/fragment scenarios.AdvancePastTopLevelEndElementIfNeeded(XmlReader)utility and invoked it inXmlSerializermethods (GetMapping,CanDeserialize) to ensure correct reader advancement when the workaround is enabled. [1] [2] [3]Testing and Infrastructure Enhancements:
XmlSerializerTests.csto verify deserialization of XML fragments with and without the workaround, including a test that disables the switch and expects a timeout exception.BlockingAfterBufferStream, a customStreamthat simulates blocking behavior after buffer exhaustion, to facilitate robust testing of streaming scenarios.XmlSerializerAppContextSwitchScopeto properly unset AppContext switches and clear cached values, ensuring test isolation and reliability.Fixes: #47371