[CSHARP-2271] ObjectSerializer should support deserializing all BSON types Created: 15/May/18  Updated: 10/Nov/22  Resolved: 10/Nov/22

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.5, 2.5.1, 2.6.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Chad Kreimendahl Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-2668 Error "ObjectSerializer does not supp... Backlog
Related
is related to CSHARP-2668 Error "ObjectSerializer does not supp... Backlog

 Description   

We've brought up a purely fresh 3.6.3 (now 3.6.4) mongo database from scratch. Having tried with 2.5 through 2.6... When attempting to test our connection, in which we simply run a "ping" command when connecting, we're getting the following exception about Timestamp BSON type not being supported:

 

{{ObjectSerializer does not support BSON type 'Timestamp'.: at }}
{{ MongoDB.Bson.Serialization.Serializers.ObjectSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at }}
{{ MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context) at }}
{{ MongoDB.Bson.Serialization.Serializers.DynamicDocumentBaseSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at }}
{{ MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at }}
{{ MongoDB.Bson.Serialization.Serializers.ObjectSerializer.DeserializeDiscriminatedValue(BsonDeserializationContext context, BsonDeserializationArgs args) at }}
{{ MongoDB.Bson.Serialization.Serializers.ObjectSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at }}
{{ MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context) at }}
{{ MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply) at }}
{{ MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.<ExecuteAsync>d__15.MoveNext() — End of stack trace from previous location where exception was thrown — at }}
{{ System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at }}
{{ MongoDB.Driver.Core.Servers.Server.ServerChannel.<ExecuteProtocolAsync>d__27`1.MoveNext() — End of stack trace from previous location where exception was thrown — at }}
{{ System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at}}
{{ MongoDB.Driver.Core.Operations.CommandOperationBase`1.<ExecuteProtocolAsync>d__28.MoveNext() — End of stack trace from previous location where exception was thrown — at }}
{{ System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at }}
{{ MongoDB.Driver.Core.Operations.ReadCommandOperation`1.<ExecuteAsync>d__2.MoveNext() — End of stack trace from previous location where exception was thrown — at }}
{{ System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at }}
{{ MongoDB.Driver.OperationExecutor.<ExecuteReadOperationAsync>d__3`1.MoveNext() — End of stack trace from previous location where exception was thrown — at }}
{{ System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at }}
{{ MongoDB.Driver.MongoDatabaseImpl.<ExecuteReadOperationAsync>d__50`1.MoveNext() — End of stack trace from previous location where exception was thrown — at }}
{{ System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at }}
{{ System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at}}
{{ MongoDB.Driver.MongoDatabaseImpl.<UsingImplicitSessionAsync>d__57`1.MoveNext() — End of stack trace from previous location where exception was thrown — at }}
{{ System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at }}
{{ System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at }}
{{ System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()}}



 Comments   
Comment by Robert Stam [ 16/May/18 ]

A document that has been read from the database must be able to be saved back to the database.

Mapping a BSON timestamp to a .NET DateTime or an Int64 would result in the value changing when it was saved back to the database.

So BsonTimestamp is the appropriate type to use.

 

Comment by Chad Kreimendahl [ 16/May/18 ]

You might also consider allowing to it cast as a "long"

Comment by Chad Kreimendahl [ 16/May/18 ]

That should work.  Is there any value in converting it to a DateTime, or would that cause issues going back the other way (saving it once modified)?

Comment by Robert Stam [ 16/May/18 ]

I think what you are doing (using dynamic/object as the TResult for RunCommand) is perfectly reasonable.

It's just that the original vision for ObjectSerializer was that it would only convert BsonValues to standard .NET types.

Based on your use case I now believe ObjectSerializer should support all BsonValue types, even if it means using types that are in the driver instead of standard .NET types.

For example, when a BSON timestamp is encountered ObjectSerializer could return an instance of BsonTimestamp.

Comment by Chad Kreimendahl [ 16/May/18 ]

You've got it. That's nearly identical to our command, though the original was doing a "dynamic" vs an "object".  For some reason, it's not an issue against databases that were previously. It appears that there was a change in 3.6 that must require some compat update to trigger. 3.4 ping just returns ok. 3.6 ping returns some stats. 

We're not using, nor would we ever have a use for Timestamp (vs DateTime), so all of these issues would only occur for us with system calls... of which there are many. We had been just taking them as "dynamic" to make our lives easier. It seems as if this is now ill advised?

Comment by Robert Stam [ 15/May/18 ]

One way I can trigger this exception is:

var client = new MongoClient();
var admin = client.GetDatabase("admin");
var result = admin.RunCommand<object>(new BsonDocument("ping"1)); 

This can be worked around using RunCommand<BsonDocument> instead of RunCommand<object>.

Comment by Robert Stam [ 15/May/18 ]

Not sure what you mean by "a simple ping command".

Can you provide a code snippet that can be used to reproduce this?

Thanks!

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