mongo-csharp-driver
mongo-csharp-driver copied to clipboard
BsonType.Timestamp support in ObjectSerializer
Scenario
Calling IMongoDatabase.RunCommandAsync<object>
fails when response contains values with BSON type Timestamp. This happens at every request when using replica sets.
A FormatException
is thrown saying: ObjectSerializer does not support BSON type Timestamp.
Specialized serializers (like BsonTimestampSerializer
) can't be used here, because the anonymous response type doesn't allow class mapping.
Solution
Add an entry to ObjectSerializer
type recognition code, in order to support Timestamp deserialization.
That's already done for other BSON types (binary, int, decimal, array, string, objectId, etc.)
Notes
CSHARP-1162 says that Timestamp class is for internal use only, this may be the reason why it's not supported. This PR does not aim to change that.
Nevertheless, RunCommandAsync
(and possibly other methods) just fail with collections that contain a Timestamp value. This is often out of programmers control, and the proposed change may be a painless solution.
We are also experiencing the exact same bug - our clients cannot run our application against a replica set. Please merge this PR!
I created a JIRA ticket: CSHARP-2668
@udda
What command are you attempting to execute?
@jyemin I don't really remember since it happened over a year ago, but I think that every command (not queries) caused the error. There was a field named like t
or _ts
, of type Timestamp
, containing the operation time. Not sure whether it's still there in MongoDB 4.
@udda yes, you're right. The field is indeed operationTime
(as well as $clusterTime.clusterTime
)
I'm experiencing the same issue, wondering what's stopping this from being merged?
Hello @idanbd,
Unfortunately this PR won't fix the failing IMongoDatabase.RunCommandAsync<object>
, as BsonType.Binary
is not supported by ObjectSerializer
as well. The reason for this is that ObjectSerializer
initially was not designed to support types which don't have direct mapping to .NET types.
Using IMongoDatabase.RunCommandAsync<BsonDodocument>
instead should resolve this issue.