[CSHARP-98] Deserialization Fails on 2nd object read Created: 15/Nov/10  Updated: 02/Apr/15  Resolved: 15/Nov/10

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 0.9

Type: Bug Priority: Major - P3
Reporter: Ed Rooth Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Win XP. .NET 3.5


Backwards Compatibility: Major Change

 Description   

Sometime between 11/5 and now this broke. Did a pull and got the latest code now the deserialization is broken.

Steps to reproduce:

1. Create two classes, one that derives from another. (ie class B: A)
2. Set up mapping through attributes, or just use default.
3. Create two instances of B and save to a collection.
4. Query the collection specifying the custom type db.GetCollection<A>("myCollection").Find(...) (make sure this query returns at least 2 documents of type B).

This fails at the deserialization stage on the 2nd type B document.

Seems to be an issue with the bsonReader's buffer index not getting updated properly b/c it is set to EndOfDocument when it tries to deserialize the 2nd document causing a the actualType to default to the nominalType and therefore causes failure when deserializing. I stepped through the code a bit and this is the beginning of where everything goes bad:

// BsonClassMapSerializer.cs line 57
var actualType = discriminatorConvention.GetActualType(bsonReader, nominalType);

The actualType comes back the same as the nominalType (A) when it should come back as B. Since the data stored in the db is of type B, the properties cant get set and an exception gets thrown.

Stack Trace:

at MongoDB.Bson.DefaultSerializer.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType) in C:\DEVELOPMENT\trunk\Beast\mongo-csharp-driver\Bson\DefaultSerializer\BsonClassMapSerializer.cs:line 91
at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(BsonReader bsonReader, Type nominalType) in C:\DEVELOPMENT\trunk\Beast\mongo-csharp-driver\Bson\Serialization\BsonSerializer.cs:line 70
at MongoDB.Bson.Serialization.BsonSerializer.Deserialize[T](BsonReader bsonReader) in C:\DEVELOPMENT\trunk\Beast\mongo-csharp-driver\Bson\Serialization\BsonSerializer.cs:line 46
at MongoDB.Driver.Internal.MongoReplyMessage`1.ReadFrom(BsonBuffer buffer) in C:\DEVELOPMENT\trunk\Beast\mongo-csharp-driver\Driver\Internal\MongoReplyMessage.cs:line 88
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument]() in C:\DEVELOPMENT\trunk\Beast\mongo-csharp-driver\Driver\Internal\MongoConnection.cs:line 291
at MongoDB.Driver.MongoCursor`2.MongoCursorEnumerator.GetReply(MongoRequestMessage message) in C:\DEVELOPMENT\trunk\Beast\mongo-csharp-driver\Driver\Core\MongoCursor.cs:line 487
at MongoDB.Driver.MongoCursor`2.MongoCursorEnumerator.GetFirst() in C:\DEVELOPMENT\trunk\Beast\mongo-csharp-driver\Driver\Core\MongoCursor.cs:line 452
at MongoDB.Driver.MongoCursor`2.MongoCursorEnumerator.MoveNext() in C:\DEVELOPMENT\trunk\Beast\mongo-csharp-driver\Driver\Core\MongoCursor.cs:line 369
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)



 Comments   
Comment by Robert Stam [ 15/Nov/10 ]

Thanks for the detailed information in the bug report. Made it really easy to reproduce and fix.

Generated at Wed Feb 07 21:35:47 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.