-
Type:
Bug
-
Resolution: Works as Designed
-
Priority:
Unknown
-
None
-
Affects Version/s: 2.19.0
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Summary
When trying to insert a document into MongoDB using the C# Driver with a Typed document we receive a recursive error because it is not able to serialize (and deserialize) the data
Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).
Driver v2.19.0
Server Standalone. I don't know what is the version of the Mongo server, but I'm using the latest docker image to this day for development.
How to Reproduce
using System.Text.Json; using System.Text.Json.Nodes; using MongoDB.Driver; var database = new MongoClient().GetDatabase("testdatabase"); JsonObject json = JsonSerializer.Deserialize<JsonObject>("{\"name\":\"Jane Doe\",\"favorite-game\":\"Stardew Valley\",\"subscriber\":false}")!; var doc = new DocumentError() { CorrelationId = "testcor", Error = json }; database.GetCollection<DocumentError>("test").InsertOne(doc); public class DocumentError { public string CorrelationId { get; set; } public JsonObject Error { get; set; } }
Additional Background
.NET 7, using Microsoft System.Text.Json.Nodes
A work around who is really not performant and is not an option for what we are developing is converting "DocumentError" to a json string, to afterward convert it into a bsondocument.
While this work, for deserializing (getting the data) we need to get it in a BsonDocument and foreach create a new instance of "DocumentError" via a constructor to make it work.