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

Projection where one field name starts with another field name fails

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.9.0
    • Affects Version/s: 2.8.0
    • Component/s: Linq
    • Labels:
      None

      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:

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
          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.

            Assignee:
            dmitry.lukyanov@mongodb.com Dmitry Lukyanov (Inactive)
            Reporter:
            john.murphy@mongodb.com John Murphy
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: