Skip to content

Handle CDATA in unknown extension data (IExtensibleDataObject)#130502

Open
StephenMolloy wants to merge 1 commit into
dotnet:mainfrom
StephenMolloy:59123-IExtensibleDataObject-CDATA
Open

Handle CDATA in unknown extension data (IExtensibleDataObject)#130502
StephenMolloy wants to merge 1 commit into
dotnet:mainfrom
StephenMolloy:59123-IExtensibleDataObject-CDATA

Conversation

@StephenMolloy

Copy link
Copy Markdown
Member

DataContractSerializer threw a SerializationException when deserializing an unknown element whose value is carried in a CDATA section into ExtensionData (types implementing IExtensibleDataObject). ReadExtensionDataValue's NodeType switch handled Text/Element/EndElement but not CDATA, and MoveToContent stops on CDATA rather than folding it into Text, so the value hit the default case and threw. ReadUnknownXmlData similarly special-cased only Text and would drop leading CDATA content.

Treat CDATA like Text in both places. The content is captured as a string node and replays as escaped text on re-serialization, preserving round-trip semantics.

Fixes #59123

DataContractSerializer threw a SerializationException when deserializing an
unknown element whose value is carried in a CDATA section into ExtensionData
(types implementing IExtensibleDataObject). ReadExtensionDataValue's NodeType
switch handled Text/Element/EndElement but not CDATA, and MoveToContent stops
on CDATA rather than folding it into Text, so the value hit the default case
and threw. ReadUnknownXmlData similarly special-cased only Text and would drop
leading CDATA content.

Treat CDATA like Text in both places. The content is captured as a string node
and replays as escaped text on re-serialization, preserving round-trip
semantics.

Fixes dotnet#59123

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@StephenMolloy StephenMolloy added this to the 11.0.0 milestone Jul 10, 2026
@StephenMolloy StephenMolloy self-assigned this Jul 10, 2026
Copilot AI review requested due to automatic review settings July 10, 2026 16:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 fixes DataContractSerializer extension-data deserialization for unknown members whose element content is carried in a CDATA section by treating XmlNodeType.CDATA the same as XmlNodeType.Text during extension-data parsing, and adds a regression test to validate down-level round-tripping via IExtensibleDataObject.

Changes:

  • Handle XmlNodeType.CDATA in ReadExtensionDataValue so primitive extension data can be read from CDATA content.
  • Preserve leading CDATA content in ReadUnknownXmlData by recognizing CDATA as text content (instead of advancing past it).
  • Add a regression test and simple versioned contracts to validate ExtensionData capture and round-trip behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/XmlObjectSerializerReadContext.cs Treat CDATA as text when reading primitive extension data and when capturing unknown XML into XmlDataNode, avoiding exceptions/dropped content.
src/libraries/System.Runtime.Serialization.Xml/tests/DataContractSerializer.cs Adds an xUnit regression test that exercises unknown-member-with-CDATA flowing into ExtensionData and round-tripping back into a newer contract.
src/libraries/System.Runtime.Serialization.Xml/tests/SerializationTypes.RuntimeOnly.cs Adds CdataVersionedV1/V2 contracts implementing IExtensibleDataObject for the new regression test scenario.

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.

DataContractSerializer cannot deserialize unknown property containing CDATA when implementing IExtensibleDataObject

2 participants