Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: initialize nullable members for nested containers #1660

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MembersContainerBuilderContext<T>(MappingBuilderContext builderCont
where T : IMemberAssignmentTypeMapping
{
private readonly Dictionary<MemberPath, MemberNullDelegateAssignmentMapping> _nullDelegateMappings = new();
private readonly HashSet<MemberPath> _initializedNullableTargetPaths = [];
private readonly HashSet<(IMemberAssignmentMappingContainer, MemberPath)> _initializedNullableTargetPaths = [];

public void AddMemberAssignmentMapping(IMemberAssignmentMapping memberMapping) => AddMemberAssignmentMapping(Mapping, memberMapping);

Expand Down Expand Up @@ -79,7 +79,7 @@ private void AddMemberAssignmentMapping(IMemberAssignmentMappingContainer contai
// the target should be non-null after this assignment and can be set as initialized.
if (!mapping.MemberInfo.IsSourceNullable && mapping.MemberInfo.TargetMember.MemberType.IsNullable())
{
_initializedNullableTargetPaths.Add(mapping.MemberInfo.TargetMember);
_initializedNullableTargetPaths.Add((container, mapping.MemberInfo.TargetMember));
}
}

Expand All @@ -93,7 +93,10 @@ private void AddNullMemberInitializers(IMemberAssignmentMappingContainer contain
if (!nullablePath.Member.CanSet)
continue;

if (!_initializedNullableTargetPaths.Add(nullablePath))
if (_initializedNullableTargetPaths.Contains((Mapping, nullablePath)))
continue;
Comment on lines +96 to +97
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This additional contains call is not needed, is it?


if (!_initializedNullableTargetPaths.Add((container, nullablePath)))
continue;

if (!BuilderContext.InstanceConstructors.TryBuild(BuilderContext.Source, type, out var ctor))
Expand Down
39 changes: 39 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectPropertyNullableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,45 @@ public partial B Map(A a)
);
}

[Fact]
public void NullableNestedMembersShouldInitializeWithNoNullAssignment()
Copy link
Contributor

@latonz latonz Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also create a test case for this slightly different one: #1661 (initialization after the if, but outside of any if)

{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
[MapProperty("NullableValue1", "V.NullableValue1")]
[MapProperty("NullableValue2", "V.NullableValue2")]
public partial B Map(A a)
""",
TestSourceBuilderOptions.Default with
{
AllowNullPropertyAssignment = false,
},
"class A { public int? NullableValue1 { get; set; } public int? NullableValue2 { get; set; } }",
"class B { public C? V { get; set; } }",
"class C { public int? NullableValue1 { get; set; } public int? NullableValue2 { get; set; } }"
);

TestHelper
.GenerateMapper(source)
.Should()
.HaveMapMethodBody(
"""
var target = new global::B();
if (a.NullableValue1 != null)
{
target.V ??= new global::C();
target.V.NullableValue1 = a.NullableValue1.Value;
}
if (a.NullableValue2 != null)
{
target.V ??= new global::C();
target.V.NullableValue2 = a.NullableValue2.Value;
}
return target;
"""
);
}

[Fact]
public void NullableClassToNullableClassPropertyThrowShouldSetNull()
{
Expand Down
Loading