Azure / Azure/azure-functions-host
[.NET] Custom collections result in stack overflow at output in ObjectResult in v3
- Dominant language
- C#
- Stars
- 2k
- Forks
- 482
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 36
Description
When trying to output a custom collection via any of the ObjectResult classes, it results in a stack overflow.
I am currently using:
1. net core 3.1
2. azure functions v3
3. net sdk version 3.0.2
The code for the function looks like this, as I was trying to generate a payload for testing the fix in issue https://github.com/Azure/azure-functions-host/issues/3370. The issue happens when executing the line with return new ObjectResult(). The toReturn variable gets the information correctly. I am guessing the problem resides with how the serialization works
```csharp
public class TestFunction
{
[FunctionName(nameof(Test))]
public async Task Test(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "test")] Payload payload,
HttpRequest req)
{
var toReturn = GetPayload();
return new OkObjectResult(toReturn);
}
private Payload GetPayload()
{
var customCollection = new CustomCollection();
customCollection.AddRange(new[] { 1, 2, 3 });
return new Payload
{
CustomCollection = customCollection
};
}
}
public class Payload
{
public CustomCollection CustomCollection { get; set; }
}
public class CustomCollection : IEnumerable
{
List _items = new List();
public void Add(T item)
{
_items.Add(item);
}
public void AddRange(T[] items)
{
_items.AddRange(items);
}
public IEnumerator GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _items.GetEnumerator();
}
}
```
The output in the debug console was this:
```
[15.01.2020` 11:01:29] Executing HTTP request: {
[15.01.2020 11:01:29] "requestId": "6da9a51a-e437-4846-9e31-800328c394a5",
[15.01.2020 11:01:29] "method": "POST",
[15.01.2020 11:01:29] "uri": "/test"
[15.01.2020 11:01:29] }
[15.01.2020 11:01:29] Executing 'Test' (Reason='This function was programmatically called via the host APIs.', Id=4db12e7d-5583-45c3-b7bf-9d4ac7ba5f2a)
[15.01.2020 11:01:29] Executed 'Test' (Succeeded, Id=4db12e7d-5583-45c3-b7bf-9d4ac7ba5f2a)
Stack overflow.
C:\Users\\AppData\Local\AzureFunctionsTools\Releases\3.2.0\cli_x64\func.exe (process 4328) exited with code -1073741819.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
```
Please note that generic collections work properly for serializing deserializing as the bug 3370 stated
Contributor guide
Research direction
Reproduce the failure using the Function, Payload, and CustomCollection shown in the issue, then trace the ObjectResult serialization path. Done means the custom IEnumerable collection serializes without a stack overflow while the existing generic collection behavior remains working.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, csharp
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100