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

BsonRepresentationAttribute not respected when string array assigned to IList property

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.19.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Summary

      When a document class has a property of type IList<string> and is decorated with the attribute to set the type as ObjectId, then assigning that property a value of List<string> properly respects the ObjectId type, but a string[] does not and stores the data as a string array in the database.

       

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      2.18.0

      How to Reproduce

      class TestDocument
      {
          [BsonRepresentation(BsonType.ObjectId)]
          public IList<string> MyObjectIdList { get; set; }
      }
      
      var objId = ObjectId.GenerateNewId().ToString();
      
      // This is incorrectly encoded to the database as an array of strings (not respecting the ObjectId representation)
      var doc = new TestDocument { MyObjectIdList = new[] { objId } };
      await collection.InsertOneAsync(document);
      // This does not find the inserted document (the search respects the ObjectId representation and attempts to find with an ObjectId value)
      await collection.Find(Builders<TestDocument>.Filter.AnyEq(doc => doc.MyObjectIdList, objId)).ToListAsync();
      
      // This is correctly encoded to the database as an array of ObjectIds
      var doc = new TestDocument { MyObjectIdList = new List<string> { objId } }; await collection.InsertOneAsync(document); // This finds the inserted document await collection.Find(Builders<TestDocument>.Filter.AnyEq(doc => doc.MyObjectIdList, objId)).ToListAsync();

      Additional Background

      None

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            tyler.ohlsen@magnopus.com Tyler Ohlsen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: