Skip to content

Commit c80be72

Browse files
committed
#1865 [BUG] Wrong type after include directive
1 parent 6a767b0 commit c80be72

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

LiteDB.Tests/Issues/Issue1865.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
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);
35+
BsonMapper.Global.Entity<Point>().DbRef(p => p.Parent);
36+
BsonMapper.Global.ResolveCollectionName = (s) => "activity";
37+
38+
using var _database = new LiteDatabase(":memory:");
39+
40+
var project = new Project() { Name = "Project" };
41+
var point1 = new Point { Parent = project, Project = project, Name = "Point 1", Start = DateTime.Now, End = DateTime.Now.AddDays(2) };
42+
var point2 = new Point { Parent = point1, Project = project, Name = "Point 2", Start = DateTime.Now, End = DateTime.Now.AddDays(2) };
43+
44+
45+
_database.GetCollection<Point>("activity").Insert(point1);
46+
_database.GetCollection<Point>("activity").Insert(point2);
47+
_database.GetCollection<Project>("activity").Insert(project);
48+
49+
50+
var p1 = _database.GetCollection<Point>()
51+
.FindById(point1.Id);
52+
Assert.Equal(typeof(Project), p1.Parent.GetType());
53+
Assert.Equal(typeof(Project), p1.Project.GetType());
54+
55+
var p2 = _database.GetCollection<Point>()
56+
.FindById(point2.Id);
57+
Assert.Equal(typeof(Point), p2.Parent.GetType());
58+
Assert.Equal(typeof(Project), p2.Project.GetType());
59+
60+
p1 = _database.GetCollection<Point>()
61+
.Include(p=>p.Parent).Include(p=>p.Project)
62+
.FindById(point1.Id);
63+
Assert.Equal(typeof(Project), p1.Parent.GetType());
64+
Assert.Equal(typeof(Project), p1.Project.GetType());
65+
66+
p2 = _database.GetCollection<Point>()
67+
.Include(p => p.Parent).Include(p => p.Project)
68+
.FindById(point2.Id);
69+
Assert.Equal(typeof(Point), p2.Parent.GetType());
70+
Assert.Equal(typeof(Project), p2.Project.GetType());
71+
}
72+
}
73+
}

LiteDB/Client/Mapper/BsonMapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,13 @@ private static void RegisterDbRefItem(BsonMapper mapper, MemberMapper member, IT
492492
if (included)
493493
{
494494
doc["_id"] = idRef;
495+
if (doc.ContainsKey("$type"))
496+
{
497+
doc["_type"] = bson["$type"];
498+
}
495499

496500
return m.Deserialize(entity.ForType, doc);
501+
497502
}
498503
else
499504
{

0 commit comments

Comments
 (0)