[CSHARP-2187] CombinedProjectionDefinition.Render() method throws StackOverflowException Created: 13/Feb/18  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Builders
Affects Version/s: 2.4.4, 2.5
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: David Stanescu Assignee: Unassigned
Resolution: Unresolved Votes: 1
Labels: triaged
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

CombinedProjectionDefinition.Render() method throws StackOverflowException when including a huge number of fields.

Here is a unit test reproducing the error:

[Fact]
public void Include_Many_Fields()
{
    var subject = CreateSubject<Person>();
 
    var projection = subject.Include(x => x.FirstName);
    var expectedProjection = new StringBuilder("{fn: 1");
    for (int i = 0; i < 10000; i++)
    {
        var field = $"Field{i}";
        projection = projection.Include(field);
        expectedProjection.Append($", {field}: 1");
    }
    expectedProjection.Append("}");
 
    Assert(projection, expectedProjection.ToString());
}

To fix the isuue I changed the constructor of the CombinedProjectionDefinition class to "unwind" the _projections collection to avoid recursively calling the Render() method:

public CombinedProjectionDefinition(IEnumerable<ProjectionDefinition<TSource>> projections)
{
    _projections = Ensure.IsNotNull(projections, nameof(projections))
        .SelectMany(projection => (projection as CombinedProjectionDefinition<TSource>)?._projections ?? new List<ProjectionDefinition<TSource>> { projection })
        .ToList();
}

Let me know if you need more information to fix the issue or if you want me to also create a pull request.



 Comments   
Comment by Ian Whalen (Inactive) [ 26/Nov/18 ]

Hey all, apologies for the delay in responding here. A pull request would be welcome - please be sure to include tests!

Comment by Ion Creanga [ 23/May/18 ]

Excelent catch, would also require for this fix to be made. Thank you MongoDB team .

Comment by Claudiu Constantin [ 22/May/18 ]

Good catch, we're also interested in this fix 

Comment by David Stanescu [ 22/May/18 ]

Any update on this?

Generated at Wed Feb 07 21:41:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.