Skip to content

Commit

Permalink
fix: incorrect initialization for nullable nested memberpath unflatte…
Browse files Browse the repository at this point in the history
…ning (#591)
  • Loading branch information
TimothyMakkison committed Jul 25, 2023
1 parent 208e8b4 commit 9b74e8f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Riok.Mapperly/Symbols/MemberPath.cs
Expand Up @@ -65,7 +65,7 @@ public IEnumerable<IReadOnlyList<IMappableMember>> ObjectPathNullableSubPaths()
if (!pathPart.IsNullable)
continue;

yield return pathParts;
yield return pathParts.ToArray();
}
}

Expand Down
26 changes: 26 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectPropertyFlatteningTest.cs
Expand Up @@ -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()
{
Expand Down

0 comments on commit 9b74e8f

Please sign in to comment.