[CSHARP-4412] BsonRepresentationAttribute not respected when string array assigned to IList property Created: 11/Nov/22  Updated: 28/Oct/23  Resolved: 18/Nov/22

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 2.19.0

Type: Bug Priority: Unknown
Reporter: Tyler Ohlsen Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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



 Comments   
Comment by Tyler Ohlsen [ 19/Jan/23 ]

Is there an ETA of when 2.19.0 will be released with this fix?

Comment by Githook User [ 18/Nov/22 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4412: Fix a few tests that were missed.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/e36e163bee0c8ce6294b60425c5e44464c67dc89

Comment by Githook User [ 18/Nov/22 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4412: BsonRepresentationAttribute not respected when string array assigned to IList property.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/014e45854cc7cdbba40618caedb4a3e340deec27

Comment by Robert Stam [ 13/Nov/22 ]

Thank you for reporting this.

I am looking into a fix.

Generated at Wed Feb 07 21:48:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.