Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't trim ShouldSerializeXXX and ResetXXX members that may be called with reflection #102780

Merged
merged 5 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -121,6 +121,11 @@ public DataColumn(string? columnName, [DynamicallyAccessedMembers(DynamicallyAcc
/// column is an attribute.
/// </summary>
[RequiresUnreferencedCode("Members from serialized types or types used in expressions may be trimmed if not referenced directly.")]
[DynamicDependency(nameof(ShouldSerializeCaption))]
[DynamicDependency(nameof(ShouldSerializeDefaultValue))]
[DynamicDependency(nameof(ShouldSerializeNamespace))]
[DynamicDependency(nameof(ResetCaption))]
[DynamicDependency(nameof(ResetNamespace))]
public DataColumn(string? columnName, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] Type dataType, string? expr, MappingType type)
{
GC.SuppressFinalize(this);
Expand Down
5 changes: 5 additions & 0 deletions src/libraries/System.Data.Common/src/System/Data/DataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public class DataSet : MarshalByValueComponent, IListSource, IXmlSerializable, I
/// <summary>
/// Initializes a new instance of the <see cref='System.Data.DataSet'/> class.
/// </summary>
[DynamicDependency(nameof(ShouldSerializeLocale))]
[DynamicDependency(nameof(ShouldSerializeRelations))]
[DynamicDependency(nameof(ShouldSerializeTables))]
[DynamicDependency(nameof(ResetRelations))]
[DynamicDependency(nameof(ResetTables))]
public DataSet()
{
GC.SuppressFinalize(this);
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Data.Common/src/System/Data/DataTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ public class DataTable : MarshalByValueComponent, IListSource, ISupportInitializ
/// <summary>
/// Initializes a new instance of the <see cref='System.Data.DataTable'/> class with no arguments.
/// </summary>
[DynamicDependency(nameof(ShouldSerializeCaseSensitive))]
[DynamicDependency(nameof(ShouldSerializeLocale))]
[DynamicDependency(nameof(ShouldSerializeNamespace))]
[DynamicDependency(nameof(ShouldSerializePrimaryKey))]
[DynamicDependency(nameof(ResetCaseSensitive))]
[DynamicDependency(nameof(ResetColumns))]
[DynamicDependency(nameof(ResetConstraints))]
[DynamicDependency(nameof(ResetIndexes))]
[DynamicDependency(nameof(ResetNamespace))]
[DynamicDependency(nameof(ResetPrimaryKey))]
public DataTable()
{
GC.SuppressFinalize(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

using System.ComponentModel;
using System.Data.SqlTypes;

using System.Reflection;
using Xunit;

namespace System.Data.Tests
Expand Down Expand Up @@ -793,6 +793,54 @@ public void NonSqlNullableType_RequiresPublicStaticNull()
Assert.Throws<ArgumentException>(() => t.Columns.Add("c3", typeof(NullableTypeWithoutNullMember)));
}

[Fact]
public void MethodsCalledByReflectionSerializersAreNotTrimmed()
{
Assert.True(ShouldSerializeExists(nameof(DataColumn.Caption)));
Assert.True(ShouldSerializeExists(nameof(DataColumn.DefaultValue)));
Assert.True(ShouldSerializeExists(nameof(DataColumn.Namespace)));

Assert.True(ResetExists(nameof(DataColumn.Caption)));
Assert.False(ResetExists(nameof(DataColumn.DefaultValue)));
Assert.True(ResetExists(nameof(DataColumn.Namespace)));

bool ShouldSerializeExists(string name) => typeof(DataColumn).GetMethod("ShouldSerialize" + name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) != null;
bool ResetExists(string name) => typeof(DataColumn).GetMethod("Reset" + name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) != null;
}

[Fact]
public void MethodsCalledByReflectionSerializersAreNotTrimmedUsingTypeDescriptor()
{
DataColumn dc = new DataColumn
{
ColumnName = "dataColumn",
DataType = typeof(DateTime)
};

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(dc);

Assert.False(properties[nameof(DataColumn.DefaultValue)].ShouldSerializeValue(dc));
dc.DefaultValue = DateTime.MinValue;
Assert.True(properties[nameof(DataColumn.DefaultValue)].ShouldSerializeValue(dc));
properties[nameof(DataColumn.DefaultValue)].ResetValue(dc); // Reset method is not available
Assert.Equal(DateTime.MinValue, dc.DefaultValue);
Assert.True(properties[nameof(DataColumn.DefaultValue)].ShouldSerializeValue(dc));

Assert.False(properties[nameof(DataColumn.Caption)].ShouldSerializeValue(dc));
dc.Caption = "Caption";
Assert.True(properties[nameof(DataColumn.Caption)].ShouldSerializeValue(dc));
properties[nameof(DataColumn.Caption)].ResetValue(dc); // Reset method is available
Assert.False(properties[nameof(DataColumn.Caption)].ShouldSerializeValue(dc));
Assert.Equal("dataColumn", dc.Caption);

Assert.False(properties[nameof(DataColumn.Namespace)].ShouldSerializeValue(dc));
dc.Namespace = "Namespace";
Assert.True(properties[nameof(DataColumn.Namespace)].ShouldSerializeValue(dc));
properties[nameof(DataColumn.Namespace)].ResetValue(dc); // Reset method is available
Assert.False(properties[nameof(DataColumn.Namespace)].ShouldSerializeValue(dc));
Assert.Equal("", dc.Namespace);
}

private sealed class NullableTypeWithNullProperty : INullable
{
public bool IsNull => true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


using System.Reflection;
using Xunit;

namespace System.Data.Tests
Expand Down Expand Up @@ -876,5 +876,15 @@ public void SetParent_missing_ParentRow()
Assert.Equal(DBNull.Value, childRow[childColumn1]);
Assert.Equal("value", childRow[childColumn2]);
}

[Fact]
public void MethodsCalledByReflectionSerializersAreNotTrimmed()
{
Assert.False(ShouldSerializeExists("LastChangedColumn"));
Assert.True(ResetExists("LastChangedColumn"));

bool ShouldSerializeExists(string name) => typeof(DataRow).GetProperty("ShouldSerialize" + name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) != null;
bool ResetExists(string name) => typeof(DataRow).GetMethod("Reset" + name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) != null;
}
}
}
16 changes: 16 additions & 0 deletions src/libraries/System.Data.Common/tests/System/Data/DataSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
using System.Tests;
using System.Reflection;

namespace System.Data.Tests
{
Expand Down Expand Up @@ -1621,6 +1622,21 @@ static void RunTest()
}
}

[Fact]
public void MethodsCalledByReflectionSerializersAreNotTrimmed()
{
Assert.True(ShouldSerializeExists(nameof(DataSet.Relations)));
Assert.True(ShouldSerializeExists(nameof(DataSet.Tables)));
Assert.True(ShouldSerializeExists(nameof(DataSet.Locale)));

Assert.True(ResetExists(nameof(DataSet.Relations)));
Assert.True(ResetExists(nameof(DataSet.Tables)));
Assert.False(ResetExists(nameof(DataSet.Locale)));

bool ShouldSerializeExists(string name) => typeof(DataSet).GetMethod("ShouldSerialize" + name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) != null;
bool ResetExists(string name) => typeof(DataSet).GetMethod("Reset" + name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) != null;
}

#region DataSet.CreateDataReader Tests and DataSet.Load Tests

private DataSet _ds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Tests;
using System.Tests;
Expand Down Expand Up @@ -1818,6 +1819,60 @@ public void TableInitializedEventTest4()
dt.Initialized -= new EventHandler(OnTableInitialized);
}

[Fact]
public void MethodsCalledByReflectionSerializersAreNotTrimmed()
{
Assert.True(ShouldSerializeExists(nameof(DataTable.CaseSensitive)));
Assert.False(ShouldSerializeExists("Columns"));
Assert.False(ShouldSerializeExists("Constraints"));
Assert.False(ShouldSerializeExists("Indexes"));
Assert.True(ShouldSerializeExists(nameof(DataTable.Locale)));
Assert.True(ShouldSerializeExists(nameof(DataTable.Namespace)));
Assert.True(ShouldSerializeExists(nameof(DataTable.PrimaryKey)));

Assert.True(ResetExists(nameof(DataTable.CaseSensitive)));
Assert.True(ResetExists("Columns"));
Assert.True(ResetExists("Constraints"));
Assert.True(ResetExists("Indexes"));
Assert.False(ResetExists(nameof(DataTable.Locale)));
Assert.True(ResetExists(nameof(DataTable.Namespace)));
Assert.True(ResetExists(nameof(DataTable.PrimaryKey)));

bool ShouldSerializeExists(string name) => typeof(DataTable).GetMethod("ShouldSerialize" + name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) != null;
bool ResetExists(string name) => typeof(DataTable).GetMethod("Reset" + name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) != null;
}

[Fact]
public void MethodsCalledByReflectionSerializersAreNotTrimmedUsingTypeDescriptor()
{
DataTable dt = new DataTable();
dt.CaseSensitive = true;
dt.Locale = new CultureInfo("en-US");
dt.PrimaryKey = new DataColumn[] { dt.Columns.Add("id", typeof(int)) };
dt.Namespace = "NS";

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(dt);

Assert.True(properties[nameof(DataTable.PrimaryKey)].ShouldSerializeValue(dt));
properties[nameof(DataTable.PrimaryKey)].ResetValue(dt);
Assert.False(properties[nameof(DataTable.PrimaryKey)].ShouldSerializeValue(dt));
Assert.Equal(0, dt.PrimaryKey.Length);

Assert.True(properties[nameof(DataTable.CaseSensitive)].ShouldSerializeValue(dt));
properties[nameof(DataTable.CaseSensitive)].ResetValue(dt);
Assert.False(properties[nameof(DataTable.CaseSensitive)].ShouldSerializeValue(dt));
Assert.False(dt.CaseSensitive);

Assert.True(properties[nameof(DataTable.Locale)].ShouldSerializeValue(dt));
properties[nameof(DataTable.Locale)].ResetValue(dt);
Assert.True(properties[nameof(DataTable.Locale)].ShouldSerializeValue(dt)); // Reset method is not available

Assert.True(properties[nameof(DataTable.Namespace)].ShouldSerializeValue(dt));
properties[nameof(DataTable.Namespace)].ResetValue(dt);
Assert.False(properties[nameof(DataTable.Namespace)].ShouldSerializeValue(dt));
Assert.Equal("", dt.Namespace);
}

private void OnTableInitialized(object src, EventArgs args)
{
_tableInitialized = true;
Expand Down
Loading