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

Using JsonTypeInfo<object> with JsonSerializerContext configured throws error during serialization steps #87073

Closed
captainsafia opened this issue Jun 2, 2023 · 2 comments · Fixed by #87093
Assignees
Labels
area-System.Text.Json blocking-release partner-impact This issue impacts a partner who needs to be kept updated
Milestone

Comments

@captainsafia
Copy link
Member

Description

The ASP.NET framework derives a JsonTypeInfo<object> for use in the serialization of filter functions that can return any object type. When used in serialization with a JsonContext, the JsonTypeInfo<object> throws an unhandled exception to the user.

Reproduction Steps

using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

builder.Services.ConfigureHttpJsonOptions(o => o.SerializerOptions.TypeInfoResolver = JsonContext.Default);

var app = builder.Build();

app.MapGet("/", () => "Hello World!")
    .AddEndpointFilter(async (c, n) => {
        var r = await n(c);
        return Results.Ok(new Todo("Test todo", false));
    });

app.Run();

public record Todo(string Name, bool IsCompleted);


[JsonSerializable(typeof(Todo))]
public partial class JsonContext : JsonSerializerContext
{ }

Expected behavior

Serialization of the payload to complete successfully.

Actual behavior

System.NotSupportedException: JsonTypeInfo metadata for type 'System.Object' was not provided by TypeInfoResolver of type 'Microsoft.AspNetCore.Http.G...

System.NotSupportedException
JsonTypeInfo metadata for type 'System.Object' was not provided by TypeInfoResolver of type 'Microsoft.AspNetCore.Http.Generators.Tests.SharedTestJsonContext'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.

Regression?

No response

Known Workarounds

No response

Configuration

dotnet --info
.NET SDK:
 Version:   8.0.100-preview.5.23275.15
 Commit:    7a4252d678

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  13.3
 OS Platform: Darwin
 RID:         osx.13-arm64
 Base Path:   /Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk/8.0.100-preview.5.23275.15/

.NET workloads installed:
There are no installed workloads to display.

Host:
  Version:      8.0.0-preview.6.23280.5
  Architecture: arm64
  Commit:       bf7fb2ecbf

.NET SDKs installed:
  6.0.116 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk]
  8.0.100-preview.3.23159.20 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk]
  8.0.100-preview.4.23176.5 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk]
  8.0.100-preview.5.23226.2 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk]
  8.0.100-preview.5.23257.1 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk]
  8.0.100-preview.5.23275.15 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.16 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.3.23159.3 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.4.23174.2 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.4.23225.3 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.5.23252.26 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.5.23273.2 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.16 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.3.23158.1 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.3.23159.4 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.4.23174.4 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.4.23206.2 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.4.23218.5 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.4.23225.9 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.5.23227.1 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.5.23251.1 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.5.23252.13 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.5.23271.5 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.5.23272.14 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.6.23279.6 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.6.23280.5 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  x64   [/usr/local/share/dotnet/x64]

Environment variables:
  DOTNET_ROOT       [/Users/captainsafia/repos/2-aspnetcore/.dotnet]

global.json file:
  /Users/captainsafia/repos/2-aspnetcore/global.json

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jun 2, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jun 2, 2023
@captainsafia
Copy link
Member Author

cc: @eiriktsarpalis @eerhardt

@eiriktsarpalis eiriktsarpalis added area-System.Text.Json and removed untriaged New issue has not been triaged by the area owner needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jun 2, 2023
@eiriktsarpalis eiriktsarpalis added this to the 8.0.0 milestone Jun 2, 2023
@eiriktsarpalis eiriktsarpalis self-assigned this Jun 2, 2023
@ghost
Copy link

ghost commented Jun 2, 2023

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

The ASP.NET framework derives a JsonTypeInfo<object> for use in the serialization of filter functions that can return any object type. When used in serialization with a JsonContext, the JsonTypeInfo<object> throws an unhandled exception to the user.

Reproduction Steps

using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

builder.Services.ConfigureHttpJsonOptions(o => o.SerializerOptions.TypeInfoResolver = JsonContext.Default);

var app = builder.Build();

app.MapGet("/", () => "Hello World!")
    .AddEndpointFilter(async (c, n) => {
        var r = await n(c);
        return Results.Ok(new Todo("Test todo", false));
    });

app.Run();

public record Todo(string Name, bool IsCompleted);


[JsonSerializable(typeof(Todo))]
public partial class JsonContext : JsonSerializerContext
{ }

Expected behavior

Serialization of the payload to complete successfully.

Actual behavior

System.NotSupportedException: JsonTypeInfo metadata for type 'System.Object' was not provided by TypeInfoResolver of type 'Microsoft.AspNetCore.Http.G...

System.NotSupportedException
JsonTypeInfo metadata for type 'System.Object' was not provided by TypeInfoResolver of type 'Microsoft.AspNetCore.Http.Generators.Tests.SharedTestJsonContext'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.

Regression?

No response

Known Workarounds

No response

Configuration

dotnet --info
.NET SDK:
 Version:   8.0.100-preview.5.23275.15
 Commit:    7a4252d678

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  13.3
 OS Platform: Darwin
 RID:         osx.13-arm64
 Base Path:   /Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk/8.0.100-preview.5.23275.15/

.NET workloads installed:
There are no installed workloads to display.

Host:
  Version:      8.0.0-preview.6.23280.5
  Architecture: arm64
  Commit:       bf7fb2ecbf

.NET SDKs installed:
  6.0.116 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk]
  8.0.100-preview.3.23159.20 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk]
  8.0.100-preview.4.23176.5 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk]
  8.0.100-preview.5.23226.2 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk]
  8.0.100-preview.5.23257.1 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk]
  8.0.100-preview.5.23275.15 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.16 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.3.23159.3 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.4.23174.2 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.4.23225.3 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.5.23252.26 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.5.23273.2 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.16 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.3.23158.1 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.3.23159.4 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.4.23174.4 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.4.23206.2 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.4.23218.5 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.4.23225.9 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.5.23227.1 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.5.23251.1 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.5.23252.13 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.5.23271.5 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.5.23272.14 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.6.23279.6 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.6.23280.5 [/Users/captainsafia/repos/2-aspnetcore/.dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  x64   [/usr/local/share/dotnet/x64]

Environment variables:
  DOTNET_ROOT       [/Users/captainsafia/repos/2-aspnetcore/.dotnet]

global.json file:
  /Users/captainsafia/repos/2-aspnetcore/global.json

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Other information

No response

Author: captainsafia
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@eiriktsarpalis eiriktsarpalis added blocking-release partner-impact This issue impacts a partner who needs to be kept updated labels Jun 2, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jun 3, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jun 5, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json blocking-release partner-impact This issue impacts a partner who needs to be kept updated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants