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

Making JsonOptions AOT/Trimmer-safe with EnsureJsonTrimmability switch #45886

Merged
merged 11 commits into from
Jan 20, 2023

Conversation

brunolins16
Copy link
Member

@brunolins16 brunolins16 commented Jan 5, 2023

@eerhardt
Copy link
Member

eerhardt commented Jan 9, 2023

This looks good to start, but we should have code that switches based on this setting.

@brunolins16
Copy link
Member Author

brunolins16 commented Jan 9, 2023

This looks good to start, but we should have code that switches based on this setting.

💯 I was holding this PR until #45405 is merged to have JsonOptions behave based on this switch, but it is taking longer than I expected.

@brunolins16 brunolins16 marked this pull request as draft January 10, 2023 21:33
@brunolins16 brunolins16 marked this pull request as ready for review January 12, 2023 00:41
@brunolins16 brunolins16 changed the title Adding EnsureJsonTrimmability switch Making JsonnOptions AOT/Trimmer-safe with EnsureJsonTrimmability switch Jan 12, 2023
@brunolins16 brunolins16 changed the title Making JsonnOptions AOT/Trimmer-safe with EnsureJsonTrimmability switch Making JsonOptions AOT/Trimmer-safe with EnsureJsonTrimmability switch Jan 12, 2023
@JamesNK
Copy link
Member

JamesNK commented Jan 13, 2023

Please rebase 🙏

There have been some suppressions added to JsonOptions since the changes were made. Should verify that they are no longer needed with this improvement.

@JamesNK
Copy link
Member

JamesNK commented Jan 13, 2023

With this change, should the warnings be removed from ProblemDetails.Extensions?

@brunolins16
Copy link
Member Author

With this change, should the warnings be removed from ProblemDetails.Extensions?

Excellent question. IDK, users can now specify the types,needed for Extensions, in their own context and will combine with the internal context #45906 (review)

@davidfowl
Copy link
Member

Yes, the warnings should be removed.

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to get some tests written when Microsoft.AspNetCore.EnsureJsonTrimmability == false. In dotnet/runtime, we use the RemoteExecutor class to run test code with different runtime config options. See https://github.com/dotnet/runtime/blob/df9de14e807b5327977bcd288fac5dfb4a6c6a6b/src/libraries/System.Reflection.Emit.Lightweight/tests/DynamicMethodCtor.cs#L178-L201 for an example.

@@ -32,13 +32,14 @@ public static class HttpRequestJsonExtensions
/// <param name="request">The request to read from.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the operation.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
[RequiresUnreferencedCode(RequiresUnreferencedCodeMessage)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why havne't we done this for all methods in this class? For example,

    public static async ValueTask<TValue?> ReadFromJsonAsync<TValue>(
        this HttpRequest request,
        JsonSerializerOptions? options,
        CancellationToken cancellationToken = default)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar question for HttpResponseJsonExtensions.

Copy link
Member Author

@brunolins16 brunolins16 Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the user provides a JsonSerializerOptions the TypeInfoResolver could be null, default value, and we cannot call GetTypeInfo dotnet/runtime#80529

@@ -26,7 +27,7 @@ public class JsonOptions
// The JsonSerializerOptions.GetTypeInfo method is called directly and needs a defined resolver
// setting the default resolver (reflection-based) but the user can overwrite it directly or calling
// .AddContext<TContext>()
TypeInfoResolver = CreateDefaultTypeResolver()
TypeInfoResolver = TrimmingAppContextSwitches.EnsureJsonTrimmability ? null : CreateDefaultTypeResolver()
};

// Use a copy so the defaults are not modified.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the plan for the suppressions on line 39-44?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, they will suppress in the Microsoft.AspNetCore.Http.Extensions.WarningSuppressions.xml and I will file an issue to start a discussion if we need something similar to runtime (LibraryBuild.xml).

@brunolins16 brunolins16 requested review from a team, dougbu and wtgodbe as code owners January 18, 2023 22:43
@brunolins16
Copy link
Member Author

brunolins16 commented Jan 18, 2023

@dougbu @wtgodbe I don't know much about the testing infrastructure, but I tried to follow the pattern we already have. Feel free to point out what is the right approach to introduce the Microsoft.Dotnet.RemoteExecutor package.

5b432db

@wtgodbe
Copy link
Member

wtgodbe commented Jan 18, 2023

@dougbu @wtgodbe I don't know much about the testing infrastructure, but I tried to follow the pattern we already have. Feel free to point out what is the right approach to introduce the Microsoft.Dotnet.RemoteExecutor package.

Looks good to me

@brunolins16
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants