-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JsonSerializer throws serializing new object() with JsonSerializerDefaults.Web #61995
Comments
This looks to be an issue with using System.Text.Json;
// The following works:
await JsonSerializer.SerializeAsync(Stream.Null, new object(),
new JsonSerializerOptions(JsonSerializerDefaults.General));
await JsonSerializer.SerializeAsync(Stream.Null, new { },
new JsonSerializerOptions(JsonSerializerDefaults.Web));
// The following throws:
await JsonSerializer.SerializeAsync(Stream.Null, new object(),
new JsonSerializerOptions(JsonSerializerDefaults.Web)); The above
|
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescribe the bugI found use To Reproducethis is Code var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/test", () => new object());
app.Run(); Exceptionscurl -X 'GET' 'http://localhost:5066/test' -H 'accept: application/json' An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Operation is not valid due to the current state of the object.
at System.Text.Json.Serialization.JsonConverter`1.WriteNumberWithCustomHandling(Utf8JsonWriter writer, T value, JsonNumberHandling handling)
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteCore[TValue](JsonConverter jsonConverter, Utf8JsonWriter writer, TValue& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteStreamAsync[TValue](Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
at System.Text.Json.JsonSerializer.WriteStreamAsync[TValue](Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
at System.Text.Json.JsonSerializer.WriteStreamAsync[TValue](Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.HttpResponseJsonExtensions.WriteAsJsonAsyncSlow[TValue](Stream body, TValue value, JsonSerializerOptions options, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.ExecuteObjectReturn(Object obj, HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) Further technical details
运行时环境:
|
using System.Text.Json;
// The following throws:
var res1 = JsonSerializer.Serialize(new object(), new JsonSerializerOptions(JsonSerializerDefaults.Web));
Console.WriteLine(res1);
// this works
var res2 = JsonSerializer.Serialize(new object(), new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.Strict
});
Console.WriteLine(res2); the code in runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs Line 142 in f9dfd41
|
Minimal reproduction: using System.Text.Json;
using System.Text.Json.Serialization;
var options = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString };
JsonSerializer.Serialize(new object(), options); This is a regression introduced by #54436. Changes in how polymorphic serialization for I've posted PR #62020 which fixes the problem. We might consider that change for servicing, however I find the |
I agree it's a bit of a corner case, but this was found by an ASP.NET customer pretty quickly after releasing .NET 6. Getting this patched could save people some debugging in the future. |
+1 for a servicing change, this has been causing us a fair bit of grief. At least I now know the cause! |
Is there a workaround to get ASP to return an empty json response i.e. Edit: |
Describe the bug
I found use
return new object()
will throw exception in .Net6 !To Reproduce
this is Code
a minimal web API
Program.cs
Exceptions
curl -X 'GET' 'http://localhost:5066/test' -H 'accept: application/json'
Further technical details
dotnet --info
:PS> dotnet --info
.NET SDK (反映任何 global.json):
Version: 6.0.100
Commit: 9e8b04bbff
运行时环境:
OS Name: Windows
OS Version: 10.0.19043
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.100\
The text was updated successfully, but these errors were encountered: