mapperly icon indicating copy to clipboard operation
mapperly copied to clipboard

Diagnostic for mapping null mismatch

Open latonz opened this issue 1 year ago • 9 comments

Emit a diagnostic if the source is nullable but the target isn't. Default severity should be info. Proposed message: Mapping the nullable source property {0} of {1} to the target property {2} of {3} which is not nullable

Relates https://github.com/riok/mapperly/discussions/375

latonz avatar Jul 28 '23 06:07 latonz

If you add this, please make it possible to suppress this diagnostic for a single member. So that I can say "I know this, it's fine".

cremor avatar Aug 02 '23 13:08 cremor

Please, prioritize this issue. I would find it very useful and development without it is not compile time safe.

marin-8 avatar Jul 25 '24 21:07 marin-8

In my opinion, Mapperly is compile-safe without this feature, as all generated code is type-safe and provides diagnostics for invalid mappings. I can still understand your need for this feature, as it would definitely be a nice addition. However, this is not currently high on our priority list. With only one :+1: reaction, it also doesn't seem to be one of the most important features missing by the community... You are always welcome to contribute, we will support you with tips and reviews. If you cannot wait for this feature and do not have the time to implement it yourself, we are open to discussing sponsorship for specific features.

latonz avatar Jul 26 '24 00:07 latonz

Thanks for the quick reply!

I'm sorry, I didn't mean to say that "the code that Mapperly produces is not compile-time safe" in any way. What I meant is that:

  • When a property in the source class is nullable but not in the target class, and the actual value is null, the resulting value assigned to the target property will be the default for it's type. This can produce unexpected behaviour (in the case of value types) or errors (in the case of reference types in a [nullable enable] context). That is why I think a diagnostic that warns you when this is happening is very important.

After writing my previous comment I discovered that in the 4.0.0-next.1 preview documentation there is a new diagnostic, RMG076, that seems to address this (?). It's description is "Cannot assign null to non-nullable member.". I will try it whenever I can, but what's your comment on that?

Thanks again!

marin-8 avatar Jul 26 '24 07:07 marin-8

We definitely want to implement this diagnostic, it's just not on top of our priority list 😊 RMG076 is only for MapValue...

latonz avatar Jul 30 '24 23:07 latonz

Okay, I understand. Thanks anyway! ❤️

marin-8 avatar Jul 31 '24 00:07 marin-8

Hey @latonz, I would like to work on this, could you assign me? 😄

F1nn-T avatar Aug 22 '24 14:08 F1nn-T