Skip to content

Commit

Permalink
Use scopes to resolve array naming issue. (#55451)
Browse files Browse the repository at this point in the history
* Use scope to avoid using duplicate names for arrays in ILGen serializer.

* Remove comment.
  • Loading branch information
StephenMolloy authored Jul 13, 2021
1 parent 88112bc commit 1749deb
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,7 @@ private void WriteMember(SourceInfo source, AttributeAccessor attribute, TypeDes
string aiVar = "ai" + memberTypeDesc.Name;
string iVar = "i";
string fullTypeName = memberTypeDesc.CSharpName;
ilg.EnterScope();
WriteArrayLocalDecl(fullTypeName, aVar, source, memberTypeDesc);
if (memberTypeDesc.IsNullable)
{
Expand Down Expand Up @@ -1361,6 +1362,8 @@ private void WriteMember(SourceInfo source, AttributeAccessor attribute, TypeDes
{
ilg.EndIf();
}

ilg.ExitScope();
}
else
{
Expand Down Expand Up @@ -1435,6 +1438,7 @@ private void WriteArray(SourceInfo source, string? choiceSource, ElementAccessor
if (elements.Length == 0 && text == null) return;
string arrayTypeName = arrayTypeDesc.CSharpName;
string aName = "a" + arrayTypeDesc.Name;
ilg.EnterScope();
WriteArrayLocalDecl(arrayTypeName, aName, source, arrayTypeDesc);
LocalBuilder aLoc = ilg.GetLocal(aName);
if (arrayTypeDesc.IsNullable)
Expand Down Expand Up @@ -1486,6 +1490,8 @@ private void WriteArray(SourceInfo source, string? choiceSource, ElementAccessor
{
ilg.EndIf();
}

ilg.ExitScope();
}

[RequiresUnreferencedCode("calls WriteElements")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,44 @@ public static void Xml_TypeWithDateTimePropertyAsXmlTime()
}
}

[Fact]
public static void Xml_NamespaceTypeNameClashTest()
{
var serializer = new XmlSerializer(typeof(NamespaceTypeNameClashContainer));

Assert.NotNull(serializer);

var root = new NamespaceTypeNameClashContainer
{
A = new[] { new SerializationTypes.TypeNameClashA.TypeNameClash { Name = "N1" }, new SerializationTypes.TypeNameClashA.TypeNameClash { Name = "N2" } },
B = new[] { new SerializationTypes.TypeNameClashB.TypeNameClash { Name = "N3" } }
};

var xml = @"<?xml version=""1.0""?>
<Root xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<A>
<Name>N1</Name>
</A>
<A>
<Name>N2</Name>
</A>
<B>
<Name>N3</Name>
</B>
</Root>";

var actualRoot = SerializeAndDeserialize<NamespaceTypeNameClashContainer>(root, xml);

Assert.NotNull(actualRoot);
Assert.NotNull(actualRoot.A);
Assert.NotNull(actualRoot.B);
Assert.Equal(root.A.Length, actualRoot.A.Length);
Assert.Equal(root.B.Length, actualRoot.B.Length);
Assert.Equal(root.A[0].Name, actualRoot.A[0].Name);
Assert.Equal(root.A[1].Name, actualRoot.A[1].Name);
Assert.Equal(root.B[0].Name, actualRoot.B[0].Name);
}

[Fact]
public static void Xml_ArrayAsGetSet()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,35 @@ public class TypeWithKnownTypesOfCollectionsWithConflictingXmlName
public object Value2 = new SimpleType[1];

}

namespace TypeNameClashA
{
[System.Xml.Serialization.XmlType("TypeClashA")]
public class TypeNameClash
{
public string Name { get; set; }
}
}

namespace TypeNameClashB
{
[System.Xml.Serialization.XmlType("TypeClashB")]
public class TypeNameClash
{
public string Name { get; set; }
}
}

[System.Xml.Serialization.XmlRootAttribute("Root")]
[System.Xml.Serialization.XmlType("ContainerType")]
public class NamespaceTypeNameClashContainer
{
[System.Xml.Serialization.XmlElementAttribute("A")]
public TypeNameClashA.TypeNameClash[] A { get; set; }

[System.Xml.Serialization.XmlElementAttribute("B")]
public TypeNameClashB.TypeNameClash[] B { get; set; }
}
}

public class TypeWithXmlElementProperty
Expand Down Expand Up @@ -921,7 +950,6 @@ public class TypeWithXmlNodeArrayProperty
public XmlNode[] CDATA { get; set; }
}


public class Animal
{
public int Age;
Expand Down

0 comments on commit 1749deb

Please sign in to comment.