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

net7: ValidationProblemDetails cannot be handled by JsonSerializerContext #83815

Closed
oising opened this issue Mar 23, 2023 · 5 comments
Closed

Comments

@oising
Copy link

oising commented Mar 23, 2023

Description

When attempting to add the new Json serializer context source generators to my NET 7 MVC project, and following the apparently breaking changes in #77115, my Web Api attempted to return a ValidationProblemDetails and I received a runtime error of:

System.NotSupportedException: Metadata for type 'Microsoft.AspNetCore.Mvc.ValidationProblemDetails' was not provided by TypeInfoResolver of type 'MyJsonSerializationContext'. 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.

So, I added [JsonSerializable(typeof(Microsoft.AspNetCore.Mvc.ValidationProblemDetails))] to the JsonSerializerContext and now I get the following at compile time:

'ValidationProblemDetailsJsonConverter' is inaccessible due to its protection level.

Reproduction Steps

  • create a dotnet 7 webapi project
  • return a custom type from a webapi method
  • create a custom json serializer context for this custom type
  • test return of the custom type (should be ok)
  • introduce a runtime error in the return of the method, returning a ValidationProblemDetails

This will introduce the runtime error. Follow the instructions from the runtime, and you will receive the previously mentioned compile-time error as the source generator cannot generate boilerplate for VPD.

Expected behavior

[JsonSerializable(typeof(Microsoft.AspNetCore.Mvc.ValidationProblemDetails))] should work on the custom context.

Actual behavior

[JsonSerializable(typeof(Microsoft.AspNetCore.Mvc.ValidationProblemDetails))] fails.

Regression?

A likely regression implied via #77115 but I cannot test here.

Known Workarounds

No idea.

#if DOTNET6_0 || DOTNET8_0            
            options.JsonSerializerOptions.AddContext<MySerializationContext>();
#endif

:(

Configuration

dotnet sdk 7.0.4

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Mar 23, 2023
@ghost
Copy link

ghost commented Mar 23, 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

When attempting to add the new Json serializer context source generators to my NET 7 MVC project, and following the apparently breaking changes in #77115, my Web Api attempted to return a ValidationProblemDetails and I received a runtime error of:

System.NotSupportedException: Metadata for type 'Microsoft.AspNetCore.Mvc.ValidationProblemDetails' was not provided by TypeInfoResolver of type 'MyJsonSerializationContext'. 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.

So, I added [JsonSerializable(typeof(Microsoft.AspNetCore.Mvc.ValidationProblemDetails))] to the JsonSerializerContext and now I get the following at compile time:

'ValidationProblemDetailsJsonConverter' is inaccessible due to its protection level.

Reproduction Steps

  • create a dotnet 7 webapi project
  • return a custom type from a webapi method
  • create a custom json serializer context for this custom type
  • test return of the custom type (should be ok)
  • introduce a runtime error in the return of the method, returning a ValidationProblemDetails

This will introduce the runtime error. Follow the instructions from the runtime, and you will receive the previously mentioned compile-time error as the source generator cannot generate boilerplate for VPD.

Expected behavior

[JsonSerializable(typeof(Microsoft.AspNetCore.Mvc.ValidationProblemDetails))] should work on the custom context.

Actual behavior

[JsonSerializable(typeof(Microsoft.AspNetCore.Mvc.ValidationProblemDetails))] fails.

Regression?

A likely regression implied via #77115 but I cannot test here.

Known Workarounds

No idea.

Configuration

dotnet sdk 7.0.4

Other information

No response

Author: oising
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@oising
Copy link
Author

oising commented Mar 23, 2023

Additionally, I'd love to know if there's a way to reserve the Json source generators for specific types and to just let the older runtime behaviour manage non-critical types (such as ValidationProblem) -- I find the documentation and subsequent changes between net6 and net7 and associated bugs around it too confusing.

@eiriktsarpalis
Copy link
Member

cc @eerhardt. The ASP.NET team is working to provide a solution specifically for that use case in .NET 8.

As a temporary workaround, you could try using configuring your JsonSerializerOptions to use reflection-based fallback:

options.TypeInfoResolver = JsonTypeInfoResolver.Combine(MySerializationContext.Default, new DefaultJsonTypeInfoResolver());

@eiriktsarpalis
Copy link
Member

Addressed in dotnet/aspnetcore#43236. Should be fixed in .NET 8 Preview-2

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Mar 23, 2023
@oising
Copy link
Author

oising commented Mar 23, 2023

cc @eerhardt. The ASP.NET team is working to provide a solution specifically for that use case in .NET 8.

As a temporary workaround, you could try using configuring your JsonSerializerOptions to use reflection-based fallback:

options.TypeInfoResolver = JsonTypeInfoResolver.Combine(MySerializationContext.Default, new DefaultJsonTypeInfoResolver());

A minor correction to your workaround (for anyone else who finds this thread):

options.**JsonSerializerOptions**.TypeInfoResolver = JsonTypeInfoResolver.Combine( ...

@ghost ghost locked as resolved and limited conversation to collaborators Apr 22, 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