Skip to content

Remove unreachable NotImplementedException array-with-choice - #129491

Merged
StephenMolloy merged 5 commits into
dotnet:mainfrom
StephenMolloy:1400-remove-choice-array-guard-in-reflection-XmlSerializer
Jul 9, 2026
Merged

StephenMolloy merged 5 commits into
dotnet:mainfrom
StephenMolloy:1400-remove-choice-array-guard-in-reflection-XmlSerializer

Conversation

@StephenMolloy

Copy link
Copy Markdown
Contributor

WriteArray in ReflectionXmlSerializationReader threw NotImplementedException when memberMapping.ChoiceIdentifier was non-null. That memberMapping is a freshly constructed local whose ChoiceIdentifier is always null, so the throw was unreachable dead code. The choice value for an array-typed choice element is recorded by the calling WriteElement against the owning member after WriteArray returns, so the reflection-based serializer already round-trips these types correctly.

Add a regression test covering an [XmlChoiceIdentifier] member where one of the choice element types is itself an array (its element mapping is an ArrayMapping). The test runs in both the codegen and ReflectionOnly XmlSerializer test projects.

Fixes #1400
(Well, not really fixing, because the exception was impossible to hit. But also added some test coverage assurance just in case.)

…eflection-based XmlSerializer (dotnet#1400)

WriteArray in ReflectionXmlSerializationReader threw NotImplementedException when memberMapping.ChoiceIdentifier was non-null. That memberMapping is a freshly constructed local whose ChoiceIdentifier is always null, so the throw was unreachable dead code. The choice value for an array-typed choice element is recorded by the calling WriteElement against the owning member after WriteArray returns, so the reflection-based serializer already round-trips these types correctly.

Add a regression test covering an [XmlChoiceIdentifier] member where one of the choice element types is itself an array (its element mapping is an ArrayMapping). The test runs in both the codegen and ReflectionOnly XmlSerializer test projects.

Co-authored-by: Copilot <[email protected]>
@StephenMolloy StephenMolloy added this to the 11.0.0 milestone Jun 16, 2026
@StephenMolloy
StephenMolloy requested a review from mangod9 June 16, 2026 23:45
@StephenMolloy StephenMolloy self-assigned this Jun 16, 2026
Copilot AI review requested due to automatic review settings June 16, 2026 23:45

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 removes an unreachable NotImplementedException in the reflection-based XmlSerializer reader’s array-handling path and adds a regression test to ensure [XmlChoiceIdentifier] works when one of the choice element types is itself an array (ArrayMapping).

Changes:

  • Removed dead/unreachable memberMapping.ChoiceIdentifier != null throw in ReflectionXmlSerializationReader.WriteArray.
  • Added a new round-trip test covering an [XmlChoiceIdentifier] member where one choice element is an array (int[]), validating both value and populated choice-identifier array.
  • Added a small helper type + enum used by the new test.

Reviewed changes

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

File Description
src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs Removes unreachable NotImplementedException and documents why choice tracking is handled by the caller (WriteElement).
src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs Adds regression test and supporting types for array-typed choice elements ([XmlChoiceIdentifier] + int[]).

StephenMolloy and others added 2 commits June 29, 2026 17:36
TypeWithArrayLikeChoiceElement is declared in the test assembly, not in SerializableAssembly.dll, so the pregen (sgen) test project has no pregenerated serializer for it and fails at runtime.

Move the test and its supporting types from the shared XmlSerializerTests.cs (compiled into all three test projects, including pregen) into XmlSerializerTests.RuntimeOnly.cs, which is compiled only into the runtime-serializer projects (RefEmit/ILGen and ReflectionOnly). This preserves coverage in both runtime serializers - including the reflection path that validates the dotnet#1400 fix - while excluding only the pregen project.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings June 30, 2026 01:22

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

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

pr feedback

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings June 30, 2026 01:40

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

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

A prior review-feedback commit nested TypeWithArrayLikeChoiceElement and ArrayLikeChoice inside XmlSerializerTests. That broke serialization: the writer matches an [XmlChoiceIdentifier] value by comparing the choice enum's TypeDesc.FullName against choiceSource.GetType().FullName, but TypeDesc normalizes nested-type names by replacing '+' with '.' (Types.cs), so a nested enum's TypeDesc name never equals its reflection FullName. The choice failed to match and serialization threw 'The type System.String may not be used in this context.'

Move the types back to the top level (matching all existing choice tests, e.g. MoreChoices) and add a comment explaining why they cannot be nested. Also apply the reviewer's nameof suggestion for the XmlChoiceIdentifier target.

Co-authored-by: Copilot <[email protected]>
@StephenMolloy

Copy link
Copy Markdown
Contributor Author

/ba-g Test failure is unrelated

@StephenMolloy
StephenMolloy merged commit f681a89 into dotnet:main Jul 9, 2026
86 of 88 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-preview7 Jul 10, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
WriteArray in ReflectionXmlSerializationReader threw
NotImplementedException when memberMapping.ChoiceIdentifier was
non-null. That memberMapping is a freshly constructed local whose
ChoiceIdentifier is always null, so the throw was unreachable dead code.
The choice value for an array-typed choice element is recorded by the
calling WriteElement against the owning member after WriteArray returns,
so the reflection-based serializer already round-trips these types
correctly.

Add a regression test covering an [XmlChoiceIdentifier] member where one
of the choice element types is itself an array (its element mapping is
an ArrayMapping). The test runs in both the codegen and ReflectionOnly
XmlSerializer test projects.

Fixes #1400
(Well, not really fixing, because the exception was impossible to hit.
But also added some test coverage assurance just in case.)

---------

Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 10, 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.

[ReflectionBasedXmlSerializer] To Support ArrayMapping Types Having ChoiceIdentifier

3 participants