Skip to content

Commit 86bfc32

Browse files
committed
#1865 fix incorrect type issue with include directive. tests fixes
1 parent c80be72 commit 86bfc32

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

LiteDB.Tests/Issues/Issue1651_Tests.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@
55

66
namespace LiteDB.Tests.Issues
77
{
8-
public class Order : BaseEntity
8+
9+
public class Issue1651_Tests
910
{
10-
public Customer Customer { get; set; }
11-
}
11+
public class Order : BaseEntity
12+
{
13+
public Customer Customer { get; set; }
14+
}
1215

13-
public class Customer : BaseEntity
14-
{
15-
public string Name { get; set; }
16-
}
16+
public class Customer : BaseEntity
17+
{
18+
public string Name { get; set; }
19+
}
20+
21+
public class BaseEntity
22+
{
23+
public Guid Id { get; set; }
24+
}
1725

18-
public class BaseEntity
19-
{
20-
public Guid Id { get; set; }
21-
}
22-
public class Issue1651_Tests
23-
{
2426
[Fact]
2527
public void Find_ByRelationId_Success()
2628
{

LiteDB.Tests/Issues/Issue1865.cs renamed to LiteDB.Tests/Issues/Issue1865_Tests.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
namespace LiteDB.Tests.Issues
88
{
9-
9+
1010
public class Issue1865_Tests
1111
{
1212
public class Project : BaseEntity
1313
{
14-
14+
public List<BaseEntity> Points { get; set; } = new List<BaseEntity>();
1515
}
1616

1717
public class Point : BaseEntity
@@ -31,43 +31,58 @@ public class BaseEntity
3131
[Fact]
3232
public void Incluced_document_types_should_be_reald()
3333
{
34-
BsonMapper.Global.Entity<Point>().DbRef(p => p.Project);
35-
BsonMapper.Global.Entity<Point>().DbRef(p => p.Parent);
36-
BsonMapper.Global.ResolveCollectionName = (s) => "activity";
34+
BsonMapper.Global.Entity<Point>().DbRef(p => p.Project, "activity");
35+
BsonMapper.Global.Entity<Point>().DbRef(p => p.Parent, "activity");
36+
BsonMapper.Global.Entity<Project>().DbRef(p => p.Points, "activity");
37+
38+
//BsonMapper.Global.ResolveCollectionName = (s) => "activity";
3739

3840
using var _database = new LiteDatabase(":memory:");
41+
var projectsCol = _database.GetCollection<Project>("activity");
42+
var pointsCol = _database.GetCollection<Point>("activity");
3943

4044
var project = new Project() { Name = "Project" };
4145
var point1 = new Point { Parent = project, Project = project, Name = "Point 1", Start = DateTime.Now, End = DateTime.Now.AddDays(2) };
4246
var point2 = new Point { Parent = point1, Project = project, Name = "Point 2", Start = DateTime.Now, End = DateTime.Now.AddDays(2) };
47+
48+
project.Points.Add(point1);
49+
project.Points.Add(point2);
4350

44-
45-
_database.GetCollection<Point>("activity").Insert(point1);
46-
_database.GetCollection<Point>("activity").Insert(point2);
47-
_database.GetCollection<Project>("activity").Insert(project);
51+
pointsCol.Insert(point1);
52+
pointsCol.Insert(point2);
53+
projectsCol.Insert(project);
4854

4955

50-
var p1 = _database.GetCollection<Point>()
56+
var p1 = pointsCol
5157
.FindById(point1.Id);
5258
Assert.Equal(typeof(Project), p1.Parent.GetType());
5359
Assert.Equal(typeof(Project), p1.Project.GetType());
5460

55-
var p2 = _database.GetCollection<Point>()
61+
var p2 = pointsCol
5662
.FindById(point2.Id);
5763
Assert.Equal(typeof(Point), p2.Parent.GetType());
5864
Assert.Equal(typeof(Project), p2.Project.GetType());
5965

60-
p1 = _database.GetCollection<Point>()
61-
.Include(p=>p.Parent).Include(p=>p.Project)
66+
var prj = projectsCol
67+
.FindById(project.Id);
68+
Assert.Equal(typeof(Point), prj.Points[0].GetType());
69+
70+
p1 = pointsCol
71+
.Include(p => p.Parent).Include(p => p.Project)
6272
.FindById(point1.Id);
6373
Assert.Equal(typeof(Project), p1.Parent.GetType());
6474
Assert.Equal(typeof(Project), p1.Project.GetType());
6575

66-
p2 = _database.GetCollection<Point>()
76+
p2 = pointsCol
6777
.Include(p => p.Parent).Include(p => p.Project)
6878
.FindById(point2.Id);
6979
Assert.Equal(typeof(Point), p2.Parent.GetType());
7080
Assert.Equal(typeof(Project), p2.Project.GetType());
81+
82+
prj = projectsCol
83+
.Include(p => p.Points)
84+
.FindById(project.Id);
85+
Assert.Equal(typeof(Point), prj.Points[0].GetType());
7186
}
7287
}
7388
}

LiteDB/Client/Mapper/BsonMapper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,10 @@ private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, IT
577577
if (included)
578578
{
579579
item["_id"] = idRef;
580+
if (item.AsDocument.ContainsKey("$type"))
581+
{
582+
item["_type"] = item["$type"];
583+
}
580584

581585
result.Add(item);
582586
}

0 commit comments

Comments
 (0)