Nullable compilation error #4384
Replies: 9 comments 12 replies
-
To demonstrate the issue described above, here's some rough (uncompiled, untested, just typed into this editor) sample code that should compile with v12 but will not compile if you upgrade to v13: #nullable enable
namespace BreakingChangeExample;
public class ThingConverter
{
private readonly IMapper _mapper;
public ThingConverter(IMapper mapper)
{
_mapper = mapper;
}
public ConvertedThing ConvertTheThing(UnconvertedThing unconvertedThing) =>
_mapper.Map<ConvertedThing>(unconvertedThing);
} You would also run into unexpected compilation issues on upgrade if you were using ConvertedThing convertedThing = _mapper.Map<ConvertedThing>(unconvertedThing); |
Beta Was this translation helpful? Give feedback.
-
This is by design and it was discussed before. |
Beta Was this translation helpful? Give feedback.
-
Ok, I found one of the discussions. Regarding this comment: #4297 (comment) Using the possibility of a custom mapper that returns null as justification to always have every |
Beta Was this translation helpful? Give feedback.
-
I'd rather not go down that rabbit hole. It seems to me that your code assumes things that are not true. |
Beta Was this translation helpful? Give feedback.
-
These overloads: TDestination? Map<TSource, TDestination>(TSource? source);
TDestination? Map<TSource, TDestination>(TSource? source, TDestination? destination);
TDestination? Map<TDestination>(object? source, Action<IMappingOperationOptions<object, TDestination>> opts);
TDestination? Map<TSource, TDestination>(TSource? source, Action<IMappingOperationOptions<TSource, TDestination>> opts);
TDestination? Map<TSource, TDestination>(TSource? source, TDestination? destination, Action<IMappingOperationOptions<TSource, TDestination>> opts); If I'm using a generic overload, where I specify the |
Beta Was this translation helpful? Give feedback.
-
The reality is that underneath the covers we use expression tree compilation, where none of these nullablility rules are enforced anyway. You're making assumptions that just aren't enforced in reality by AutoMapper. This isn't too far off other expression-tree-based tools, where we just can't make those guarantees. So we don't. We let you enforce that guarantee and assumption. |
Beta Was this translation helpful? Give feedback.
-
So is the issue I'm struggling to see here that If this really has to be this way, I'll adjust. It just seems odd to push a requirement for null checking and exception handling out this way though, when I imagine teams will end up creating wrappers that have better null-handling to absorb what seems absorbable within AutoMapper. |
Beta Was this translation helpful? Give feedback.
-
Also running into this, I suddenly need to fix a million nullable warnings, which in facts aren't warnings at all. I'm not upgrading right now, waiting for a fix. There was nothing wrong with the original code, and I think we can safely assume that when you pass in an object into the |
Beta Was this translation helpful? Give feedback.
-
I'm just going to revert nullables for now, clearly this was a bit too disruptive and will need a bit more careful thought to introduce. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In the upgrade docs, you state the following:
Yet in the Target .NET 6 PR, the
IMapperBase
andIMapper
definitions have no overloads (generic or non-generic) that accept a non-null destination object and return a non-null object (which is contrary to what you are stating in the documentation).i.e. A simple version 12 to version 13 upgrade results in a breaking change for all existing calls to generic
Map
overloads in projects that use#nullable enable
because the inputs and return type for those has changed to be nullable objects.We have code with calls to generic
Map
overloads where we're passing in non-null source object and receiving a non-null destination object, and those calls now result in compilation errors with the v13 upgrade.That's an upgrade blocker, and a poor implementation of
nullable enable
that should be fixed asap. You should also consider pulling the release/marking it as obsolete so that people don't upgrade until this is fixed.The change that needs to be revisited is in
src/AutoMapper/Mapper.cs
. It appears that the developer who implemented that change simply made everything nullable (or close to it), including the return types, on all generic and non-generic overloads, which is completely wrong.Beta Was this translation helpful? Give feedback.
All reactions