Ignore properties with matching names but different types.

This commit is contained in:
Mohammadreza Taikandi 2020-12-24 20:05:52 +00:00
parent f1122af3d1
commit 7b03c7a55e
2 changed files with 37 additions and 2 deletions

View File

@ -101,11 +101,12 @@ namespace MapTo
{
return sourceTypeSymbol
.GetAllMembersOfType<IPropertySymbol>()
.Select(p => p.Name)
.Select(p => (p.Name, p.Type.ToString()))
.Intersect(classSymbol
.GetAllMembersOfType<IPropertySymbol>()
.Where(p => p.GetAttributes().All(a => a.AttributeClass?.Name != SourceBuilder.IgnorePropertyAttributeName))
.Select(p => p.Name))
.Select(p => (p.Name, p.Type.ToString())))
.Select(p => p.Name)
.ToImmutableArray();
}
}

View File

@ -413,5 +413,39 @@ namespace MapTo
diagnostics.ShouldBeSuccessful();
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult);
}
[Fact]
public void When_FoundMatchingPropertyNameWithDifferentType_Should_Ignore()
{
// Arrange
var source = GetSourceText(new SourceGeneratorOptions(
UseMapToNamespace: true,
PropertyBuilder: builder =>
{
builder
.PadLeft(Indent2).AppendLine("public string Prop4 { get; set; }");
},
SourcePropertyBuilder: builder => builder.PadLeft(Indent2).AppendLine("public int Prop4 { get; set; }")));
var expectedResult = @"
public partial class Foo
{
public Foo(Test.Models.Baz baz)
{
if (baz == null) throw new ArgumentNullException(nameof(baz));
Prop1 = baz.Prop1;
Prop2 = baz.Prop2;
Prop3 = baz.Prop3;
}
".Trim();
// Act
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
// Assert
diagnostics.ShouldBeSuccessful();
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult);
}
}
}