AutoMapper / AutoMapper.Extensions.Microsoft.DependencyInjection

MIT License
258 stars 79 forks source link

Set assembliesToScanArray to Distinct assemblies #166

Closed valverij closed 2 years ago

valverij commented 2 years ago

If multiple types with the same assembly are sent in via on of the AddAutoMapper calls that use profileAssemblyMarkerTypes (or if duplicate assemblies are just passed in), then any Profiles in those assemblies end up being duplicated in the MapperConfiguration options, which can cause a lot of inconsistent behavior.

Initially, this was discovered via the exception thrown in TypeMap.CheckDifferent(TypePair types) when using Profiles that contain maps with IncludeAllDerived() set. If the assembly contains more than one Profile class, then the maps with IncludeAllDerived set are duplicated, and that CheckDifferent validation throws at runtime.

This change accounts for this accidental duplication of assemblies during configuration by explicitly setting assembliesToScanArray to distinct assemblies.

lbargaoanu commented 2 years ago

https://github.com/AutoMapper/AutoMapper.Extensions.Microsoft.DependencyInjection/blob/986267d95da70fef4c4811b59065533137bdd304/src/AutoMapper.Extensions.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs#L78

jbogard commented 2 years ago

I'm confused why you would see this error as we have that Distinct check already, do you have a repro?

valverij commented 2 years ago

@jbogard @lbargaoanu Here is a gist showing the error in action: https://gist.github.com/valverij/b32df6dcd47cf4934f5c09d8a6707244

lbargaoanu commented 1 year ago

@valverij Ok, that test should be part of the PR. And we only need one Distinct, the one you added. You can reopen this PR, or simply don't pass duplicated assemblies anymore :)

github-actions[bot] commented 1 year ago

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.