Repository navigation
Closed
dotnet/corefx
#40996Description
var test = new { Name = "测试11" };
Console.WriteLine(JsonSerializer.Serialize(test, new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }));Here's what's causing the StackOverflow:
Original text:
From @Zonciu on Tuesday, September 10, 2019 2:16:16 PM
Describe the bug
WebApi returning specific content cause stack overflow
To Reproduce
Reproduction project
Steps to reproduce the behavior:
- Using this version of NET Core 3.0 Preview 9
- dotnet new webapi
- Add controller methods:
public class Test
{
public string Name { get; set; }
}
[ApiController]
[Route("test")]
public class TestController : ControllerBase
{
[HttpGet("1")]
public Test Get([FromServices] ILogger<Test> logger)
{
logger.LogInformation("Get test 2");
return new Test {Name = "测试"};
}
[HttpGet("2")]
public Test Get2([FromServices] ILogger<Test> logger)
{
logger.LogInformation("Get test 2");
return new Test {Name = "测试11"};
}
}
- Access
/test/1is ok, but/test/2causing stack overflow and crashed.
Expected behavior
Access /test/2, return {"name":"测试1"}
Screenshots
Additional context
dotnet --info output
.NET Core SDK(反映任何 global.json):
Version: 3.0.100-preview9-014004
Commit: 8e7ef240a5
运行时环境:
OS Name: Windows
OS Version: 10.0.17763
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.0.100-preview9-014004\
Host (useful for support):
Version: 3.0.0-preview9-19423-09
Commit: 2be172345a
.NET Core SDKs installed:
2.1.801 [C:\Program Files\dotnet\sdk]
3.0.100-preview9-014004 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0-preview9.19424.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0-preview9-19423-09 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0-preview9-19423-09 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Copied from original issue: dotnet/aspnetcore#13856



Activity
pranavkm commentedon Sep 10, 2019
From @rynowak on Tuesday, September 10, 2019 2:51:25 PM
@ahsonkhan - the only differnce between these cases appears to be the text.
pranavkm commentedon Sep 10, 2019
From @mkArtakMSFT on Tuesday, September 10, 2019 4:00:42 PM
@rynowak I can repro this. yes, the only difference is the text.
pranavkm commentedon Sep 10, 2019
From @mkArtakMSFT on Tuesday, September 10, 2019 4:01:06 PM
Thanks for reporting this, @Zonciu.
pranavkm commentedon Sep 10, 2019
From @mkArtakMSFT on Tuesday, September 10, 2019 4:04:23 PM
pranavkm commentedon Sep 10, 2019
From @pranavkm on Tuesday, September 10, 2019 4:27:00 PM
Here's the minimal repro step without MVC involved:
Here's what's causing the StackOverflow:
It's specifically the
UnsafeRelaxedJsonEncodingwhich is what MVC uses by default for Json serialization.[-]WebApi return specific content cause stack overflow[/-][+]UnsafeRelaxedJsonEscaping can result in StackOverflowException[/+][-]UnsafeRelaxedJsonEscaping can result in StackOverflowException[/-][+][System.Text.Json] UnsafeRelaxedJsonEscaping can result in StackOverflowException[/+]ericstj commentedon Sep 10, 2019
Let's try to get this in 3.0. @ahsonkhan can you take a look.
pranavkm commentedon Sep 10, 2019
@Zonciu, in the meanwhile, you could configure MVC to use the default JavaScriptEncoder which works around this particular instance of the issue:
ahsonkhan commentedon Sep 11, 2019
@Zonciu, note, that particular workaround wouldn't work in general. I couldn't come up with a good workaround for the bug. Please re-try with the latest changes/nightly build and verify the issue has been fixed for your scenario. Otherwise, re-open the issue.
Fixed in master by dotnet/corefx#40996, and in release by dotnet/corefx#40997
Zonciu commentedon Sep 11, 2019
@ahsonkhan @pranavkm For now I use
AddControllers().AddNewtonsoftJson(), that works fine.ahsonkhan commentedon Sep 11, 2019
That works too. Let me know when you revert back to using the built-in serializer and if you find any bugs/issues or have some feedback to share.
xcaptain commentedon Sep 19, 2019
I have the same issue using
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscapingto serialize an object causes a stack overflow, is this bug fixed? I'm using dotnet core 3 preview 9Zonciu commentedon Sep 19, 2019
@xcaptain It has been fixed in nightly build, but not in 3.0 rc1
xcaptain commentedon Sep 19, 2019
I'm still having this issue even I upgraded to 3.0 rc1
My code is at https://dotnetfiddle.net/5rUTaW
if I change the
Nameto other string it runs. @Zonciu can you help test my case?Zonciu commentedon Sep 19, 2019
@xcaptain 3.0 rc1 doesn't contain this fix, you need to use Newtonsoft.Json, or try the daily build
I'd tested it in docker (nightly build 09/17/2019), it works.
xcaptain commentedon Sep 19, 2019
@Zonciu Thanks