Skip to content

Commit

Permalink
fix: prevent mapping backing field (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Aug 1, 2023
1 parent e8fc876 commit d768dbb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Riok.Mapperly/Descriptors/SymbolAccessor.cs
Expand Up @@ -235,8 +235,9 @@ private IEnumerable<IMappableMember> GetAllAccessibleMappableMembersCore(ITypeSy
return namedType.TupleElements.Select(x => MappableMember.Create(this, x)).WhereNotNull();
}

// member must be property or a none backing variable field
return GetAllMembers(symbol)
.Where(x => x is { IsStatic: false, Kind: SymbolKind.Property or SymbolKind.Field })
.Where(x => x is { IsStatic: false, Kind: SymbolKind.Property } or IFieldSymbol { IsStatic: false, AssociatedSymbol: null })
.DistinctBy(x => x.Name)
.Select(x => MappableMember.Create(this, x))
.WhereNotNull();
Expand Down
21 changes: 21 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectVisibilityTest.cs
Expand Up @@ -212,4 +212,25 @@ internal partial class Mapper

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task PropertyInTheSameClassShouldIgnoreBackingField()
{
var source = TestSourceBuilder.CSharp(
"""
public partial class A
{
public string Value { get; set; }

[Riok.Mapperly.Abstractions.Mapper(UseDeepCloning = true)]
internal partial class Mapper
{
public partial A Clone(A source);
}
}
"""
);

return TestHelper.VerifyGenerator(source);
}
}
@@ -0,0 +1,15 @@
//HintName: A.Mapper.g.cs
// <auto-generated />
#nullable enable
public partial class A
{
internal partial class Mapper
{
public partial global::A Clone(global::A source)
{
var target = new global::A();
target.Value = source.Value;
return target;
}
}
}

0 comments on commit d768dbb

Please sign in to comment.