Skip to content

Commit 3d3185b

Browse files
committed
fix bug with abstract type
1 parent 3e0ceed commit 3d3185b

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

src/EntityChange/EntityComparer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ private void CompareObject(Type type, object? original, object? current)
115115
var currentValue = accessor.GetValue(current);
116116

117117
var propertyName = accessor.Name;
118-
var propertyType = accessor.MemberType.GetUnderlyingType();
118+
119+
// using value type to support abstracts
120+
var propertyType = originalValue?.GetType()
121+
?? currentValue?.GetType()
122+
?? accessor.MemberType.GetUnderlyingType();
119123

120124
_memberStack.Push(memberMapping);
121125
_pathStack.PushProperty(propertyName);

test/EntityChange.Tests/EntityCompareTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.ObjectModel;
44
using System.Globalization;
55
using System.Linq;
6+
using System.Threading.Channels;
67
using System.Threading.Tasks;
78

89
using EntityChange.Tests.Models;
@@ -1094,6 +1095,34 @@ public void CompareArrayRootElementsTest()
10941095
changeRecord.Path.Should().Be("[0]");
10951096
}
10961097

1098+
[Fact]
1099+
public async Task CompareAbstract()
1100+
{
1101+
var obj1 = new Consumer()
1102+
{
1103+
SomeProperty = new ConcreteClass()
1104+
{
1105+
Id = 1,
1106+
SomeString = "Test1"
1107+
}
1108+
};
1109+
var obj2 = new Consumer()
1110+
{
1111+
SomeProperty = new ConcreteClass()
1112+
{
1113+
Id = 2,
1114+
SomeString = "Test2"
1115+
}
1116+
};
1117+
1118+
var comparer = new EntityComparer();
1119+
var changes = comparer.Compare(obj1, obj2);
1120+
1121+
changes.Should().NotBeEmpty();
1122+
1123+
await Verifier.Verify(changes).UseDirectory("Snapshots");
1124+
}
1125+
10971126
private void WriteMarkdown(IReadOnlyList<ChangeRecord> changes)
10981127
{
10991128
var formatter = new MarkdownFormatter();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace EntityChange.Tests.Models;
2+
3+
public abstract class AbstractBase
4+
{
5+
public int Id { get; set; }
6+
}
7+
8+
public class ConcreteClass : AbstractBase
9+
{
10+
public string SomeString { get; set; }
11+
}
12+
13+
public class Consumer
14+
{
15+
public AbstractBase SomeProperty { get; set; }
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
PropertyName: SomeString,
4+
DisplayName: Some String,
5+
Path: SomeProperty.SomeString,
6+
Operation: Replace,
7+
OriginalValue: Test1,
8+
CurrentValue: Test2,
9+
OriginalFormatted: Test1,
10+
CurrentFormatted: Test2
11+
},
12+
{
13+
PropertyName: Id,
14+
DisplayName: Id,
15+
Path: SomeProperty.Id,
16+
Operation: Replace,
17+
OriginalValue: 1,
18+
CurrentValue: 2,
19+
OriginalFormatted: 1,
20+
CurrentFormatted: 2
21+
}
22+
]

0 commit comments

Comments
 (0)