Skip to content

Commit fcffb69

Browse files
authored
Merge pull request #1868 from for7raid/issue1865
fix #1865 Wrong type after include directive
2 parents a1926f0 + 86bfc32 commit fcffb69

File tree

3 files changed

+112
-13
lines changed

3 files changed

+112
-13
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
{
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Xunit;
4+
using System.Linq;
5+
using System.Security.Cryptography;
6+
7+
namespace LiteDB.Tests.Issues
8+
{
9+
10+
public class Issue1865_Tests
11+
{
12+
public class Project : BaseEntity
13+
{
14+
public List<BaseEntity> Points { get; set; } = new List<BaseEntity>();
15+
}
16+
17+
public class Point : BaseEntity
18+
{
19+
public BaseEntity Project { get; set; }
20+
public BaseEntity Parent { get; set; }
21+
public DateTime Start { get; internal set; }
22+
public DateTime End { get; internal set; }
23+
}
24+
25+
public class BaseEntity
26+
{
27+
public ObjectId Id { get; set; } = ObjectId.NewObjectId();
28+
public string Name { get; set; }
29+
}
30+
31+
[Fact]
32+
public void Incluced_document_types_should_be_reald()
33+
{
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";
39+
40+
using var _database = new LiteDatabase(":memory:");
41+
var projectsCol = _database.GetCollection<Project>("activity");
42+
var pointsCol = _database.GetCollection<Point>("activity");
43+
44+
var project = new Project() { Name = "Project" };
45+
var point1 = new Point { Parent = project, Project = project, Name = "Point 1", Start = DateTime.Now, End = DateTime.Now.AddDays(2) };
46+
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);
50+
51+
pointsCol.Insert(point1);
52+
pointsCol.Insert(point2);
53+
projectsCol.Insert(project);
54+
55+
56+
var p1 = pointsCol
57+
.FindById(point1.Id);
58+
Assert.Equal(typeof(Project), p1.Parent.GetType());
59+
Assert.Equal(typeof(Project), p1.Project.GetType());
60+
61+
var p2 = pointsCol
62+
.FindById(point2.Id);
63+
Assert.Equal(typeof(Point), p2.Parent.GetType());
64+
Assert.Equal(typeof(Project), p2.Project.GetType());
65+
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)
72+
.FindById(point1.Id);
73+
Assert.Equal(typeof(Project), p1.Parent.GetType());
74+
Assert.Equal(typeof(Project), p1.Project.GetType());
75+
76+
p2 = pointsCol
77+
.Include(p => p.Parent).Include(p => p.Project)
78+
.FindById(point2.Id);
79+
Assert.Equal(typeof(Point), p2.Parent.GetType());
80+
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());
86+
}
87+
}
88+
}

LiteDB/Client/Mapper/BsonMapper.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,13 @@ private static void RegisterDbRefItem(BsonMapper mapper, MemberMapper member, IT
508508
if (included)
509509
{
510510
doc["_id"] = idRef;
511+
if (doc.ContainsKey("$type"))
512+
{
513+
doc["_type"] = bson["$type"];
514+
}
511515

512516
return m.Deserialize(entity.ForType, doc);
517+
513518
}
514519
else
515520
{
@@ -588,6 +593,10 @@ private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, IT
588593
if (included)
589594
{
590595
item["_id"] = idRef;
596+
if (item.AsDocument.ContainsKey("$type"))
597+
{
598+
item["_type"] = item["$type"];
599+
}
591600

592601
result.Add(item);
593602
}

0 commit comments

Comments
 (0)