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 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
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<linker>
<assembly fullname="System.Data.Common">
<type fullname="System.Data.DataColumn">
<method name="ResetCaption" />
<method name="ResetNamespace" />
<method name="ShouldSerializeCaption" />
<method name="ShouldSerializeDefaultValue" />
<method name="ShouldSerializeNamespace" />
</type>
<type fullname="System.Data.DataRow">
<method name="ResetLastChangedColumn" />
</type>
<type fullname="System.Data.DataSet">
<method name="ResetRelations" />
<method name="ResetTables" />
<method name="ShouldSerializeLocale" />
<method name="ShouldSerializeRelations" />
<method name="ShouldSerializeTables" />
</type>
<type fullname="System.Data.DataTable">
<method name="ResetCaseSensitive" />
<method name="ResetColumns" />
<method name="ResetConstraints" />
<method name="ResetIndexes" />
<method name="ResetNamespace" />
<method name="ResetPrimaryKey" />
<method name="ShouldSerializeCaseSensitive" />
<method name="ShouldSerializeLocale" />
<method name="ShouldSerializeNamespace" />
<method name="ShouldSerializePrimaryKey" />
</type>
</assembly>
</linker>
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);
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);
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);
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