Comments (6)
Two solutions I can see right now. The first is to set a property condition on your type map:
modelMapper.createTypeMap(Source.class, Dest.class)
.setPropertyCondition(Conditions.isNotNull());
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: modelMapper.getConfiguration().setProvider
.
I still intend to look into the problem of instantiating intermediate objects on the destination when mapping a source value that is null.
from modelmapper.
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?
from modelmapper.
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);
}
from modelmapper.
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.
from modelmapper.
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;
}
};
from modelmapper.
Fixed via 3107b8c
from modelmapper.
Related Issues (20)
- issue with record from java 14 HOT 1
- why modelMapper doesn't work to copy complex data from the same Entity? HOT 1
- ModelMapper cannot map all fields when mapping from proxy class over interface HOT 1
- ModelMapper mapping is updating table primary key instead of foreign key. HOT 1
- Error mapping, then reboot, works for a while
- How to skip not loaded Lazy fields?
- Test fails after upgrading from 3.1.0 to 3.1.1
- Java 21 support HOT 18
- Support of Java Module System
- Custom mapping in nested objects HOT 4
- Skip field of generic class HOT 1
- Mapping Source object to Destination object
- Mapping nested generic object with generic HOT 1
- Use specific converter for one field
- Model Mapper does not map values correctly (Intermittent - once in a few tries)
- Model Mapper Low performance
- Jackson Module: @JsonTypeInfo support for automatically handling inheritance HOT 1
- ModelMapper fails to map nested entity user field HOT 1
- When condition does not work for null-field
- How to ignore mapping errors HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from modelmapper.