Skip to content

Commit ddb00b9

Browse files
CopilotStefH
andcommitted
Add three-level-deep nested new test to exercise recursive TryRebuildMemberInitExpression
Agent-Logs-Url: https://github.com/zzzprojects/System.Linq.Dynamic.Core/sessions/42b1ec4e-90f4-484b-ae3a-73f3bee9c50b Co-authored-by: StefH <249938+StefH@users.noreply.github.com>
1 parent 417258d commit ddb00b9

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,13 +2321,46 @@ public void DynamicExpressionParser_ParseLambda_NestedObjectInitialization()
23212321
result.CurrentDepartment!.Id.Should().Be(3);
23222322
}
23232323

2324+
// https://github.com/zzzprojects/System.Linq.Dynamic.Core/issues/701
2325+
[Fact]
2326+
public void DynamicExpressionParser_ParseLambda_NestedObjectInitialization_ThreeLevelsDeep()
2327+
{
2328+
// Arrange — exercises the recursive TryRebuildMemberInitExpression path.
2329+
// The parser propagates _resultType (CustomerForNestedNewTest) into all nested new
2330+
// expressions. The middle "new (new (3 as Id) as Sub)" therefore builds a
2331+
// MIE<Customer>{ Sub = MIE<Department>{Id=3} }. When the outer new binds that to its own
2332+
// "Sub" property (type DepartmentForNestedNewTest), TryRebuildMemberInitExpression is
2333+
// called and encounters the inner MIE<Department>{Id=3} binding — a MemberInitExpression
2334+
// itself — which triggers the recursive call to rebuild it for SubDepartmentForNestedNewTest.
2335+
var srcType = typeof(CustomerForNestedNewTest);
2336+
2337+
// Act
2338+
var lambda = DynamicExpressionParser.ParseLambda(ParsingConfig.DefaultEFCore21, srcType, srcType,
2339+
"new (new (new (3 as Id) as Sub) as Sub)");
2340+
var @delegate = lambda.Compile();
2341+
var result = (CustomerForNestedNewTest)@delegate.DynamicInvoke(new CustomerForNestedNewTest())!;
2342+
2343+
// Assert
2344+
result.Should().NotBeNull();
2345+
result.Sub.Should().NotBeNull();
2346+
result.Sub!.Sub.Should().NotBeNull();
2347+
result.Sub.Sub!.Id.Should().Be(3);
2348+
}
2349+
23242350
public class CustomerForNestedNewTest
23252351
{
23262352
public int Id { get; set; }
23272353
public DepartmentForNestedNewTest? CurrentDepartment { get; set; }
2354+
public DepartmentForNestedNewTest? Sub { get; set; }
23282355
}
23292356

23302357
public class DepartmentForNestedNewTest
2358+
{
2359+
public int Id { get; set; }
2360+
public SubDepartmentForNestedNewTest? Sub { get; set; }
2361+
}
2362+
2363+
public class SubDepartmentForNestedNewTest
23312364
{
23322365
public int Id { get; set; }
23332366
}

0 commit comments

Comments
 (0)