-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Cannot use JSON source generator with (HttpValidation)ProblemDetails #43236
Comments
@martincostello I worked with the JSON source generation for problem details when developing the
Unless if you knowo all types that will be there at runtime and added then to your context. |
Did this issue not occur prior to preview7? |
In the context of where it was occurring where I found it, no. I have a bit of custom code in an example app that's used to generate examples for the OpenAPI docs with Swashbuckle.AspNetCore that was using the default JSON serializer which had a serializer context associated with it. Since updating to preview 7, that started to throw. To "fix" it, I had to reconfigure the filter to use the copy-constructor to create a separate options and unassociate the context: martincostello/api@6d37785 |
@dotnet/area-system-text-json do you know what might be going on here? Did something change in preview7 to cause this problem when using the source generator?
|
This seems related to an intentional breaking change we made in Preview 7. See dotnet/runtime#71714 for more details and a workaround.
The error message appears related to a bug fixed recently in dotnet/runtime#71714, cc @krwq. |
Thanks for the pointer at the workaround @eiriktsarpalis - I got this back to working how it was in preview 6 with this change: martincostello/api@b43a46c |
I just struggled through the same exact issue with I started a discussion over in dotnet/runtime asking about the implications. It would be nice to have a way around this without needing to fallback to the reflection resolver. Though... perhaps it won't be an issue once Problem Details service is in the mix |
FWIW I should say that adding reflection-based fallback defeats the purpose of using source generators. It seems unfortunate that we need to rely on reflection (accidentally in .NET 6, explicitly starting with .NET 7) to add serialization support for the validation models. It sounds to me like MVC might want to expose a builder.Services.ConfigureRouteHandlerJsonOptions((options) =>
{
options.TypeInfoResolver = JsonTypeInfoResolver.Combine(ApplicationJsonSerializerContext.Default, ValidationContext.Default);
}); cc @davidfowl |
Those of us migrating from @khellang's The options would be:
As the converters are specified as class-level attributes the second option doesn't seem possible. If it were, we'd likely need (or at least want) a way to subclass the current It seems like our only options are to either remove the property or be at peace with the fact that we need the reflection fallback just to cover this case. That may be alright, but it could cause other types to slip through the cracks if we accidentally miss annotating them as we'd no longer receive the source-gen errors that we get with the latest previews. |
Adding the aspnetcore/src/Mvc/Mvc.Core/src/Infrastructure/DefaultProblemDetailsFactory.cs Lines 95 to 99 in f6362e0
For my middleware, it's easily excluded by setting |
In that case, wouldn't it cause all For what it's worth, the new services uses
I don't want to remove it; I'd like to keep it =) |
Then what hits this code path? 🤔 aspnetcore/src/Mvc/Mvc.Core/src/Infrastructure/DefaultApiProblemDetailsWriter.cs Lines 54 to 70 in f6362e0
|
I blame early morning coffee-less code interpreting =) |
Triage question: Is there anything we can or should do to help with
Are you suggesting that we add a new public Would it be okay to just call aspnetcore/src/Shared/ProblemDetails/ProblemDetailsJsonConverter.cs Lines 43 to 48 in f1353bd
|
JsonSerializerContext already implements that interface via the source generator, so it really just boils down to defining your own source generated context for the required types. |
The problem (at least in my case) is that I'm using source-gen with my MVC API. By default The work-around is to enable the reflection-fallback but that seems like it defeats the purpose. If aspnet exposed a |
Are you using source generators for performance reasons? |
Sort of. Prior to source-gen we had custom converters for all of our types after a couple of my colleagues did some extensive profiling and saw enough improvement that it was felt to be warranted. From there we saw minor improvements with fast-path source-gen serialization, not enough to justify it on its own, but the cost of not needing to maintain custom converters is what caused the switch. Going forward, we are starting to look at getting our apps trimmable. We aren't there yet, but are trying to avoid adding anything new that could bring trimmer warnings. |
@eiriktsarpalis So basically, what you are saying is we could make something like this public (probably including
As I mentioned before, the |
Unfortunately, no. Relevant issue: dotnet/runtime#58134. Even if we do address it for common primitives, fundamentally the experience won't change: any runtime types will need to be explicitly opted in. |
😟 So, since MVC adds the aspnetcore/src/Mvc/Mvc.Core/src/Infrastructure/DefaultProblemDetailsFactory.cs Lines 95 to 99 in f6362e0
|
@martincostello In .NET 8, we changed to when |
@martincostello #44132 is completed and you should be able from .NET 8 Preview 2 source-gen a problemdetails, if needed |
Is there an existing issue for this?
Describe the bug
If the JSON source generator is enabled for the default JSON serializer options and an application uses the
Results.Problem()
orResults.ValidationProblem()
methods, it is not possible to make the two work together.If the
HttpValidationProblemDetails
orProblemDetails
is not registered with theJsonSerializerContext
, then an exception is thrown at runtime:If a developer attempts to add either type using
[JsonSerializable]
, then the application fails to compile:Expected Behavior
An application is successfully able to use
Results.Problem()
orResults.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
andProblemDetailsJsonConverter
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.The text was updated successfully, but these errors were encountered: