IQueryable ProjectTo Recursion | Stackoverflow Exception after adding a recursive property
Please do the checklist before filing an issue:
- [x] I have read the documentation, including the FAQ
- [x ] I can reproduce the bug using the latest prerelease version
- [x ] I have searched existing discussion and issue to avoid duplicates
Describe the bug I read multiple threads and existing issues but I can't find any relevant answers.
The issues occured in a more specific use case with multiple nested properties and classes. However, the issues remains.
Given we have these entities (yes it doesn't make sense now but at the end it results to an issues of recursion)
- Case
- Request
- Case
- Request
Then you launch a build.
- It doesn't respond for several minutes.
- Then you get a stack overflow exception with several hundred of random errors.
- All the existing mappers are showing an error.
- All the source generated code are gone.
- You don't even know what happen.
At the end I figured out that we had a recursive properties on our projection DTO.
Some information that I would like to know?
- Is Mapperly supposed to support recursive configuration similar as what Automapper offer?
- Is there any possible solution to configure in order to support projection with recursive properties?
- Are we planning to get some runtime detection/error to point out the recursive properties causing the issues?
Declaration code
// Projection
await @case.ProjectToCaseDto()
.FirstOrDefaultAsync(cancellationToken);
// Mapper snapshot
[Mapper]
[UseStaticMapper(typeof(ReferenceTableMapper))]
public static partial class CaseMapper
{
[MapperRequiredMapping(RequiredMappingStrategy.Target)]
public static partial IQueryable<CaseDto> ProjectToCaseDto(this IQueryable<Case> cases);
Actual relevant generated code
// Actual relevant code generated by Mapperly
// see also https://mapperly.riok.app/docs/configuration/generated-source/
Expected relevant generated code N/A
Reported relevant diagnostics N/A
Environment (please complete the following information):
- Mapperly Version: 4.1.1
- Nullable reference types: [e.g. enabled]
- .NET Version: 9.0
- Target Framework: net 9.0
- Compiler Version: [e.g. 4.8.0-3.23524.11 (f43cd10b), obtain with
#error versionin any.csfile] - C# Language Version: 12.0
- IDE: JetBrains Rider 2024.3.6 Build #RD-243.25659.34, built on February 26, 2025
- OS: macOS 15.3.1 (24D70)
Additional context Add any other context about the problem here.
Thanks for reporting, this was discussed multiple times but actually never implemented. Until there is support for recursion depth you need to ignore the member or manually map it. See https://github.com/riok/mapperly/pull/878, https://github.com/riok/mapperly/issues/785.
Hi, bumping this issue as it hit me today as well. Slapping [MapperIgnoreSource] on a mapping method while pointing to the cycle property does solve the issue, but... I only figured that out after about 20 restarts of VS, as this bug (I believe it is a bug) absolutely murders any compilation process. CLI was flooded with 3K of errors as well, so I was reverting changes one by one until I struck the culprit. Some error message and StackOverflow prevention method would be highly recommended for this scenario. Thanks!