Skip to content

Commit bf223dd

Browse files
JaleChakiAlexander Syrtsev
andauthored
Prevent InvalidOperationException when custom equality operator return type is not Boolean (#489)
Co-authored-by: Alexander Syrtsev <syrtsev.alexander@devexpress.com>
1 parent 81a763d commit bf223dd

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

net/DevExtreme.AspNet.Data.Tests/CustomEqualTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using Xunit;
45

56
namespace DevExtreme.AspNet.Data.Tests {
@@ -30,6 +31,18 @@ public void WithOperator() {
3031
);
3132
}
3233

34+
[Fact]
35+
public void WrongOperator() {
36+
TestFilter(
37+
new[] {
38+
new DataItemWrongOperator { Value = 1 },
39+
new DataItemWrongOperator { Value = 2 }
40+
},
41+
"=", new DataItemWrongOperator { Value = 1 },
42+
".Where(obj => Equals(obj, {1}))"
43+
);
44+
}
45+
3346
[Fact]
3447
public void Struct() {
3548
TestFilter(
@@ -82,6 +95,22 @@ public override string ToString()
8295
=> "{" + Value + "}";
8396
}
8497

98+
class DataItemWrongOperator {
99+
public int Value;
100+
[SuppressMessage("Style", "IDE0060")]
101+
public static DataItemWrongOperator operator ==(DataItemWrongOperator l, DataItemWrongOperator r)
102+
=> null;
103+
[SuppressMessage("Style", "IDE0060")]
104+
public static DataItemWrongOperator operator !=(DataItemWrongOperator l, DataItemWrongOperator r)
105+
=> null;
106+
public override bool Equals(object obj)
107+
=> Value == (obj as DataItemWrongOperator).Value;
108+
public override int GetHashCode()
109+
=> Value.GetHashCode();
110+
public override string ToString()
111+
=> "{" + Value + "}";
112+
}
113+
85114
struct DataItemStruct {
86115
public int Value;
87116
public override bool Equals(object obj)

net/DevExtreme.AspNet.Data/FilterExpressionCompiler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ bool HasEqualityOperator(Type type) {
168168
if(type == typeof(Guid) || type == typeof(DateTimeOffset) || type == typeof(TimeSpan))
169169
return true;
170170

171-
return type.GetMethod("op_Equality", new[] { type, type }) != null;
171+
var operatorMethod = type.GetMethod("op_Equality", new[] { type, type });
172+
return operatorMethod != null && operatorMethod.ReturnType == typeof(bool);
172173
}
173174

174175
bool HasComparisonOperator(Type type) {

0 commit comments

Comments
 (0)