riok / mapperly

A .NET source generator for generating object mappings. No runtime reflection.
https://mapperly.riok.app
Apache License 2.0
2.52k stars 138 forks source link

Computed read-only properties causing RMG066 warning #1402

Closed RichardD2 closed 4 weeks ago

RichardD2 commented 1 month ago

Describe the bug Creating a mapping between two entities, both of which contain the same read-only computed property of a nullable value type, produces an erroneous RMG066 warning.

Declaration code

namespace Db
{
    public class Foo
    {
        public int? CategoryId { get; set; }
        public bool? IsSpecialCategory => CategoryId == 42;
    }
}

namespace Dto
{
    public class Foo
    {
        public int? CategoryId { get; set; }
        public bool? IsSpecialCategory => CategoryId == 42;
    }
}

namespace Mapping
{
    [Mapper]
    public static partial class FooMapping
    {
        public static partial Dto.Foo MapToDto(this Db.Foo source);
    }
}

Actual relevant generated code

namespace Mapping
{
    public static partial class FooMapping
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.6.0.0")]
        public static partial global::Dto.Foo MapToDto(global::Db.Foo source)
        {
            var target = new global::Dto.Foo();
            target.CategoryId = source.CategoryId;
            return target;
        }
    }
}

Expected relevant generated code The actual generated code is correct. However, the mapping is flagged with:

RMG066 No members are mapped in the object mapping from bool? to bool?

Environment (please complete the following information):

Additional context Explicitly adding a MapperIgnoreSource for the computed property makes the warning go away:

[Mapper]
public static partial class FooMapping
{
    [MapperIgnoreSource(nameof(Db.Foo.IsSpecialCategory))]
    public static partial Dto.Foo MapToDto(this Db.Foo source);
}
latonz commented 1 month ago

Thanks for reporting. I cannot reproduce the RMG066, I always get RMG020 for Value and HasValue of the bool? (which isn't optimal either). Do you have any properties set on the MapperAttribute or do you have a MapperDefaultsAttribute for your assembly?

RichardD2 commented 1 month ago

No additional settings. I'm getting the two RMG020 hints and the RMG066 warning with just the code shown.

image

image

I've attached the sample project I'm using: Mapperly1402.zip