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; }
- has to be done before
-
CSHARP-4632 The memberinfo argument must be for class {ChildTestDto}, but was for {ParentTestDto}}
- Closed
- related to
-
CSHARP-4524 Handle projections using constructors for classes that use public fields instead of public properties
- Closed