[CSHARP-1284] Projection to object with string Id member yields CreateCannotDeserializeFromBsonTypeException Created: 17/May/15  Updated: 05/Apr/16  Resolved: 02/Jun/15

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

Type: Bug Priority: Minor - P4
Reporter: Kieren Johnstone Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

This appears to occur with any projection built with Builders<T>.Projection.

The model class has an Id member which is a string, but no annotation attributes, for example:

public class SomeClass { public string Id

{get;set;} public string Name {get;set;}

}

var result = _collection.FindOneAndUpdateAsync(filter, updateDef, new FindOneAndUpdateOptions
{
IsUpsert = true,
Projection = Builders<T>.Projection.Expression(u => new

{ U.Id }

),
ReturnDocument = ReturnDocument.Before
}).Result;

The exception is thrown in StringSerializer.DeserializeValue. In this case, the local variable bsonType is ObjectId, and _representation is String.

Am I correct in thinking a check for this, followed by a call to bsonReader.ReadString(), would be the fix?

If so, I'd like to take the opportunity to build a Pull Request, but am unsure of the testing/submission guidelines, since the page linked to in the docs either no longer exists or has moved from where GitHub points to. Could you advise please?



 Comments   
Comment by Kieren Johnstone [ 01/Jun/15 ]

Sorry for the delay - it was my mistake. Effectively, I was doing an upsert with all properties in the $setOnInsert (by building the update document manually). But I excluded the id property at the time because I was using an old version of Mongo (which threw an error with the upsert+$setOnInsert+id combo) and weirdly decided to just not set it.

That meant an ObjectId was generated server-side, but then my Id field client-side was still just a 'string'.

I'm quite certain then that this was just my mistake - now fixed by using the upsert+$sOI+id combo and the latest version of Mongo

Comment by Craig Wilson [ 22/May/15 ]

Kieren,

Any update on this?

Craig

Comment by Craig Wilson [ 17/May/15 ]

If you were to decorate your string property with a [BsonRepresentation(BsonType.ObjectId)] attribute (or specified via code), this should work fine. But yes, you are right that the problem is we are expecting a string and receiving an ObjectId that was created by the server.

What do your Id properties look like? Are they ObjectId's or a custom application identifer.

Comment by Kieren Johnstone [ 17/May/15 ]

(I had removed _id from my upsert because I was seeing the error described in https://jira.mongodb.org/browse/SERVER-9958 )

Comment by Kieren Johnstone [ 17/May/15 ]

Ok, this occurs because I insert the object during an upsert which did not specify the _id field, leaving it to be automatically populated. Then the deserialisation fails because it can't convert ObjectId to string.

I think this is just my mistake, but before I close off, I'd like to confirm this is expected behaviour?

Comment by Kieren Johnstone [ 17/May/15 ]

In fact, oddly projection was just the first way I'd tried to get a string Id out. Even with no projection, the object fails to be deserialized. This could be a mistake on my part..?

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