Skip to content

Commit

Permalink
Fixes to support multi-dimensional arrays
Browse files Browse the repository at this point in the history
Fixes #26975
  • Loading branch information
roji committed Apr 25, 2022
1 parent 459a025 commit 5b2b58c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,25 @@ private static void FormatParameterValue(StringBuilder builder, object? paramete
case IList list:
builder.Append("{ ");

for (var i = 0; i < list.Count; i++)
// Note: multi-dimensional arrays implement non-generic IList. They do not support indexing, but do support enumeration.
var isFirst = true;
foreach (var element in list.Cast<object>().Take(5))
{
if (i > 4)
if (isFirst)
{
builder.Append("...");
break;
isFirst = false;
}

FormatParameterValue(builder, list[i]);

if (i < list.Count - 1)
else
{
builder.Append(", ");
}

FormatParameterValue(builder, element);
}

if (list.Count > 5)
{
builder.Append(", ...");
}

builder.Append(" }");
Expand Down
5 changes: 4 additions & 1 deletion src/EFCore/ChangeTracking/ValueComparer`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public ValueComparer(
var param1 = Expression.Parameter(type, "v1");
var param2 = Expression.Parameter(type, "v2");

// We exclude multi-dimensional arrays even though they're IStructuralEquatable because of
// https://github.com/dotnet/runtime/issues/66472
if (typeof(IStructuralEquatable).IsAssignableFrom(type))
{
return Expression.Lambda<Func<T?, T?, bool>>(
Expand Down Expand Up @@ -172,7 +174,8 @@ public ValueComparer(
protected static Expression<Func<T, T>> CreateDefaultSnapshotExpression(bool favorStructuralComparisons)
{
if (!favorStructuralComparisons
|| !typeof(T).IsArray)
|| !typeof(T).IsArray
|| typeof(T).GetArrayRank() != 1)
{
return v => v;
}
Expand Down
3 changes: 1 addition & 2 deletions test/EFCore.Tests/Storage/ValueComparerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void Throws_for_comparer_with_wrong_type()
[InlineData(typeof(float), (float)1, (float)2, null)]
[InlineData(typeof(double), (double)1, (double)2, null)]
[InlineData(typeof(JustAnEnum), JustAnEnum.A, JustAnEnum.B, null)]
[InlineData(typeof(int[]), new[] { 1, 2 }, new[] { 3, 4 }, null)]
public ValueComparer Default_comparer_works_for_normal_types(Type type, object value1, object value2, int? hashCode)
=> CompareTest(type, value1, value2, hashCode);

Expand Down Expand Up @@ -94,8 +95,6 @@ private static ValueComparer CompareTest(Type type, object value1, object value2
Assert.False(keyComparer.Equals(null, value2));
Assert.True(keyComparer.Equals(null, null));

Assert.Equal(hashCode ?? value1.GetHashCode(), keyComparer.GetHashCode(value1));

return comparer;
}

Expand Down

0 comments on commit 5b2b58c

Please sign in to comment.