Skip to content

Commit 81a763d

Browse files
authored
Fix NRE in a filter by an associated object (#487)
1 parent fa19b4f commit 81a763d

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using DevExpress.Xpo;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
9+
namespace DevExtreme.AspNet.Data.Tests.Xpo {
10+
public class Bug486 {
11+
12+
[Persistent(nameof(Bug486) + "_" + nameof(Department))]
13+
public class Department {
14+
[Key]
15+
public Guid ID { get; set; }
16+
}
17+
18+
[Persistent(nameof(Bug486) + "_" + nameof(Employee))]
19+
public class Employee {
20+
[Key]
21+
public Guid ID { get; set; }
22+
23+
[Association("Department-Employees")]
24+
public Department Department { get; set; }
25+
}
26+
27+
[Fact]
28+
public async Task Scenario() {
29+
await UnitOfWorkHelper.ExecAsync(uow => {
30+
{
31+
var department = new Department();
32+
var employee = new Employee {
33+
Department = department
34+
};
35+
36+
uow.Save(department);
37+
uow.Save(employee);
38+
39+
uow.CommitChanges();
40+
}
41+
{
42+
var employees = uow.Query<Employee>();
43+
var department = uow.Query<Department>().First();
44+
45+
var loadOptions = new SampleLoadOptions {
46+
Filter = new object[] {
47+
nameof(Employee.Department),
48+
department
49+
}
50+
};
51+
52+
var exception = Record.Exception(() => DataSourceLoader.Load(employees, loadOptions));
53+
54+
Assert.Null(exception);
55+
}
56+
});
57+
}
58+
59+
}
60+
}

net/DevExtreme.AspNet.Data/DataSourceExpressionBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void AddFilter(IList filterOverride = null) {
5151
if(filterOverride != null || Context.HasFilter) {
5252
var filterExpr = filterOverride != null && filterOverride.Count < 1
5353
? Expression.Lambda(Expression.Constant(false), Expression.Parameter(GetItemType()))
54-
: new FilterExpressionCompiler(GetItemType(), Context.GuardNulls, Context.UseStringToLower).Compile(filterOverride ?? Context.Filter);
54+
: new FilterExpressionCompiler(GetItemType(), Context.GuardNulls, Context.UseStringToLower, Context.SupportsEqualsMethod).Compile(filterOverride ?? Context.Filter);
5555

5656
Expr = QueryableCall(nameof(Queryable.Where), Expression.Quote(filterExpr));
5757
}

net/DevExtreme.AspNet.Data/DataSourceLoadContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ partial class DataSourceLoadContext {
7676
public IList Filter => _options.Filter;
7777
public bool HasFilter => !IsEmptyList(_options.Filter);
7878
public bool UseStringToLower => _options.StringToLower ?? DataSourceLoadOptionsBase.StringToLowerDefault ?? _providerInfo.IsLinqToObjects;
79+
public bool SupportsEqualsMethod => !_providerInfo.IsXPO;
7980
}
8081

8182
// Grouping

net/DevExtreme.AspNet.Data/FilterExpressionCompiler.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ const string
1919

2020
bool _stringToLower;
2121

22-
public FilterExpressionCompiler(Type itemType, bool guardNulls, bool stringToLower = false)
22+
readonly bool _supportsEqualsMethod;
23+
24+
public FilterExpressionCompiler(Type itemType, bool guardNulls, bool stringToLower = false, bool supportsEqualsMethod = true)
2325
: base(itemType, guardNulls) {
2426
_stringToLower = stringToLower;
27+
_supportsEqualsMethod = supportsEqualsMethod;
2528
}
2629

2730
public LambdaExpression Compile(IList criteriaJson) {
@@ -115,7 +118,7 @@ Expression CompileBinary(ParameterExpression dataItemExpr, IList criteriaJson) {
115118

116119
if(expressionType == ExpressionType.Equal || expressionType == ExpressionType.NotEqual) {
117120
var type = Utils.StripNullableType(accessorExpr.Type);
118-
if(!HasEqualityOperator(type)) {
121+
if(_supportsEqualsMethod && !HasEqualityOperator(type)) {
119122
if(type.IsValueType) {
120123
accessorExpr = Expression.Convert(accessorExpr, typeof(Object));
121124
valueExpr = Expression.Convert(valueExpr, typeof(Object));

0 commit comments

Comments
 (0)