Repository navigation
Description
Note
This issue was authored with the assistance of GitHub Copilot. The analysis and repro were verified against the dotnet/runtime build.
Description
The reflection-based XmlSerializer reader (ReflectionXmlSerializationReader) resolves the [XmlChoiceIdentifier] enum value incorrectly when the choice enum uses [XmlEnum] aliases (i.e., the enum member's C# identifier differs from the XML element name). In that case it leaves the choice identifier member at its default value instead of the value that corresponds to the element actually read.
The ILGen reader (XmlSerializationReaderILGen) and the C# code-gen reader (XmlSerializationReaderCodeGen) handle this correctly. So the behavior depends on which serialization backend is in use for the same public API, same types, and same XML.
This is independent of, and was discovered while working on, #1401.
Reproduction
public enum AliasedChoiceType
{
[XmlEnum("Word")]
WordChoice,
[XmlEnum("Number")]
NumberChoice,
}
public class AliasedChoiceContainer
{
[XmlChoiceIdentifier("ChoiceType")]
[XmlElement("Word", typeof(string))]
[XmlElement("Number", typeof(int))]
public object Item;
[XmlIgnore]
public AliasedChoiceType ChoiceType;
}Deserialize:
<?xml version="1.0"?>
<AliasedChoiceContainer><Number>42</Number></AliasedChoiceContainer>Expected
Item == 42 and ChoiceType == AliasedChoiceType.NumberChoice (the element read was <Number>).
Actual
- ILGen reader:
ChoiceType == NumberChoice✔ (correct) - Reflection reader:
ChoiceType == WordChoice✘ (left atdefault(AliasedChoiceType), which is the first member)
Item is deserialized correctly in both readers; only the choice-identifier member is wrong in the reflection reader.
Round-trip self-inconsistency
This is not a "stricter but defensible" difference — the reflection serializer does not round-trip with its own writer. ReflectionXmlSerializationWriter emits <Number> for ChoiceType == NumberChoice, but the reflection reader then reads <Number> back as WordChoice. Serialize-then-deserialize through the same serializer silently changes the value.
Root cause
ChoiceIdentifierAccessor.MemberIds[i] is populated by XmlReflectionImporter (around XmlReflectionImporter.cs:1992) to hold the enum member's C# identifier (Constants[j].Name), position-aligned to Elements[i] by matching on the constant's XmlName.
- The codegen readers index this position-aligned array:
choice.MemberIds[elementIndex](e.g.XmlSerializationReaderILGen.cs:3264,XmlSerializationReader.cs:4929). The matched element's index yields the correct constant identifier regardless of aliasing. - The reflection reader instead compares each
MemberIdsentry (a C# identifier) directly against the incoming XML element name (ReflectionXmlSerializationReader.cs,member.ChoiceSourcearound line 1786):
foreach (var name in choice.MemberIds!)
{
if (name == elementName) // comparing identifier against XML element name
{
object choiceValue = Enum.Parse(choice.Mapping!.TypeDesc!.Type!, name);
SetOrAddValueToMember(o, choiceValue, choice.MemberInfo!);
break;
}
}This only works when the enum identifier happens to equal the XML element name. Under [XmlEnum] aliasing it matches nothing, so the choice member is never set.
Suggested fix
Make the reflection reader's ChoiceSource match the incoming element name against each constant's XmlName (mirroring ReflectionXmlSerializationWriter.FindChoiceEnumValue, around ReflectionXmlSerializationWriter.cs:300) and assign the corresponding Constants[j].Name. The codegen readers need no change.
Notes
- Only the reflection-based reader is affected. NativeAOT and
Mode.ReflectionOnlyscenarios use this reader. - The common case where enum members are named identically to their XML elements works in all backends; only aliased (or otherwise non-identifier element names) trigger the bug.
Activity
Fix reflection XmlSerializer choice identifier with XmlEnum aliases (#…