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.