Skip to content
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

[Breaking change]: Using the System.Text.Json source generator with ASP. NET Core throws NotSupportedException in RC1 *only* #494

Open
1 of 3 tasks
brunolins16 opened this issue Sep 16, 2022 · 0 comments

Comments

@brunolins16
Copy link
Contributor

brunolins16 commented Sep 16, 2022

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 dotnet/aspnetcore#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.
  • 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

  1. [Breaking change]: System.Text.Json no longer hardcodes polymorphism for root-level object types dotnet/docs#30758
  2. [Breaking change]: System.Text.Json source generator no longer fall backs to reflection-based serialization dotnet/docs#30755

ASP.NET CORE

  1. Async minimal route handlers don't serialize child members dotnet/aspnetcore#39856
  2. Add generic method overloads to Microsoft.AspNetCore.Http.Results static class dotnet/aspnetcore#41724 (comment)

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

@ghost ghost locked as resolved and limited conversation to collaborators Sep 16, 2022
@guardrex guardrex added the 7.0.0 label Nov 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants