-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XmlSerializer.Serialize doesn't work when using TrimMode=link (#44772)
* XmlSerializer.Serialize doesn't work when using TrimMode=link Make XmlSerializer work correctly with ILLinker trimming. 1. Use constant BindingFlags to work around dotnet/linker#1617 2. Annotate CodeGenerator.CreateTypeBuilder correctly to preserve base class members Fix #41389 * Update ILLinker suppressions file. * PR feedback. Fix ILLinker suppresions.
- Loading branch information
Showing
5 changed files
with
232 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
10 changes: 10 additions & 0 deletions
10
src/libraries/System.Private.Xml/tests/TrimmingTests/System.Xml.TrimmingTests.proj
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project DefaultTargets="Build"> | ||
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.props))" /> | ||
|
||
<ItemGroup> | ||
<TestConsoleAppSourceFiles Include="XmlSerializer.Deserialize.cs" /> | ||
<TestConsoleAppSourceFiles Include="XmlSerializer.Serialize.cs" /> | ||
</ItemGroup> | ||
|
||
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.targets))" /> | ||
</Project> |
86 changes: 86 additions & 0 deletions
86
src/libraries/System.Private.Xml/tests/TrimmingTests/XmlSerializer.Deserialize.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Xml.Schema; | ||
|
||
namespace System.Xml.Serialization.TrimmingTests | ||
{ | ||
internal class Program | ||
{ | ||
// Preserve these types until XmlSerializer is fully trim-safe. | ||
// see https://github.com/dotnet/runtime/issues/44768 | ||
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Response))] | ||
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(DataUpdates))] | ||
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(DataUpdatesDataUpdateInfo))] | ||
public static int Main() | ||
{ | ||
using StringReader stringReader = new StringReader(@"<?xml version=""1.0"" encoding=""UTF-8""?> | ||
<Response> | ||
<DataUpdates> | ||
<DataUpdateInfo DataDate=""2009-04-13T00:00:00"" DataType=""Data"" LastUpdatedDate=""2010-12-12T02:53:19.257"" /> | ||
<DataUpdateInfo DataDate=""2009-04-14T00:00:00"" DataType=""Data"" LastUpdatedDate=""2010-12-12T02:53:19.257"" /> | ||
<DataUpdateInfo DataDate=""2009-04-15T00:00:00"" DataType=""Data"" LastUpdatedDate=""2010-12-12T01:52:51.047"" /> | ||
</DataUpdates> | ||
</Response>"); | ||
|
||
Response obj = (Response)new XmlSerializer(typeof(Response)).Deserialize(stringReader); | ||
if (obj.DataUpdates.DataUpdateInfo.Count == 3 && | ||
obj.DataUpdates.DataUpdateInfo.All(i => i.DataDate.Year == 2009 && i.LastUpdatedDate.Year == 2010)) | ||
{ | ||
return 100; | ||
} | ||
|
||
return -1; | ||
} | ||
} | ||
|
||
[Serializable] | ||
[XmlType(AnonymousType = true)] | ||
[XmlRoot(Namespace = "", IsNullable = false)] | ||
public class Response | ||
{ | ||
public Response() | ||
{ | ||
this.DataUpdates = new DataUpdates(); | ||
} | ||
|
||
[XmlElement(Order = 0)] | ||
public DataUpdates DataUpdates { get; set; } | ||
} | ||
|
||
[Serializable] | ||
[XmlType(AnonymousType = true)] | ||
[XmlRoot(Namespace = "", IsNullable = false)] | ||
public class DataUpdates | ||
{ | ||
public DataUpdates() | ||
{ | ||
this.DataUpdateInfo = new List<DataUpdatesDataUpdateInfo>(); | ||
} | ||
|
||
[XmlElement("DataUpdateInfo", Form = XmlSchemaForm.Unqualified, Order = 0)] | ||
public List<DataUpdatesDataUpdateInfo> DataUpdateInfo { get; set; } | ||
} | ||
|
||
[Serializable] | ||
[XmlType(AnonymousType = true)] | ||
public class DataUpdatesDataUpdateInfo | ||
{ | ||
public DataUpdatesDataUpdateInfo() | ||
{ | ||
} | ||
|
||
[XmlAttribute] | ||
public DateTime DataDate { get; set; } | ||
|
||
[XmlAttribute] | ||
public string DataType { get; set; } | ||
|
||
[XmlAttribute] | ||
public DateTime LastUpdatedDate { get; set; } | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/libraries/System.Private.Xml/tests/TrimmingTests/XmlSerializer.Serialize.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Xml.Schema; | ||
|
||
namespace System.Xml.Serialization.TrimmingTests | ||
{ | ||
internal class Program | ||
{ | ||
// Preserve these types until XmlSerializer is fully trim-safe. | ||
// see https://github.com/dotnet/runtime/issues/44768 | ||
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Response))] | ||
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(DataUpdates))] | ||
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(DataUpdatesDataUpdateInfo))] | ||
public static int Main() | ||
{ | ||
Response obj = new Response(); | ||
obj.DataUpdates.DataUpdateInfo.Add(new DataUpdatesDataUpdateInfo() | ||
{ | ||
DataDate = new DateTime(2009, 4, 13), | ||
DataType = "Data", | ||
LastUpdatedDate = new DateTime(2010, 12, 12) | ||
}); | ||
obj.DataUpdates.DataUpdateInfo.Add(new DataUpdatesDataUpdateInfo() | ||
{ | ||
DataDate = new DateTime(2009, 4, 14), | ||
DataType = "Data", | ||
LastUpdatedDate = new DateTime(2010, 12, 12) | ||
}); | ||
|
||
using StringWriter writer = new StringWriter(); | ||
new XmlSerializer(typeof(Response)).Serialize(writer, obj); | ||
string serialized = writer.ToString(); | ||
|
||
if (serialized.Contains("<Response") && | ||
serialized.Contains("<DataUpdates>") && | ||
serialized.Contains(@"<DataUpdateInfo DataDate=""2009-04-13T00:00:00"" DataType=""Data"" LastUpdatedDate=""2010-12-12T00:00:00"" />") && | ||
serialized.Contains(@"<DataUpdateInfo DataDate=""2009-04-14T00:00:00"" DataType=""Data"" LastUpdatedDate=""2010-12-12T00:00:00"" />")) | ||
{ | ||
return 100; | ||
} | ||
|
||
return -1; | ||
} | ||
} | ||
|
||
[Serializable] | ||
[XmlType(AnonymousType = true)] | ||
[XmlRoot(Namespace = "", IsNullable = false)] | ||
public class Response | ||
{ | ||
public Response() | ||
{ | ||
this.DataUpdates = new DataUpdates(); | ||
} | ||
|
||
[XmlElement(Order = 0)] | ||
public DataUpdates DataUpdates { get; set; } | ||
} | ||
|
||
[Serializable] | ||
[XmlType(AnonymousType = true)] | ||
[XmlRoot(Namespace = "", IsNullable = false)] | ||
public class DataUpdates | ||
{ | ||
public DataUpdates() | ||
{ | ||
this.DataUpdateInfo = new List<DataUpdatesDataUpdateInfo>(); | ||
} | ||
|
||
[XmlElement("DataUpdateInfo", Form = XmlSchemaForm.Unqualified, Order = 0)] | ||
public List<DataUpdatesDataUpdateInfo> DataUpdateInfo { get; set; } | ||
} | ||
|
||
[Serializable] | ||
[XmlType(AnonymousType = true)] | ||
public class DataUpdatesDataUpdateInfo | ||
{ | ||
public DataUpdatesDataUpdateInfo() | ||
{ | ||
} | ||
|
||
[XmlAttribute] | ||
public DateTime DataDate { get; set; } | ||
|
||
[XmlAttribute] | ||
public string DataType { get; set; } | ||
|
||
[XmlAttribute] | ||
public DateTime LastUpdatedDate { get; set; } | ||
} | ||
} |