-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Fix DataGrid native aot crash when sorting. #17248
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ba85205
Fix DataGrid native aot crash when sorting.
dameng324 a232d91
Update DataGridSortDescription.cs
dameng324 237b8bd
add customType test
dameng324 ae9bd3f
does not crash when property type is not sortable.
dameng324 295aa80
check `RuntimeFeature.IsDynamicCodeSupported` to fallback to reflecti…
dameng324 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
223 changes: 223 additions & 0 deletions
223
tests/Avalonia.Controls.DataGrid.UnitTests/Collections/ComparerTests.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,223 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using Avalonia.Collections; | ||
using Avalonia.Logging; | ||
using Xunit; | ||
|
||
namespace Avalonia.Controls.DataGridTests.Collections; | ||
|
||
public class NoStringTypeComparerTests | ||
{ | ||
public static IEnumerable<object[]> GetComparerForNotStringTypeParameters() | ||
{ | ||
yield return | ||
[ | ||
nameof(Item.IntProp1), | ||
(object item, object value) => | ||
{ | ||
(item as Item)!.IntProp1 = (int)value; | ||
}, | ||
(object item) => (object)(item as Item)!.IntProp1, | ||
new object[] { 2, 3, 1 }, | ||
new object[] { 1, 2, 3 } | ||
]; | ||
yield return | ||
[ | ||
nameof(Item.IntProp2), | ||
(object item, object value) => | ||
{ | ||
(item as Item)!.IntProp2 = (int?)value; | ||
}, | ||
(object item) => (object)(item as Item)!.IntProp2, | ||
new object[] { 2, 3, null, 1 }, | ||
new object[] { null, 1, 2, 3 } | ||
]; | ||
yield return | ||
[ | ||
nameof(Item.DoubleProp1), | ||
(object item, object value) => | ||
{ | ||
(item as Item)!.DoubleProp1 = (double)value; | ||
}, | ||
(object item) => (object)(item as Item)!.DoubleProp1, | ||
new object[] { 2.1, 3.1, 1.1 }, | ||
new object[] { 1.1, 2.1, 3.1 } | ||
]; | ||
yield return | ||
[ | ||
nameof(Item.DoubleProp2), | ||
(object item, object value) => | ||
{ | ||
(item as Item)!.DoubleProp2 = (double?)value; | ||
}, | ||
(object item) => (object)(item as Item)!.DoubleProp2, | ||
new object[] { 2.1, 3.1, null, 1.1 }, | ||
new object[] { null, 1.1, 2.1, 3.1 } | ||
]; | ||
yield return | ||
[ | ||
nameof(Item.DecimalProp1), | ||
(object item, object value) => | ||
{ | ||
(item as Item)!.DecimalProp1 = (decimal)value; | ||
}, | ||
(object item) => (object)(item as Item)!.DecimalProp1, | ||
new object[] { 2.1M, 3.1M, 1.1M }, | ||
new object[] { 1.1M, 2.1M, 3.1M } | ||
]; | ||
yield return | ||
[ | ||
nameof(Item.DecimalProp2), | ||
(object item, object value) => | ||
{ | ||
(item as Item)!.DecimalProp2 = (decimal?)value; | ||
}, | ||
(object item) => (object)(item as Item)!.DecimalProp2, | ||
new object[] { 2.1M, 3.1M, null, 1.1M }, | ||
new object[] { null, 1.1M, 2.1M, 3.1M } | ||
]; | ||
yield return | ||
[ | ||
nameof(Item.EnumProp1), | ||
(object item, object value) => | ||
{ | ||
(item as Item)!.EnumProp1 = (LogEventLevel)value; | ||
}, | ||
(object item) => (object)(item as Item)!.EnumProp1, | ||
new object[] { LogEventLevel.Information, LogEventLevel.Debug, LogEventLevel.Error }, | ||
new object[] { LogEventLevel.Debug, LogEventLevel.Information, LogEventLevel.Error } | ||
]; | ||
yield return | ||
[ | ||
nameof(Item.EnumProp2), | ||
(object item, object value) => | ||
{ | ||
(item as Item)!.EnumProp2 = (LogEventLevel?)value; | ||
}, | ||
(object item) => (object)(item as Item)!.EnumProp2, | ||
new object[] | ||
{ | ||
LogEventLevel.Information, | ||
LogEventLevel.Debug, | ||
null, | ||
LogEventLevel.Error | ||
}, | ||
new object[] | ||
{ | ||
null, | ||
LogEventLevel.Debug, | ||
LogEventLevel.Information, | ||
LogEventLevel.Error | ||
} | ||
]; | ||
yield return | ||
[ | ||
nameof(Item.CustomProp2), | ||
(object item, object value) => | ||
{ | ||
(item as Item)!.CustomProp2 = (CustomType?)value; | ||
}, | ||
(object item) => (object)(item as Item)!.CustomProp2, | ||
new object[] | ||
{ | ||
new CustomType() { Prop = 2 }, | ||
new CustomType() { Prop = 3 }, | ||
null, | ||
new CustomType() { Prop = 1 } | ||
}, | ||
new object[] | ||
{ | ||
null, | ||
new CustomType() { Prop = 1 }, | ||
new CustomType() { Prop = 2 }, | ||
new CustomType() { Prop = 3 } | ||
} | ||
]; | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(GetComparerForNotStringTypeParameters))] | ||
public void GetComparerForNotStringType_Correctly_WhenSorting( | ||
string pathName, | ||
Action<object, object> setAction, | ||
Func<object, object> getAction, | ||
object[] orignal, | ||
object[] ordered | ||
) | ||
{ | ||
List<Item> items = new(); | ||
for (int i = 0; i < orignal.Length; i++) | ||
{ | ||
var item = new Item(); | ||
setAction(item, orignal[i]); | ||
items.Add(item); | ||
} | ||
|
||
//Ascending | ||
var sortDescription = DataGridSortDescription.FromPath( | ||
pathName, | ||
ListSortDirection.Ascending | ||
); | ||
sortDescription.Initialize(typeof(Item)); | ||
var result = sortDescription.OrderBy(items).ToList(); | ||
|
||
for (int i = 0; i < ordered.Length; i++) | ||
{ | ||
Assert.Equal(ordered[i], getAction(result[i])); | ||
} | ||
|
||
//Descending | ||
sortDescription = DataGridSortDescription.FromPath(pathName, ListSortDirection.Descending); | ||
sortDescription.Initialize(typeof(Item)); | ||
result = sortDescription.OrderBy(items).ToList(); | ||
|
||
ordered = ordered.Reverse().ToArray(); | ||
for (int i = 0; i < ordered.Length; i++) | ||
{ | ||
Assert.Equal(ordered[i], getAction(result[i])); | ||
} | ||
} | ||
|
||
private class Item | ||
{ | ||
public int IntProp1 { get; set; } | ||
public int? IntProp2 { get; set; } | ||
public double DoubleProp1 { get; set; } | ||
public double? DoubleProp2 { get; set; } | ||
|
||
public decimal DecimalProp1 { get; set; } | ||
public decimal? DecimalProp2 { get; set; } | ||
|
||
public LogEventLevel EnumProp1 { get; set; } | ||
public LogEventLevel? EnumProp2 { get; set; } | ||
public CustomType? CustomProp2 { get; set; } | ||
} | ||
|
||
public struct CustomType : IComparable | ||
{ | ||
public int Prop { get; set; } | ||
|
||
public int CompareTo(object obj) | ||
{ | ||
if (obj is CustomType other) | ||
{ | ||
return Prop.CompareTo(other.Prop); | ||
} | ||
else | ||
{ | ||
return 1; | ||
} | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj is CustomType other) | ||
{ | ||
return Prop == other.Prop; | ||
} | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On NET6_0_OR_GREATER you can check for this property whether reflection is supported: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.runtimefeature.isdynamiccodesupported?view=net-8.0
On AOT builds IsDynamicCodeSupported returns false, and this fallback would make sense there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way we also won't regress any application that relies on this reflection sorting right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice suggestion. Done.