[CSHARP-97] Deserializing a BsonDocument to .net object should not throw FormatException if the document has extra properties. Created: 14/Nov/10  Updated: 02/Apr/15  Resolved: 14/Nov/10

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

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

VS 2k8 Win7



 Description   

If I have a bson document with keys that do not map to properties in a .net object I am deserializing to, a FormatException is thrown. While I am returning more info than necessary over the wire (doing the mongo equivalent of SELECT * ) I think the code should still function.

at MongoDB.Bson.DefaultSerializer.BsonClassMapSerializer.DeserializeDocument(BsonReader bsonReader, Type nominalType) in D:\justin's projects\mongo-csharp\Bson\DefaultSerializer\BsonClassMapSerializer.cs:line 83
at MongoDB.Bson.Serialization.BsonSerializer.DeserializeDocument(BsonReader bsonReader, Type nominalType) in D:\justin's projects\mongo-csharp\Bson\Serialization\BsonSerializer.cs:line 69
at MongoDB.Bson.Serialization.BsonSerializer.DeserializeDocument[T](BsonReader bsonReader) in D:\justin's projects\mongo-csharp\Bson\Serialization\BsonSerializer.cs:line 45
at MongoDB.Driver.Internal.MongoReplyMessage`1.ReadFrom(BsonBuffer buffer) in D:\justin's projects\mongo-csharp\Driver\Internal\MongoReplyMessage.cs:line 88
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument]() in D:\justin's projects\mongo-csharp\Driver\Internal\MongoConnection.cs:line 291
at MongoDB.Driver.MongoCursor`2.MongoCursorEnumerator.GetReply(MongoRequestMessage message) in D:\justin's projects\mongo-csharp\Driver\Core\MongoCursor.cs:line 477
at MongoDB.Driver.MongoCursor`2.MongoCursorEnumerator.GetFirst() in D:\justin's projects\mongo-csharp\Driver\Core\MongoCursor.cs:line 442
at MongoDB.Driver.MongoCursor`2.MongoCursorEnumerator.MoveNext() in D:\justin's projects\mongo-csharp\Driver\Core\MongoCursor.cs:line 369
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at MongoDB.Driver.MongoCollection.FindOneAs[TQuery,TDocument](TQuery query) in D:\justin's projects\mongo-csharp\Driver\Core\MongoCollection.cs:line 292
at MongoDB.Driver.MongoCollection.FindOneAs[TDocument](IBsonSerializable query) in D:\justin's projects\mongo-csharp\Driver\Core\MongoCollection.cs:line 286
at BEGIN MY COMPANIES CODE



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

Works as designed.

Comment by Justin Dearing [ 14/Nov/10 ]

Ok that works for me. You can close this, and thanks for pointing out the risks,

Comment by Robert Stam [ 14/Nov/10 ]

You can always use [BsonIgnoreExtraElements] to deserialize documents with extra elements.

This question is whether it is safe to ignore extra elements without your permission. I think it is not, as it can easily lead to data loss (for example, if you save the document back to the database).

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