[CSHARP-4289] LinqV3 Projection ObjectId deserialization error Created: 03/Aug/22  Updated: 28/Oct/23  Resolved: 29/Oct/22

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: 2.15.1, 2.17.1
Fix Version/s: 2.19.0

Type: Bug Priority: Major - P3
Reporter: Laurent Jeancler Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Zip Archive MongoDb.CSharp.Driver.ConsoleApp.zip    
Backwards Compatibility: Fully Compatible

 Description   

Summary

LinqProvider V3 failed to materialize a C# projection object (defined class, not an anomymous class) from a C# source object with an ObjectId string serializer : An error occurred while deserializing the Id property of class ConsoleApp.Projections.IdObjectBase: Cannot deserialize a 'String' from BsonType 'ObjectId'.

Tested with MongoDb driver 2.17.1 and 2.15.1 over local MongoDb instance.

How to Reproduce

See C# .Net6 attached project that reproduces the error.

Additional Background

ObjectId deserialization is working as expected when using LinqProvider.V2.

I really think that deserialization MUST use the C# source object serialization configuration over the projection.

Please note that it is working fine (and as expected) when using an anomyous object for projection with LinqProvider.V3.



 Comments   
Comment by Githook User [ 29/Oct/22 ]

Author:

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

Message: CSHARP-4289: Projected class should use correct serializers.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/2166608ca848d64ad1da9cc33a7eca2b7d891834

Comment by Robert Stam [ 18/Oct/22 ]

A minimal repro is:

public class C
{
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
}
 
public class R
{
    public string V { get; set; }
}
 
[Fact]
public void Select_should_work()
{
    var collection = CreateCollection();
 
    var queryable = collection.AsQueryable()
        .Select(x => new R { V = x.Id });
 
    var stages = Translate(collection, queryable);
    AssertStages(stages, "{ $project : { V : '$_id', _id : 0 } }");
 
    var results = queryable.ToList();
    results.Select(r => r.V).Should().Equal("111111111111111111111111");
}
 
private IMongoCollection<C> CreateCollection()
{
    var collection = GetCollection<C>();
 
    CreateCollection(
        collection,
        new C { Id = "111111111111111111111111" });
 
    return collection;
}

Comment by Esha Bhargava [ 05/Aug/22 ]

laurent.jeancler@assurware.com Thank you for reporting this issue. We'll look into it and get back to you soon.

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