Announcements
Announcements copied to clipboard
[Breaking change]: Using the System.Text.Json source generator with ASP. NET Core throws NotSupportedException in RC1 *only*
Description
In .NET 7 RC1, we started requiring that the type object
to be explicitly specified with a [JsonSerializable]
attribute applied to your Json Serializer context class in Minimal APIs applications using System.Text.Json
source generation. This was an unexpected behavioral change that we will be fixed in RC2.
Version
.NET 7 RC1
Previous behavior
Before .NET 7 RC1, if you are using System.Text.Json
source generation with ASP.NET Core Minimal APIs
your endpoint's response will be serialized (JSON
format) correctly when you have your custom types declared in the JsonSerializerContext
.
The following code shows an example using System.Text.Json
source generation with ASP.NET Core Minimal APIs
:
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureHttpJsonOptions(o => o.SerializerOptions.AddContext<JsonContext>());
var app = builder.Build();
app.MapGet("/", () =>
{
var l = new List<MyClass> { new(), new() };
return Results.Ok(l);
});
app.Run();
[JsonSerializable(typeof(List<MyClass>))]
public partial class JsonContext : JsonSerializerContext
{}
public class MyClass
{
public int MyProp { get; set; }
}
New behavior
In .NET RC 1 and RC 1 only (this is being fixed in RC 2) the same sample application will throw a NotSupportedException
.
NotSupportedException: Metadata for type 'System.Object' was not provided by TypeInfoResolver of type 'JsonContext'.
If using source generation, ensure that all root types passed to the serializer have been indicated with 'JsonSerializableAttribute',
along with any types that might be serialized polymorphically.
System.Text.Json.ThrowHelper.ThrowNotSupportedException_NoMetadataForType(Type type, IJsonTypeInfoResolver resolver)
See https://github.com/dotnet/aspnetcore/issues/43894 for more context.
Type of breaking change
- [ ] Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load/execute or different run-time behavior.
- [ ] Source incompatible: Source code may encounter a breaking change in behavior when targeting the new runtime/component/SDK, such as compile errors or different run-time behavior.
- [X] Behavioral change: Existing code and binaries may experience different run-time behavior.
Reason for change
This is an unexpected side effect cause by a combination of multiple changes, listed below, detected only after .NET 7 RC1 release.
.NET Runtime
- https://github.com/dotnet/docs/issues/30758
- https://github.com/dotnet/docs/issues/30755
ASP.NET CORE
- https://github.com/dotnet/aspnetcore/issues/39856
- https://github.com/dotnet/aspnetcore/issues/41724#issuecomment-1142735823
The root cause was already identified, and a fix will be available in .NET 7 RC2.
Recommended action
You can add the [JsonSerializable(typeof(object))]
to your JsonSerializerContext or wait for RC 2 when this unexpected behavior will be fixed.
[JsonSerializable(typeof(List<MyClass>))]
[JsonSerializable(typeof(object))]
public partial class JsonContext : JsonSerializerContext
{}
Affected APIs
- Minimal APIs
- Controllers (using Microsoft.AspNetCore.Http.HttpResults)