Skip to content

Commit 9010d77

Browse files
committed
Merge branch 'master' of https://github.com/mbdavid/LiteDB
2 parents 03b2579 + c6b8167 commit 9010d77

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Xunit;
4+
using System.Linq;
5+
6+
namespace LiteDB.Tests.Issues
7+
{
8+
public class Issue2112_Tests
9+
{
10+
private BsonMapper _mapper = new BsonMapper();
11+
12+
[Fact]
13+
public void Serialize_covariant_collection_has_type()
14+
{
15+
IA a = new A { Bs = new List<B> { new B() } };
16+
17+
var docA = _mapper.Serialize<IA>(a).AsDocument;
18+
var docB = docA["Bs"].AsArray[0].AsDocument;
19+
20+
Assert.True(docA.ContainsKey("_type"));
21+
Assert.True(docB.ContainsKey("_type"));
22+
}
23+
24+
[Fact]
25+
public void Deserialize_covariant_collection_succeed()
26+
{
27+
IA a = new A { Bs = new List<B> { new B() } };
28+
var serialized = _mapper.Serialize<IA>(a);
29+
30+
var deserialized = _mapper.Deserialize<IA>(serialized);
31+
32+
Assert.Equal(1, deserialized.Bs.Count);
33+
}
34+
35+
interface IA
36+
{
37+
// at runtime this will be a List<B>
38+
IReadOnlyCollection<IB> Bs { get; set; }
39+
}
40+
41+
class A : IA
42+
{
43+
public IReadOnlyCollection<IB> Bs { get; set; }
44+
}
45+
46+
interface IB
47+
{
48+
49+
}
50+
51+
class B : IB
52+
{
53+
54+
}
55+
}
56+
}

LiteDB/Client/Mapper/BsonMapper.Serialize.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ internal BsonValue Serialize(Type type, object obj, int depth)
135135
// check if is a list or array
136136
else if (obj is IEnumerable)
137137
{
138-
return this.SerializeArray(Reflection.GetListItemType(obj.GetType()), obj as IEnumerable, depth);
138+
return this.SerializeArray(Reflection.GetListItemType(type), obj as IEnumerable, depth);
139139
}
140140
// otherwise serialize as a plain object
141141
else

0 commit comments

Comments
 (0)