automapper-plus icon indicating copy to clipboard operation
automapper-plus copied to clipboard

Missing skip null fields

Open camerpu opened this issue 1 year ago • 2 comments

Hi, I'd like to know why this overwriten function:

    protected function setDestinationValue(
        $destination,
        string $propertyName,
        $value
    ): void {
        $this->propertyWriter->setProperty(
            $destination,
            $propertyName,
            $value
        );
    }

In MapFrom.php doesn't have this check from his parent:

        if ($value === null && $this->options->shouldIgnoreNullProperties()) {
            return;
        }

In this case, when I use mapToObject, it won't skip null fields even I configured it in options by:

            ->getOptions()
            ->ignoreNullProperties();

camerpu avatar Aug 03 '22 11:08 camerpu

That's a very good question actually... It does seem I specifically added this behavior, as per this commit. The commit doesn't mention any reason, nor are there any related issues around that time. When I think about it, I have to agree with you that the expected behavior for mapFrom should be to respect the ignoreNullProperties option.

Since this is a BC breaking change, I'm a bit reluctant to add this to the 1.x release though. As a solution for you right this moment, I'd suggest creating your own MapFrom operation, extend it from the library's and overwrite the setDestinationValue method. I know this isn't ideal, but it should work for now.

mark-gerarts avatar Aug 04 '22 07:08 mark-gerarts

Ok, thank you for the fast response and help :)

camerpu avatar Aug 04 '22 07:08 camerpu