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

NewExpression and MemberInitExpression behaviour differs

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.19.2
    • Affects Version/s: None
    • Component/s: LINQ3
    • Labels:
      None
    • Fully Compatible
    • Not Needed
    • 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?

      Following on from CSHARP-4524, if you add a member initialization expression to the constructor, you switch from NewExpressionToAggregationExpressionTranslator to MemberInitExpressionToAggregationExpressionTranslator. The problem is that the latter requires exact matches of property names even for ctor parameters. Additionally we don't map readonly fields and properties causing the projection to fail as soon as a MemberInit expression is added to the constructor.

      The desired behaviour is that:

      new Data(x.Value) { Optional = 42 }

      (using MemberInitExpressionToAggregationExpressionTranslator) should work similarly to:

      new Data(x.Value)

      (using NewExpressionToAggregationExpressionTranslator). Adding an optional MemberInit expression shouldn't cause the projection to fail.

      [Fact]
      public void Projection_using_MemberInit_should_work()
      {
          RequireServer.Check().Supports(Feature.FindProjectionExpressions);
          var collection = CreateCollection(LinqProvider.V3);
          var find = collection.Find("{}").Project(x => new SpawnDataWithOptionalExtraInfo(x.StartDate, x.SpawnPeriod) { ExtraInfo = 42 });
                                                                                                                                                                              
          var results = find.ToList();
                                                                                                                                                                              
          var projection = find.Options.Projection;
          var serializerRegistry = BsonSerializer.SerializerRegistry;
          var documentSerializer = serializerRegistry.GetSerializer<MyData>();
          var renderedProjection = projection.Render(documentSerializer, serializerRegistry, LinqProvider.V3);
          renderedProjection.Document.Should().Be("{ Date : '$StartDate', Period : '$SpawnPeriod', ExtraInfo: 42, _id : 0 }");
                                                                                                                                                                              
          results.Should().HaveCount(1);
          results[0].Date.Should().Be(new DateTime(2023, 1, 2, 3, 4, 5, DateTimeKind.Utc));
          results[0].Period.Should().Be(SpawnPeriod.LIVE);
          results[0].ExtraInfo.Should().Be(42);
      }
                                                                                                                                                                              
      private struct SpawnDataWithOptionalExtraInfo
      {
          public readonly DateTime Date;
          public readonly SpawnPeriod Period;
          public int ExtraInfo;
                                                                                                                                                                              
          public SpawnDataWithOptionalExtraInfo(DateTime date, SpawnPeriod period)
          {
              // Normally there is more complex handling here, value-type semantics are important, there are custom comparison operators, etc. hence the point of this struct.
              Date = date;
              Period = period;
              ExtraInfo = 42;
          }
                                                                                                                                                                              
          public bool Equals(SpawnDataWithOptionalExtraInfo other) => Date == other.Date && Period == other.Period && ExtraInfo == other.ExtraInfo;
      }
      

            Assignee:
            oleksandr.poliakov@mongodb.com Oleksandr Poliakov
            Reporter:
            james.kovacs@mongodb.com James Kovacs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: