[CSHARP-3653] Cannot deserialize a 'String' from BsonType 'ObjectId' Created: 06/May/21 Updated: 27/Oct/23 Resolved: 21/May/21 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Serialization |
| Affects Version/s: | 2.12.1, 2.12.2, 2.12.3 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Unknown |
| Reporter: | Роберт Лейбович | Assignee: | Dmitry Lukyanov (Inactive) |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Linux, .NET Core 3.1, .NET 5.0.5 in ASP.NET Web application |
||
| Description |
|
The first example from MongoDB documentation is not working. Here is the structure of my document: _id: ObjectId; is_approved: Boolean; text:String; The code of my entity is following: The last line gives an exception: ---> System.FormatException: Cannot deserialize a 'String' from BsonType 'ObjectId'. at MongoDB.Bson.Serialization.Serializers.StringSerializer.DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.Serializers.SealedClassSerializerBase`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.IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context) at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap) It is worth to mention that the bug can be reproduced in web application. In console app it works fine. |
| Comments |
| Comment by Роберт Лейбович [ 22/May/21 ] | |||||
|
Hi Dmitry, Thank you very much for pointing me the error. | |||||
| Comment by Dmitry Lukyanov (Inactive) [ 21/May/21 ] | |||||
|
Hello robert.lebovich@gmail.com,
pay attention that this class contains "_id" field as string, so this is exactly what the error "Cannot deserialize a 'String' from BsonType 'ObjectId'" says for you, you should clean up your code in particular you should use the Joke version where "Id" is defined as ObjectId. | |||||
| Comment by Роберт Лейбович [ 20/May/21 ] | |||||
|
Hi, I created a repo that is exact copy of my code but without keys. Here is the url: https://github.com/telebovich/weird-mongodb-behavior Please tell my if you need the keys for the bot and the database. How can I pass them in a secure way? Thank you, Tzvi (Robert) Leybovych. | |||||
| Comment by Dmitry Lukyanov (Inactive) [ 14/May/21 ] | |||||
|
Hey robert.lebovich@gmail.com, can you please provide a repro with a particular document that triggers this error? As it was mentioned before, it looks like the error is related to documents structure that you use in the your db, for example it can be done with one more step in your reproduction:
| |||||
| Comment by Роберт Лейбович [ 14/May/21 ] | |||||
|
Hi James,
I was pretty sure that the error is in web app. Now I see that it is not. My problem is that I created an empty application and added the code for telegram bot. It was working without serialization. But then I tried to serialize Bson to my own POCO object it stopped working.
Thank you, Tzvi (Robert) Leybovych. | |||||
| Comment by James Kovacs [ 07/May/21 ] | |||||
|
Hi, robert.lebovich@gmail.com, Thank you for reporting this issue to us. We have attempted to reproduce this issue with the MongoDB .NET/C# driver v2.12.3 and an ASP.NET Core Web App (dotnet new webapp) running on .NET 5.0. We were not able to reproduce the issue. Examining the error message:
This indicates that the driver is attempting to deserialize a string, but found an ObjectId in the database. Given that the only string in your model is the text field of the Joke class, it would suggest that there is a mismatch between your database schema and your C# class definition. Please review the documents in your database to see if one contains an ObjectId for the text field in your joker.jokes namespace. If not, please provide a complete self-contained repro of the issue including a mongodump of your joker.jokes namespace as a zip file attached to this case so that we can attempt to reproduce the issue. Sincerely, |