Git Product home page Git Product logo

Comments (6)

jhalterman avatar jhalterman commented on July 22, 2024 1

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.

jhalterman avatar jhalterman commented on July 22, 2024

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.

mlindenmuth avatar mlindenmuth commented on July 22, 2024

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.

jhalterman avatar jhalterman commented on July 22, 2024

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.

mlindenmuth avatar mlindenmuth commented on July 22, 2024

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.

jhalterman avatar jhalterman commented on July 22, 2024

Fixed via 3107b8c

from modelmapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.