@@ -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