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

Use Json TypeInfoResolverChain #47450

Merged
merged 2 commits into from
Mar 29, 2023
Merged

Conversation

eerhardt
Copy link
Member

@eerhardt eerhardt commented Mar 27, 2023

  • Removes all usages of JsonSerializerOptions.AddContext, which will be obsoleted by Obsolete JsonSerializerOptions.AddContext runtime#83280
  • Update ProblemDetailsJsonContext to be added in a Configure<JsonOptions>() call back and to always be added to the beginning of the resolver chain at that time
    • This gives us the simplest, most understandable pattern for all libraries to follow.
  • Small clean up in the API template's usings

- 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
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Mar 27, 2023
@@ -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)
Copy link
Member

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.

Copy link
Member Author

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:

  1. Microsoft.AspNetCore.Http.Json.JsonOptions sets the TypeInfoResolver to DefaultJsonTypeInfoResolver 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).
  2. AddProblemDetails() registers a Configure<JsonOptions>() call back, and always adds the ProblemDetailsJsonContext to the beginning of the chain at that time

This has the following behaviors:

  1. In a "normal" CoreCLR app, you get the chain { ProblemDetailsContext, DefaultResolver }, like you want
  2. 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.
  3. 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
  4. This sets a pattern for other libraries who require the same behavior (adding their own source generated JsonContext to the resolver chain).

@eerhardt eerhardt added area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Mar 28, 2023
…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).
@eerhardt
Copy link
Member Author

FYI - I plan on merging this today to unblock the runtime work in dotnet/runtime#84022. Let me know if you have further feedback.

@eerhardt eerhardt merged commit 7c0091a into dotnet:main Mar 29, 2023
@eerhardt eerhardt deleted the RemoveAddContext branch March 29, 2023 23:28
@ghost ghost added this to the 8.0-preview4 milestone Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants