Skip to content

Commit

Permalink
feat: fix incorrect private set when ~MemberVisibility.Accessible
Browse files Browse the repository at this point in the history
… is used
  • Loading branch information
TimothyMakkison committed Nov 21, 2023
1 parent 86408ce commit bae8838
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/Riok.Mapperly/Symbols/SetterMemberPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ public static SetterMemberPath Build(MappingBuilderContext ctx, MemberPath membe

private static (IMappableMember, bool) BuildMemberSetter(MappingBuilderContext ctx, IMappableMember member)
{
if (ctx.SymbolAccessor.IsDirectlyAccessible(member.MemberSymbol))
{
if (ctx.SymbolAccessor.IsDirectlyAccessible(member.MemberSymbol) && member.CanSetDirectly)
return (member, false);
}

if (member.MemberSymbol.Kind == SymbolKind.Field)
{
Expand Down
38 changes: 38 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/UnsafeAccessorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,44 @@ public void ManualUnflattenedPropertyNullablePath()
);
}

[Fact]
public void PropertyWithPrivateSetter()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"partial B Map(A source); ",
TestSourceBuilderOptions.WithMemberVisibility(MemberVisibility.All),
"class A { public int Value { get; set; } }",
"class B { public int Value { get; private set; } }"
);

TestHelper
.GenerateMapper(source)
.Should()
.HaveMapMethodBody(
"""
var target = new global::B();
target.SetValue(source.Value);
return target;
"""
);
}

[Fact]
public void PropertyWithProtectedSetterWhenDisabledShouldDiagnostic()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"partial B Map(A source);",
TestSourceBuilderOptions.WithMemberVisibility(MemberVisibility.All & ~MemberVisibility.Protected),
"class A { public int Value { get; set; } }",
"class B { public int Value { get; protected set; } }"
);

TestHelper
.GenerateMapper(source, TestHelperOptions.AllowDiagnostics)
.Should()
.HaveDiagnostic(DiagnosticDescriptors.CannotMapToReadOnlyMember);
}

[Fact]
public Task PrivateField()
{
Expand Down

0 comments on commit bae8838

Please sign in to comment.