diff --git a/src/Riok.Mapperly/Symbols/MemberPath.cs b/src/Riok.Mapperly/Symbols/MemberPath.cs index 2bf154a9a1..4ed923cdbf 100644 --- a/src/Riok.Mapperly/Symbols/MemberPath.cs +++ b/src/Riok.Mapperly/Symbols/MemberPath.cs @@ -65,7 +65,7 @@ public IEnumerable> ObjectPathNullableSubPaths() if (!pathPart.IsNullable) continue; - yield return pathParts; + yield return pathParts.ToArray(); } } diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyFlatteningTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyFlatteningTest.cs index 20ef901a93..361ee1267b 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyFlatteningTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyFlatteningTest.cs @@ -302,6 +302,32 @@ public void ManualUnflattenedPropertyNullablePath() ); } + [Fact] + public void ManualUnflattenedPropertyDeepNullablePath() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + "[MapProperty($\"MyValueId\", \"Value.Nested.Id\"), MapProperty($\"MyValueId2\", \"Value.Nested.Id2\")] partial B Map(A source);", + "class A { public string MyValueId { get; set; } public string MyValueId2 { get; set; } }", + "class B { public C? Value { get; set; } }", + "class C { public D? Nested { get; set; } }", + "class D { public string Id { get; set; } public string Id2 { get; set; } }" + ); + + TestHelper + .GenerateMapper(source) + .Should() + .HaveSingleMethodBody( + """ + var target = new global::B(); + target.Value ??= new(); + target.Value.Nested ??= new(); + target.Value.Nested.Id = source.MyValueId; + target.Value.Nested.Id2 = source.MyValueId2; + return target; + """ + ); + } + [Fact] public Task ManualUnflattenedPropertyNullablePathNoParameterlessCtorShouldDiagnostic() {