Skip to content

Fix reflection-based XmlSerializer to support XmlSchemaObject-derived types#129490

Merged
StephenMolloy merged 2 commits into
dotnet:mainfrom
StephenMolloy:1399-Reflection-based-support-for-XmlSchemaObject-types
Jun 30, 2026
Merged

Fix reflection-based XmlSerializer to support XmlSchemaObject-derived types#129490
StephenMolloy merged 2 commits into
dotnet:mainfrom
StephenMolloy:1399-Reflection-based-support-for-XmlSchemaObject-types

Conversation

@StephenMolloy

Copy link
Copy Markdown
Member

Fixes two related bugs exposed when deserializing XmlSchemaObject-derived types with the reflection-based XmlSerializer:

  1. ReflectionXmlSerializationReader.WriteLiteralStructMethod threw NotImplementedException when the target type was assignable from XmlSchemaObject. The IL-based reader sets DecodeName = false instead, which prevents XmlConvert.DecodeName from corrupting schema names that contain colons (e.g. xs:string). This change matches that behavior.

  2. The member-flushing loop unconditionally called GetSetMemberValueDelegate for all collection-type members, including read-only collection properties like XmlSchemaElement.Constraints. Calling PropertyInfo.SetValue on a property with no setter threw ArgumentException. The fix detects read-only properties (no setter) and adds items directly into the existing collection instance returned by the getter, using AddObjectsIntoTargetCollection.

Fixes #1399

… types

Fixes two related bugs exposed when deserializing XmlSchemaObject-derived
types with the reflection-based XmlSerializer:

1. ReflectionXmlSerializationReader.WriteLiteralStructMethod threw
   NotImplementedException when the target type was assignable from
   XmlSchemaObject. The IL-based reader sets DecodeName = false instead,
   which prevents XmlConvert.DecodeName from corrupting schema names that
   contain colons (e.g. xs:string). This change matches that behavior.

2. The member-flushing loop unconditionally called GetSetMemberValueDelegate
   for all collection-type members, including read-only collection properties
   like XmlSchemaElement.Constraints. Calling PropertyInfo.SetValue on a
   property with no setter threw ArgumentException. The fix detects read-only
   properties (no setter) and adds items directly into the existing collection
   instance returned by the getter, using AddObjectsIntoTargetCollection.

Fixes dotnet#1399

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@StephenMolloy StephenMolloy added this to the 11.0.0 milestone Jun 16, 2026
@StephenMolloy
StephenMolloy requested a review from mangod9 June 16, 2026 23:43
@StephenMolloy StephenMolloy self-assigned this Jun 16, 2026
Copilot AI review requested due to automatic review settings June 16, 2026 23:43

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 improves the reflection-based XmlSerializer deserialization path for XmlSchemaObject-derived types by aligning key behaviors with the IL-generated reader and adding regression coverage in the XmlSerializer test suite.

Changes:

  • In the reflection reader, disable DecodeName when deserializing XmlSchemaObject-derived types (matching the IL-generated reader’s behavior).
  • Fix deserialization for collection members backed by read-only properties (no setter) by populating the existing collection instance instead of trying to assign a new one.
  • Add/relocate tests to cover XmlSchema and XmlSchemaObject round-tripping scenarios (including read-only collection properties like XmlSchemaElement.Constraints) under ReflectionOnly mode.

Reviewed changes

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

File Description
src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.RuntimeOnly.cs Adds regression tests for XmlSchema / XmlSchemaObject round-tripping, including read-only collection properties.
src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.Internal.cs Removes the Xml_XmlSchema test from the internal-only bucket now that ReflectionOnly behavior is covered.
src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs Implements the reflection-reader fixes for XmlSchemaObject name decoding and read-only collection property population.

@StephenMolloy
StephenMolloy merged commit cc23ebc into dotnet:main Jun 30, 2026
86 of 88 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-preview7 Jun 30, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
… types (#129490)

Fixes two related bugs exposed when deserializing
XmlSchemaObject-derived types with the reflection-based XmlSerializer:

1. ReflectionXmlSerializationReader.WriteLiteralStructMethod threw
NotImplementedException when the target type was assignable from
XmlSchemaObject. The IL-based reader sets DecodeName = false instead,
which prevents XmlConvert.DecodeName from corrupting schema names that
contain colons (e.g. xs:string). This change matches that behavior.

2. The member-flushing loop unconditionally called
GetSetMemberValueDelegate for all collection-type members, including
read-only collection properties like XmlSchemaElement.Constraints.
Calling PropertyInfo.SetValue on a property with no setter threw
ArgumentException. The fix detects read-only properties (no setter) and
adds items directly into the existing collection instance returned by
the getter, using AddObjectsIntoTargetCollection.

Fixes #1399

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

[ReflectionBasedXmlSerializer] To Support Serializing XmlSchemaObject

3 participants