mark-gerarts / automapper-plus

An AutoMapper for PHP
MIT License
551 stars 30 forks source link

Missing skip null fields #84

Open camerpu opened 2 years ago

camerpu commented 2 years ago

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();
mark-gerarts commented 2 years ago

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.

camerpu commented 2 years ago

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