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

The projection serializer does not respect Bson[Class]Map and custom serialization parameters

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.9.1
    • Component/s: Serialization
    • Labels:
      None

      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);
                  }
              }
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            onyxmaster Aristarkh Zagorodnikov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: