Skip to content

Implement XmlSerializer end element handling workaround and tests#126765

Merged
StephenMolloy merged 2 commits intodotnet:mainfrom
StephenMolloy:47371-EndOfStream-Workaround
Apr 30, 2026
Merged

Implement XmlSerializer end element handling workaround and tests#126765
StephenMolloy merged 2 commits intodotnet:mainfrom
StephenMolloy:47371-EndOfStream-Workaround

Conversation

@StephenMolloy
Copy link
Copy Markdown
Member

@StephenMolloy StephenMolloy commented Apr 10, 2026

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:

  • Added a new UseXmlSerializerReadEndElementWorkaround switch (default: true) to LocalAppContextSwitches to control workaround behavior for reading end elements in XML fragments.
  • Updated 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.
  • Introduced AdvancePastTopLevelEndElementIfNeeded(XmlReader) utility and invoked it in XmlSerializer methods (GetMapping, CanDeserialize) to ensure correct reader advancement when the workaround is enabled. [1] [2] [3]

Testing and Infrastructure Enhancements:

  • Added new tests to XmlSerializerTests.cs to verify deserialization of XML fragments with and without the workaround, including a test that disables the switch and expects a timeout exception.
  • Implemented BlockingAfterBufferStream, a custom Stream that simulates blocking behavior after buffer exhaustion, to facilitate robust testing of streaming scenarios.
  • Improved XmlSerializerAppContextSwitchScope to properly unset AppContext switches and clear cached values, ensuring test isolation and reliability.

Fixes: #47371

Copilot AI review requested due to automatic review settings April 10, 2026 21:26
@StephenMolloy StephenMolloy added this to the 11.0.0 milestone Apr 10, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 UseXmlSerializerReadEndElementWorkaround AppContext switch (default true) to control end-element read behavior.
  • Updated XmlSerializationReader.ReadEndElement() and XmlSerializer entry 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).

Comment thread src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XmlSerializer requires next tag after EndElement to be present in Stream

3 participants