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

feat: fix incorrect private set when ~MemberVisibility.Accessible is used #933

Merged
merged 1 commit into from
Nov 21, 2023
Merged
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
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
Loading