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

Mapping a sub-document throws System.FormatException with a duplicate element name

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Works as Designed
    • Icon: Minor - P4 Minor - P4
    • None
    • 2.4.2
    • Serialization
    • None
    • VS2015

    Description

      An exception of type System.FormatException is thrown, with the following description:

      Additional information: An error occurred while deserializing the Settings property of class Application.Models.User: Duplicate element name 'admin'.

      The offending code (a test case created to trigger the bug in isolation):

              public void TriggerUserBug()
              {
                  var connection = ConfigurationManager.AppSettings["MongoDBConnection"];
                  var database = ConfigurationManager.AppSettings["DatabaseName"];
       
                  var client = new MongoClient(connection);
                  IMongoDatabase db = client.GetDatabase(database);
                  IMongoCollection<User> collection = db.GetCollection<User>("users");
       
                  var filter = Builders<User>.Filter.Eq("_id", ObjectId.Parse("533ab4de616c6565c9bc0000"));
                  var result = collection.Find(filter).FirstOrDefault(); // generates the exception
       
                  Assert.AreEqual("533ab4de616c6565c9bc0000", result.Id.ToString());
              }
      

      The User object declares the sub-document like this:

              [BsonElement("settings")]
              public BsonDocument Settings { get; set; }
      

      The string "admin" does not appear in the code at all.

      The data in the MongoDB sub-document looks like:

          "settings" : {
              "admin" : {
                  "ext_number" : "999"
              }
          },
      

      The string "admin" does not appear anywhere else in the user document (which is very large, which is why I haven't included it here).

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            Huw Huw Leonard [X]
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: