mongo-csharp-driver icon indicating copy to clipboard operation
mongo-csharp-driver copied to clipboard

BsonType.Timestamp support in ObjectSerializer

Open udda opened this issue 7 years ago • 7 comments

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.

udda avatar Jan 10 '18 17:01 udda

We are also experiencing the exact same bug - our clients cannot run our application against a replica set. Please merge this PR!

dnickless avatar Jul 18 '19 20:07 dnickless

I created a JIRA ticket: CSHARP-2668

dnickless avatar Jul 18 '19 20:07 dnickless

@udda

What command are you attempting to execute?

jyemin avatar Jul 18 '19 21:07 jyemin

@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 avatar Jul 18 '19 23:07 udda

@udda yes, you're right. The field is indeed operationTime (as well as $clusterTime.clusterTime)

jyemin avatar Jul 19 '19 00:07 jyemin

I'm experiencing the same issue, wondering what's stopping this from being merged?

idanbd avatar Aug 02 '21 13:08 idanbd

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.

BorisDog avatar Aug 16 '21 17:08 BorisDog