|
Not sure if this is intended, but the anonymous-type based full-document projection respects the Bson[Class]Map settings, while the partial projection does not. In the following example the "Value" field of the "Data" class default is set to -1. When projecting the full document, the full serializer is used and the projected value is -1. When really proecting fields, the default (0) is used. This can bring nasty surprises because full-document projection can be used when the projection expression references document object itself, so changing the projected fields could influence the value of the other, non-projected fields.
private class Data
|
{
|
[Bson.Serialization.Attributes.BsonDefaultValue(-1)]
|
public int A { get; set; }
|
}
|
|
[Fact]
|
public void Should_produce_serializer_that_respects_bson_map()
|
{
|
var data = ProjectData(x => x, "{}");
|
data.A.Should().Be(-1);
|
}
|
|
[Fact]
|
public void Should_produce_serializer_that_respects_bson_map_when_projecting()
|
{
|
var data = ProjectData(x => new { x.A }, "{}");
|
data.A.Should().Be(-1);
|
}
|
|
private T ProjectData<T>(Expression<Func<Data, T>> projector, string json)
|
{
|
var serializer = BsonSerializer.SerializerRegistry.GetSerializer<Data>();
|
var projectionInfo = FindProjectionTranslator.Translate<Data, T>(projector, serializer, BsonSerializer.SerializerRegistry);
|
|
using (var reader = new JsonReader(json))
|
{
|
var context = BsonDeserializationContext.CreateRoot(reader);
|
return projectionInfo.ProjectionSerializer.Deserialize(context);
|
}
|
}
|
|