Repository navigation
Remove unreachable NotImplementedException array-with-choice - #129491
Merged
StephenMolloy merged 5 commits intoJul 9, 2026
Merged
StephenMolloy merged 5 commits into
StephenMolloy merged 5 commits into
Conversation
…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]>
Contributor
There was a problem hiding this comment.
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 != nullthrow inReflectionXmlSerializationReader.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[]). |
Open
3 tasks
mangod9
approved these changes
Jun 17, 2026
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]>
pr feedback Co-authored-by: Copilot Autofix powered by AI <[email protected]>
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]>
mangod9
approved these changes
Jul 8, 2026
mangod9
approved these changes
Jul 8, 2026
Contributor
Author
|
/ba-g Test failure is unrelated |
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.)