Skip to content
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

Wrong Type used for classes using Covariance #190

Open
nicoschwartz opened this issue Sep 26, 2024 · 0 comments
Open

Wrong Type used for classes using Covariance #190

nicoschwartz opened this issue Sep 26, 2024 · 0 comments

Comments

@nicoschwartz
Copy link

Hi there!

First thank you for maintaining this project 🙏

While upgrading from automapper+automapper-bundle (8.1.0+8.0.2) to automapper 9.1.2 I ran into an issue with wrong type being used for properties of a target.

From what I've debugged and tested I think the setter of a property is used to determine the type of the target but it doesn't make much sense in case of covariance - covariance on php.net - as the return type of the getter can be more specific while the type in the signature of the setter cannot.

Here's the solution that worked for covariance's case : changing the SourceTargetMappingExtractor from :
$targetTypes = $this->propertyInfoExtractor->getTypes($target, $targetProperty->property, [ReadWriteTypeExtractor::WRITE_MUTATOR => $targetProperty->writeMutator]) ?? [];
to
$targetTypes = $this->propertyInfoExtractor->getTypes($target, $targetProperty->property, [ReadWriteTypeExtractor::READ_ACCESSOR => $targetProperty->readAccessor]) ?? [];
That way it uses the getter to determine the type which makes more sense (at least in this case).

It solves my case ... but breaks some other tests I ran from automapper's repo.

I wrote some tests with basic classes to show the issue, it's available here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant