[CSHARP-2614] Projection where one field name starts with another field name fails Created: 22/May/19  Updated: 28/Oct/23  Resolved: 28/Jun/19

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.8.0
Fix Version/s: 2.9.0

Type: Bug Priority: Major - P3
Reporter: John Murphy Assignee: Dmitry Lukyanov (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Case:

 Description   

If a POCO has two fields where one name starts with the others name, any projection that uses these names will fail to render.

Repro:

    public class Person
    {
        public string FirstName { get; set; }
        public string FirstNamePreferred { get; set; }
        public string LastName { get; set; }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            var m = new MongoClient("mongodb://noused");
            var coll = m.GetDatabase("test").GetCollection<Person>("foo");
 
            var queryDefn = Builders<Person>.Filter.Eq(x => x.FirstName, "John");
            var serializer = BsonSerializer.SerializerRegistry.GetSerializer<Person>();
            // WORKING
            var query = coll.Find(queryDefn).Project(x => new Person { FirstName = x.FirstName, LastName = x.LastName });
            var render = query.Options.Projection.Render(serializer, BsonSerializer.SerializerRegistry).Document;
            if (render == null || render.ToString() != "{ \"FirstName\" : 1, \"LastName\" : 1, \"_id\" : 0 }")
                throw new Exception("Projection does not match");
            // NOT WORKING
            query = coll.Find(queryDefn).Project(x => new Person { FirstName = x.FirstName, FirstNamePreferred = x.FirstNamePreferred });
            render = query.Options.Projection.Render(serializer, BsonSerializer.SerializerRegistry).Document;
            if (render == null || render.ToString() != "{ \"FirstName\" : 1, \"FirstNamePreferred\" : 1, \"_id\" : 0 }")
                throw new Exception("Projection does not match");
        }
    }

The issue appears to arise from this check in FindProjectionTranslator.



 Comments   
Comment by Githook User [ 28/Jun/19 ]

Author:

{'name': 'Dmitry Lukyanov', 'email': 'dmitry.lukyanov@mongodb.com', 'username': 'DmitryLukyanov'}

Message: CSHARP-2614: Fix logic for projection where one field name starts with another field name.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/7008c177f786a8b24dc30f2fcc41c4675cd0d8a0

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