Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kcsombrio committed Jan 5, 2022
2 parents 03b2579 + c6b8167 commit 9010d77
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
56 changes: 56 additions & 0 deletions LiteDB.Tests/Issues/Issue2112_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using Xunit;
using System.Linq;

namespace LiteDB.Tests.Issues
{
public class Issue2112_Tests
{
private BsonMapper _mapper = new BsonMapper();

[Fact]
public void Serialize_covariant_collection_has_type()
{
IA a = new A { Bs = new List<B> { new B() } };

var docA = _mapper.Serialize<IA>(a).AsDocument;
var docB = docA["Bs"].AsArray[0].AsDocument;

Assert.True(docA.ContainsKey("_type"));
Assert.True(docB.ContainsKey("_type"));
}

[Fact]
public void Deserialize_covariant_collection_succeed()
{
IA a = new A { Bs = new List<B> { new B() } };
var serialized = _mapper.Serialize<IA>(a);

var deserialized = _mapper.Deserialize<IA>(serialized);

Assert.Equal(1, deserialized.Bs.Count);
}

interface IA
{
// at runtime this will be a List<B>
IReadOnlyCollection<IB> Bs { get; set; }
}

class A : IA
{
public IReadOnlyCollection<IB> Bs { get; set; }
}

interface IB
{

}

class B : IB
{

}
}
}
2 changes: 1 addition & 1 deletion LiteDB/Client/Mapper/BsonMapper.Serialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ internal BsonValue Serialize(Type type, object obj, int depth)
// check if is a list or array
else if (obj is IEnumerable)
{
return this.SerializeArray(Reflection.GetListItemType(obj.GetType()), obj as IEnumerable, depth);
return this.SerializeArray(Reflection.GetListItemType(type), obj as IEnumerable, depth);
}
// otherwise serialize as a plain object
else
Expand Down

0 comments on commit 9010d77

Please sign in to comment.