-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Major - P3
-
Affects Version/s: 2.14.1
-
Component/s: LINQ3
-
None
-
Fully Compatible
I have a struct that has an "Id" field mapped using a custom serializer. Unfortunately LINQ queries, in my example Find, does not seem to use the serializer, causing the query to not match any documents.
here is a full test case that reproduces the issue:
class ObjectIdAsStringSerializer : SerializerBase<string> { public static readonly ObjectIdAsStringSerializer Instance = new ObjectIdAsStringSerializer(); public override string Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { return context.Reader.ReadObjectId().ToString(); } public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, string value) { context.Writer.WriteObjectId(ObjectId.Parse(value)); } } struct Thing { public string Id { get; set; } } [Test] public async Task test_linq3() { BsonClassMap.RegisterClassMap<Thing>(cm => { cm.MapIdProperty(thing => thing.Id) .SetSerializer(ObjectIdAsStringSerializer.Instance); }); MongoClientSettings settings = MongoClientSettings.FromConnectionString("mongodb://localhost:27017/?replicaSet=rs0"); settings.LinqProvider = LinqProvider.V2; // V2 = test passes, V3 = test fails var client = new MongoClient(settings); string dbName = "testdb-" + Guid.NewGuid(); IMongoDatabase database = client.GetDatabase(dbName); const string id = "590df61373b975210006fcdf"; IMongoCollection<BsonDocument> coll = database.GetCollection<BsonDocument>("testCollection"); await coll.InsertOneAsync(BsonDocument.Create(new Dictionary<string, object?> { ["_id"] = ObjectId.Parse(id) })); IMongoCollection<Thing> typedColl = database.GetCollection<Thing>("testCollection"); long count = await typedColl.Find(t => t.Id == id).CountDocumentsAsync(); await client.DropDatabaseAsync(dbName); Assert.That(count, Is.EqualTo(1)); }
- related to
-
CSHARP-3449 custom serializers sometimes not used in aggregation
-
- Closed
-