-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
If the JSON source generator is enabled for the default JSON serializer options and an application uses the Results.Problem()
or Results.ValidationProblem()
methods, it is not possible to make the two work together.
If the HttpValidationProblemDetails
or ProblemDetails
is not registered with the JsonSerializerContext
, then an exception is thrown at runtime:
NotSupportedException: Metadata for type 'Microsoft.AspNetCore.Mvc.ProblemDetails' was not provided by TypeInfoResolver of type 'ApplicationJsonSerializerContext'. 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.
If a developer attempts to add either type using [JsonSerializable]
, then the application fails to compile:
Project\Repro\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\ApplicationJsonSerializerContext.ProblemDetails.g.cs(24,120): error CS0122: 'ProblemDetailsJsonConverter' is inaccessible due to its protection level
Expected Behavior
An application is successfully able to use Results.Problem()
or Results.ValidationProblem()
with JSON source generators and Minimal APIs.
Steps To Reproduce
A full repro project is available at martincostello/problemdetails-json-source-generator-repro.
To reproduce, clone the project and run dotnet build
.
Exceptions (if any)
No response
.NET Version
7.0.100-preview.7.22377.5
Anything else?
I think something somewhere in the JSON source generator has become more strict beginning in .NET 7 preview 7, which is what brought this to my attention.
Previously I think the source generator would just fallback to using runtime serialization so things would "just work" without you needing to add the [JsonSerializable]
attribute.
It seems like it would be fairly trivial to fix this use case by making HttpValidationProblemDetailsJsonConverter
and ProblemDetailsJsonConverter
public instead of internal, but as they're shared source that might cause other complications with multiple public types with the same name and namespace in different assemblies.