Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-4573

Serializing a PROCO containing a JsonObject fails.

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.19.0
    • Component/s: None
    • None
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • 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.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            yovodo2669@huvacliq.com Sam Stonew
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: