Skip to content

Commit a587d6d

Browse files
committed
Improved tests
1 parent d3973e1 commit a587d6d

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ private object DeserializeMemberValue(BsonDeserializationContext context, BsonMe
571571
{
572572
try
573573
{
574-
return memberMap.GetSerializer().Deserialize(context);
574+
return memberMap.GetSerializer(context.Domain).Deserialize(context);
575575
}
576576
catch (Exception ex)
577577
{

tests/MongoDB.Driver.Tests/MultipleRegistriesTests.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace MongoDB.Driver.Tests
2525
public class MultipleRegistriesTests
2626
{
2727
[Fact]
28-
public void GeneralTest()
28+
public void TestSerialization()
2929
{
3030
// At the moment having this uncommented would make the test fail due to the static caching of class maps
3131
// {
@@ -73,13 +73,46 @@ public void GeneralTest()
7373
}
7474
}
7575

76+
[Fact]
77+
public void TestDeserialization()
78+
{
79+
{
80+
var client = DriverTestConfiguration.CreateMongoClient();
81+
var db = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
82+
db.DropCollection(DriverTestConfiguration.CollectionNamespace.CollectionName);
83+
var collection = db.GetCollection<Person1>(DriverTestConfiguration.CollectionNamespace.CollectionName);
84+
85+
var person = new Person1 { Id = ObjectId.Parse("6797b56bf5495bf53aa3078f"), Name = "Mariotest", Age = 24 };
86+
collection.InsertOne(person);
87+
}
88+
89+
{
90+
var customDomain = BsonSerializer.CreateDomain();
91+
customDomain.RegisterSerializer(new CustomStringSerializer());
92+
93+
var client = DriverTestConfiguration.CreateMongoClient(c => c.SerializationDomain = customDomain);
94+
var db = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
95+
var collection = db.GetCollection<Person>(DriverTestConfiguration.CollectionNamespace.CollectionName);
96+
97+
var retrievedTyped = collection.FindSync("{}").ToList().Single();
98+
Assert.Equal("Mario", retrievedTyped.Name);
99+
}
100+
}
101+
76102
public class Person
77103
{
78104
[BsonId] public ObjectId Id { get; set; }
79105
public string Name { get; set; }
80106
public int Age { get; set; }
81107
}
82108

109+
public class Person1
110+
{
111+
[BsonId] public ObjectId Id { get; set; }
112+
public string Name { get; set; }
113+
public int Age { get; set; }
114+
}
115+
83116
public class CustomStringSerializer : SealedClassSerializerBase<string> //This serializer just adds "test" to any serialised string
84117
{
85118
/// <inheritdoc/>

0 commit comments

Comments
 (0)