You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;varbuilder= WebApplication.CreateBuilder(args);
builder.Services.ConfigureHttpJsonOptions(o => o.SerializerOptions.AddContext<JsonContext>());varapp= builder.Build();
app.MapGet("/",()=>{varl=newList<MyClass>{new(),new()};return Results.Ok(l);});
app.Run();[JsonSerializable(typeof(List<MyClass>))]publicpartialclassJsonContext:JsonSerializerContext{}publicclassMyClass{publicintMyProp{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)
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.
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.
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 usingSystem.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 withASP.NET Core Minimal APIs
your endpoint's response will be serialized (JSON
format) correctly when you have your custom types declared in theJsonSerializerContext
.The following code shows an example using
System.Text.Json
source generation withASP.NET Core Minimal APIs
: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
.See dotnet/aspnetcore#43894 for more context.
Type of breaking change
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
object
types dotnet/docs#30758ASP.NET CORE
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.Affected APIs
The text was updated successfully, but these errors were encountered: