-
Notifications
You must be signed in to change notification settings - Fork 74
Description
ASIS
Currently, it is necessary to explicitely set the PreserveReferences for parent-children objects that have backreferences.
Example: https://github.com/AutoMapper/AutoMapper.Collection/blob/master/src/AutoMapper.Collection.Tests/MapCollectionWithEqualityTests.cs#L336
If user fails to set the flag, the result is a StackOverflowException. This is bad, because there is no stacktrace to determine where did the error happened in the logs.
TOBE
I would like to remove this need to manually set the PreserveReferences manually.
The AutoMapper has a detection system to set PreserverReferences automatically.
Starting from 6.1.0 PreserveReferences is set automatically at config time whenever the recursion can be detected statically. If that doesn’t happen in your case, open an issue with a full repro and we’ll look into it.
The reason why AutoMapper.Collection doesn't detect cycle is because CheckForCycles is that EquivalentExpressionAddRemoveCollectionMapper doesn't provide info about associated types (i.e. generic types of the collection) and since cycle deteciton doesn't even have info about typemap used to map collection items, it fails to find the cycle. The undetected cycle later causes endless recursion and StackOverflowException.
I am no AutoMapper guru or anything, but it seems that it would be sufficient for EquivalentExpressionAddRemoveCollectionMapper to implement IObjectMapperInfo (probably through EnumerableMapperBase).
That would cause AutoMapper cycle detection to detect the cycles (this little piece of code here), thus it will add PreserverReferences and no StackOverflowException and happier developes.