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

XMLWordPrintableJSON

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

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • 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
            Reporter:
            Aristarkh Zagorodnikov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: