Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ private void WriteMemberElementsIf(Member[] expectedMembers, Member? anyElementM
{
string? ns = e!.Form == XmlSchemaForm.Qualified ? e.Namespace : string.Empty;
bool isList = member!.Mapping.TypeDesc!.IsArrayLike && !member.Mapping.TypeDesc.IsArray;
WriteElement(e, isList && member.Mapping.TypeDesc.IsNullable, member.Mapping.ReadOnly, ns, member.FixupIndex, fixup, member);
WriteElement(e, isList && member.Mapping.TypeDesc.IsNullable, member.Mapping.ReadOnly, ns, member.FixupIndex, fixup, member, elementIndex);
}
}
else
Expand All @@ -828,7 +828,7 @@ private void WriteMemberElementsIf(Member[] expectedMembers, Member? anyElementM
if (element.Any && element.Name.Length == 0)
{
string? ns = element.Form == XmlSchemaForm.Qualified ? element.Namespace : string.Empty;
WriteElement(element, false, false, ns, fixup: fixup, member: member);
WriteElement(element, false, false, ns, fixup: fixup, member: member, elementIndex: i);
break;
}
}
Expand All @@ -841,7 +841,7 @@ private void WriteMemberElementsIf(Member[] expectedMembers, Member? anyElementM
}
}

private object? WriteElement(ElementAccessor element, bool checkForNull, bool readOnly, string? defaultNamespace, int fixupIndex = -1, Fixup? fixup = null, Member? member = null)
private object? WriteElement(ElementAccessor element, bool checkForNull, bool readOnly, string? defaultNamespace, int fixupIndex = -1, Fixup? fixup = null, Member? member = null, int elementIndex = -1)
{
object? value = null;
if (element.Mapping is ArrayMapping arrayMapping)
Expand Down Expand Up @@ -1013,7 +1013,7 @@ private void WriteMemberElementsIf(Member[] expectedMembers, Member? anyElementM
throw new InvalidOperationException(SR.XmlInternalError);
}

member?.ChoiceSource?.Invoke(element.Name);
member?.ChoiceSource?.Invoke(elementIndex);

if (member?.ArraySource != null)
{
Expand Down Expand Up @@ -1744,19 +1744,20 @@ private static XmlSerializationCollectionFixupCallback GetCreateCollectionOfObje
ChoiceIdentifierAccessor? choice = mapping.ChoiceIdentifier;
if (choice != null && o != null)
{
member.ChoiceSource = (elementNameObject) =>
member.ChoiceSource = (elementIndex) =>
{
string? elementName = elementNameObject as string;
foreach (var name in choice.MemberIds!)
// The choice enum member that corresponds to the selected element is stored
// positionally in MemberIds, parallel to the member's Elements. This mirrors
// how the IL-generated reader resolves the choice (choice.MemberIds[elementIndex]),
// and honors [XmlEnum] aliases because the importer already resolved them.
string[]? memberIds = choice.MemberIds;
if (memberIds is null || (uint)elementIndex >= (uint)memberIds.Length)
{
if (name == elementName)
{
object choiceValue = Enum.Parse(choice.Mapping!.TypeDesc!.Type!, name);
SetOrAddValueToMember(o, choiceValue, choice.MemberInfo!);

break;
}
return;
}

object choiceValue = Enum.Parse(choice.Mapping!.TypeDesc!.Type!, memberIds[elementIndex]);
SetOrAddValueToMember(o, choiceValue, choice.MemberInfo!);
};
}

Expand Down Expand Up @@ -2107,7 +2108,7 @@ internal sealed class Member
public Func<object?>? GetSource;
public Action<object>? ArraySource;
public Action<object?>? CheckSpecifiedSource;
public Action<object>? ChoiceSource;
public Action<int>? ChoiceSource;
public Action<string, string>? XmlnsSource;
public Action<object>? EnsureCollection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3152,6 +3152,22 @@ public static void Xml_TypeWithArrayPropertyHavingChoiceErrors()
Assert.Contains("is missing enumeration value", ex.Message);
}

[Fact]
public static void Xml_TypeWithAliasedChoiceIdentifier()
{
var value = new TypeWithAliasedChoiceIdentifier()
{
Item = 42,
ChoiceType = AliasedChoiceType.NumberChoice
};

var actual = SerializeAndDeserialize(value, WithXmlHeader("<TypeWithAliasedChoiceIdentifier xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Number>42</Number></TypeWithAliasedChoiceIdentifier>"));

Assert.NotNull(actual);
Assert.Equal(value.Item, actual.Item);
Assert.Equal(value.ChoiceType, actual.ChoiceType);
}

[Fact]
public static void Xml_XmlIncludedTypesInTypedCollection()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,25 @@ public class TypeWithPropertyHavingChoiceError
public MoreChoices[] ChoiceArray;
}

public enum AliasedChoiceType
{
[XmlEnum("Word")]
WordChoice,
[XmlEnum("Number")]
NumberChoice,
}

public class TypeWithAliasedChoiceIdentifier
{
[XmlChoiceIdentifier("ChoiceType")]
[XmlElement("Word", typeof(string))]
[XmlElement("Number", typeof(int))]
public object Item;

[XmlIgnore]
public AliasedChoiceType ChoiceType;
}

internal class MyFileStreamSurrogateProvider : ISerializationSurrogateProvider
{
static MyFileStreamSurrogateProvider()
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.