Skip to content

Fix XmlSerializer regression for derived XmlText properties - #130429

Merged
StephenMolloy merged 2 commits into
dotnet:mainfrom
StephenMolloy:121881-XmlSerializer-repeated-xmlattribute-is-ok
Jul 15, 2026
Merged

StephenMolloy merged 2 commits into
dotnet:mainfrom
StephenMolloy:121881-XmlSerializer-repeated-xmlattribute-is-ok

Conversation

@StephenMolloy

Copy link
Copy Markdown
Contributor

Fix XmlSerializer regression: derived [XmlText]/[XmlNamespaceDeclarations] overrides on simpleContent base

When a derived class re-declares [XmlText] (or [XmlNamespaceDeclarations]) on
a virtual property override, InitializeStructMembers was incorrectly tracking
those as new text/xmlns accessors for the derived mapping. When the base
mapping has HasSimpleContent == true (only [XmlText], no elements), this caused
SetContentModel to throw XmlIllegalSimpleContentExtension.

Root cause: commit cc2019b changed the handling of base-declared members
from 'continue' (skip entirely) to 'RemoveUniqueAccessor + fall-through'. This
kept the derived member's element/attribute accessors correctly registered, but
also let text and xmlns accessors be registered again, which the simpleContent
check in SetContentModel rejects.

Fix: introduce 'memberDeclaredByBase' and guard the text and xmlns accessor
tracking with '&& !memberDeclaredByBase'. The derived member's accessors are
still correctly registered via AddUniqueAccessor and added to mapping.Members
(preserving cc2019b's intent for the reflection-based serializer); only
the content-model-level tracking is suppressed for inherited overrides.

Co-authored-by: Copilot [email protected]

Fixes #121881

StephenMolloy and others added 2 commits July 8, 2026 18:56
…ions] overrides on simpleContent base

When a derived class re-declares [XmlText] (or [XmlNamespaceDeclarations]) on
a virtual property override, InitializeStructMembers was incorrectly tracking
those as new text/xmlns accessors for the derived mapping. When the base
mapping has HasSimpleContent == true (only [XmlText], no elements), this caused
SetContentModel to throw XmlIllegalSimpleContentExtension.

Root cause: commit cc2019b changed the handling of base-declared members
from 'continue' (skip entirely) to 'RemoveUniqueAccessor + fall-through'. This
kept the derived member's element/attribute accessors correctly registered, but
also let text and xmlns accessors be registered again, which the simpleContent
check in SetContentModel rejects.

Fix: introduce 'memberDeclaredByBase' and guard the text and xmlns accessor
tracking with '&& !memberDeclaredByBase'. The derived member's accessors are
still correctly registered via AddUniqueAccessor and added to mapping.Members
(preserving cc2019b's intent for the reflection-based serializer); only
the content-model-level tracking is suppressed for inherited overrides.

Co-authored-by: Copilot <[email protected]>
@StephenMolloy StephenMolloy added this to the 11.0.0 milestone Jul 9, 2026
@StephenMolloy StephenMolloy self-assigned this Jul 9, 2026
Copilot AI review requested due to automatic review settings July 9, 2026 19:23

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

Fixes a regression in System.Xml.Serialization where overriding a base-declared [XmlText] (and related [XmlNamespaceDeclarations]) member in a derived type could incorrectly re-register content-model accessors, leading to XmlIllegalSimpleContentExtension being thrown during type reflection.

Changes:

  • Update XmlReflectionImporter.InitializeStructMembers to detect members already declared by the base mapping and avoid re-tracking text/xmlns accessors for those members.
  • Add regression coverage for derived overrides of [XmlText], plus additional tests around overriding [XmlAttribute] properties.
  • Add new shared test types to reproduce the inheritance/override scenarios.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Runtime.Serialization.Xml/tests/SerializationTypes.RuntimeOnly.cs Adds shared test types that model derived overrides of [XmlText] and [XmlAttribute] members.
src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.RuntimeOnly.cs Adds regression tests validating serialize/deserialize behavior for the new inheritance scenarios.
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlReflectionImporter.cs Fixes base-member override handling by suppressing duplicate text/xmlns content-model tracking.

@StephenMolloy
StephenMolloy marked this pull request as ready for review July 10, 2026 07:13
@StephenMolloy
StephenMolloy requested a review from mangod9 July 10, 2026 07:13

@mangod9 mangod9 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

looks good

@StephenMolloy

Copy link
Copy Markdown
Contributor Author

/ba-g wasm failures unrelated to this change

@StephenMolloy
StephenMolloy merged commit 8dd1f60 into dotnet:main Jul 15, 2026
96 of 98 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-preview7 Jul 15, 2026
@snakefoot

Copy link
Copy Markdown
Contributor

Candidate for merging to NET10-release-branch ?

@github-actions github-actions Bot locked and limited conversation to collaborators Aug 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET 10.0 XmlSerializer throws an exception on classes that worked on .NET 9

4 participants