AutoMapper / AutoMapper.Extensions.ExpressionMapping

MIT License
143 stars 39 forks source link

Mapping Select New fails for properties declared in a base class #118

Closed ewancoder closed 3 years ago

ewancoder commented 3 years ago

Hi, I have an issue with inherited classes. Whenever I have a property on the root level of a class that is defined in a base class (not in the derived class), and I'm trying to make an ODATA SELECT for this property, it comes to this file:

https://github.com/AutoMapper/AutoMapper.Extensions.ExpressionMapping/blob/v4.1.1/src/AutoMapper.Extensions.ExpressionMapping/XpressionMapperVisitor.cs

To the line 300 to check whether it should Bind property, and as far as I understand it should bind it because otherwise DeclaringMemberKey is null here. But it goes to line 315 there and throws an ArgumentNullException because DeclaringMemberInfo is null and ShouldBindPropertyMap returned false at the line 300.

If we go into ShouldBindPropertyMap at the line 340, we see that on the line 341 we check whether sourceMember.ReflectedType is equal to memberBindingGroup.NewType, however for the property in base class sourceMember.ReflectedType would equal to derived type and memberBindingGroup.NewType would equal to base type (maybe wise versa, but they will be different). This leads to ShouldBindPropertyMap returning false. Maybe we need to consider doing something like sourceMember.ReflectedType IS memberBindingGroup.NewType?

If I'm incorrect please correct me, but this is the issue I'm facing after upgrading from 3.x to 4.x so I needed to downgrade to the version 4.0.2 (which doesn't have this issue).

Thanks, Ivan

BlaiseD commented 3 years ago

Sounds like a bug i.e. a case that isn't handled. You're welcome to create a PR - or otherwise update your description with code we can run and reproduce the failure.

BlaiseD commented 3 years ago

Should work in the MyGet Build.

ewancoder commented 3 years ago

Thanks! I'll test this with our code when I have the time.