-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Use Json TypeInfoResolverChain #47450
Conversation
- Removes all usages of JsonSerializerOptions.AddContext, which will be obsoleted by dotnet/runtime#83280 - Update ProblemDetailsJsonContext to be added just before the DefaultJsonTypeInfoResolver if it is the last resolver in the chain. Otherwise, add it to the end of a non-empty resolver chain. - Small clean up in the API template's usings
@@ -11,23 +11,27 @@ internal sealed class ProblemDetailsJsonOptionsSetup : IPostConfigureOptions<Jso | |||
{ | |||
public void PostConfigure(string? name, JsonOptions options) | |||
{ | |||
switch (options.SerializerOptions.TypeInfoResolver) | |||
var typeInfoResolverChain = options.SerializerOptions.TypeInfoResolverChain; | |||
if (typeInfoResolverChain.Count == 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I know this isn't new in this PR, but should we instead add ours and the reflection based one in this case?
Reason is because this configure call is specifically to add our ProblemDetailsJsonContext, but now because there aren't any resolvers you also get the reflection based resolver for Problem Details, feels wrong. Especially if the normal use case for getting no resolvers is when trimming is on, which is when you would want our provided context even more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After much off-GitHub discussion, we'd decided on the following approach:
Microsoft.AspNetCore.Http.Json.JsonOptions
sets theTypeInfoResolver
toDefaultJsonTypeInfoResolver
at construction-time based on the STJ feature switch allowing Reflection or not. If Reflection isn't allowed, it leaves it null. (This is how it works today - but based on the current ASP.NET feature switch).AddProblemDetails()
registers aConfigure<JsonOptions>()
call back, and always adds theProblemDetailsJsonContext
to the beginning of the chain at that time
This has the following behaviors:
- In a "normal" CoreCLR app, you get the chain
{ ProblemDetailsContext, DefaultResolver }
, like you want - If someone clears/nulls etc any of this after the fact, the behavior they get is determined by what they changed. At least it is deterministic and understandable.
- In a trimmed/aot app, we will only add
{ ProblemDetailsContext }
, and it is up to the app to add in their SG JsonContext, which can be done before or after the PD context, as they need- When someone doesn't, there will be no JsonTypeInfo for their types, and they will get the same behavior they do today
- This sets a pattern for other libraries who require the same behavior (adding their own source generated JsonContext to the resolver chain).
…tConfigure. When adding the ProblemDetailsJsonContext, we always prepend it to the beginning of the chain at the time the configure step is executed, no matter the state of the chain. This allows for a simpler, more understandable policy for all libraries that want to add their JsonContext into the JsonSerializerOptions resolver chain. The order of the chain is now determined by the order that the configure steps were registered in DI (for example when AddProblemDetails() was called).
src/Http/Http.Extensions/src/ProblemDetailsServiceCollectionExtensions.cs
Show resolved
Hide resolved
FYI - I plan on merging this today to unblock the runtime work in dotnet/runtime#84022. Let me know if you have further feedback. |
Configure<JsonOptions>()
call back and to always be added to the beginning of the resolver chain at that time