-
Notifications
You must be signed in to change notification settings - Fork 347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mapping creates instances for null objects #32
Comments
Thanks for reporting. This is a known issue (issue #6). I'll try to address it in the next release. I'm curious, is this causing any difficulties for you? |
Oh, sorry, somehow missed that issue. Yes, it is a blocker for us since we expect null values in our object graph in some cases. Looping through the whole tree seems to defeat the point of being able to map objects automatically! I've already sub-classed ModelMapper to check for a null root value (which i think may be fixed if this issue is fixed) but I didn't see any easy way to solve the above. @Override
public <D> D map(Object source, Class<D> destinationType)
{
if (source == null)
return null;
return super.map(source, destinationType);
} |
No worries. I was waiting to see if this issue would be a priority for anyone. Since it is, I'll make it a priority for myself, hopefully within the next day or two. |
I was wondering if there was a way to also add a generic root level converter. This was my first stab at solving the problem without any code changes but I thought it might be a helpful feature if it could catch objects at all levels of mapping: Converter<Object, Object> nullCheck = new AbstractConverter<Object, Object>()
{
protected Object convert (Object source)
{
if (source != null)
{
// return something else
}
return null;
}
}; |
Two solutions I can see right now. The first is to set a property condition on your type map:
This condition will be applied to all of the properties in the TypeMap (for a source and destination type), essentially, ensuring that nothing will be mapped for any source values that are null. The second option is similar to what you were thinking with a global converter, which we don't support right now (since ModelMapper basically is the global converter). There is a notion of a global Provider though. A provider provides destination instances prior to their values being populated. You could try what you were thinking with a global provider set via: I still intend to look into the problem of instantiating intermediate objects on the destination when mapping a source value that is null. |
Fixed via 3107b8c |
If both source and destination have a matching property that is a complex object, when the property on source is null mapping causes the property on the destination to be created by the default constructor.
The text was updated successfully, but these errors were encountered: