[CSHARP-4550] NewExpression and MemberInitExpression behaviour differs Created: 28/Feb/23  Updated: 28/Oct/23  Resolved: 23/May/23

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: None
Fix Version/s: 2.19.2

Type: Improvement Priority: Unknown
Reporter: James Kovacs Assignee: Oleksandr Poliakov
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Gantt Dependency
has to be done before CSHARP-4632 The memberinfo argument must be for c... Closed
Related
related to CSHARP-4524 Handle projections using constructors... Closed
Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed
Documentation Changes Summary:

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?


 Description   

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



 Comments   
Comment by Githook User [ 24/May/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4550: Make MemberInitExpression work with struct also as long as suitable constructor exists.
Branch: v2.19.x
https://github.com/mongodb/mongo-csharp-driver/commit/372021775bc555116e16f6939609c973c3e8d048

Comment by Githook User [ 24/May/23 ]

Author:

{'name': 'Oleksandr Poliakov', 'email': '31327136+sanych-sun@users.noreply.github.com', 'username': 'sanych-sun'}

Message: CSHARP-4550: NewExpression and MemberInitExpression behaviour differs (#1089)
Branch: v2.19.x
https://github.com/mongodb/mongo-csharp-driver/commit/ad5700b35b9ac03a19144a06b68cba012c74a9f4

Comment by Githook User [ 24/May/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4550: Make MemberInitExpression work with struct also as long as suitable constructor exists.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/a6162af4617f6f8bebe98b8684ec19e3536bf117

Comment by Githook User [ 23/May/23 ]

Author:

{'name': 'Oleksandr Poliakov', 'email': '31327136+sanych-sun@users.noreply.github.com', 'username': 'sanych-sun'}

Message: CSHARP-4550: NewExpression and MemberInitExpression behaviour differs (#1089)
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/7a99e9ceb0c2286966893dc15e687790524dd773

Generated at Wed Feb 07 21:48:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.