-
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
[AOT] Fixing the problems with ProblemDetails #45646
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
81c060d
[AOT] Fixing the problems with ProblemDetails
JamesNK dea2dc4
Clean up
JamesNK ef9a963
Clean up
JamesNK 059e731
Fix tests
JamesNK 1e0fb4d
Comment
JamesNK 0ca6b47
Update
JamesNK 20ce35b
Update
JamesNK 6d21d57
PR feedback
JamesNK 5a85857
PR feedback
JamesNK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Options; | ||
|
@@ -43,20 +45,20 @@ public bool CanWrite(ProblemDetailsContext context) | |
} | ||
|
||
[UnconditionalSuppressMessage("Trimming", "IL2026", | ||
Justification = "JSON serialization of ProblemDetails.Extensions might require types that cannot be statically analyzed and we need to fallback" + | ||
"to reflection-based. The ProblemDetailsConverter is marked as RequiresUnreferencedCode already.")] | ||
Justification = "JSON serialization of ProblemDetails.Extensions might require types that cannot be statically analyzed. The property is annotated with a warning")] | ||
[UnconditionalSuppressMessage("Trimming", "IL3050", | ||
Justification = "JSON serialization of ProblemDetails.Extensions might require types that cannot be statically analyzed and we need to fallback" + | ||
"to reflection-based. The ProblemDetailsConverter is marked as RequiresDynamicCode already.")] | ||
Justification = "JSON serialization of ProblemDetails.Extensions might require types that cannot be statically analyzed. The property is annotated with a warning")] | ||
public ValueTask WriteAsync(ProblemDetailsContext context) | ||
{ | ||
var httpContext = context.HttpContext; | ||
ProblemDetailsDefaults.Apply(context.ProblemDetails, httpContext.Response.StatusCode); | ||
_options.CustomizeProblemDetails?.Invoke(context); | ||
|
||
if (context.ProblemDetails.Extensions is { Count: 0 }) | ||
// Use source generation serialization in two scenarios: | ||
// 1. There are no extensions. Source generation is faster and works well with trimming. | ||
// 2. Native AOT. In this case only the data types specified on ProblemDetailsJsonContext will work. | ||
if (context.ProblemDetails.Extensions is { Count: 0 } || !RuntimeFeature.IsDynamicCodeSupported) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would probably be a good candidate for an "AOT-specific" test, similar to how we test trimming and AOT in dotnet/runtime. For example: https://github.com/dotnet/runtime/tree/main/src/libraries/System.Diagnostics.DiagnosticSource/tests/NativeAotTests. |
||
{ | ||
// We can use the source generation in this case | ||
return new ValueTask(httpContext.Response.WriteAsJsonAsync( | ||
context.ProblemDetails, | ||
ProblemDetailsJsonContext.Default.ProblemDetails, | ||
|
@@ -69,7 +71,22 @@ public ValueTask WriteAsync(ProblemDetailsContext context) | |
contentType: "application/problem+json")); | ||
} | ||
|
||
// Additional values are specified on JsonSerializerContext to support some values for extensions. | ||
// For example, the DeveloperExceptionMiddleware serializes its complex type to JsonElement, which problem details then needs to serialize. | ||
[JsonSerializable(typeof(ProblemDetails))] | ||
[JsonSerializable(typeof(JsonElement))] | ||
[JsonSerializable(typeof(string))] | ||
[JsonSerializable(typeof(decimal))] | ||
[JsonSerializable(typeof(float))] | ||
[JsonSerializable(typeof(double))] | ||
[JsonSerializable(typeof(int))] | ||
[JsonSerializable(typeof(long))] | ||
[JsonSerializable(typeof(Guid))] | ||
[JsonSerializable(typeof(Uri))] | ||
[JsonSerializable(typeof(TimeSpan))] | ||
[JsonSerializable(typeof(DateTime))] | ||
[JsonSerializable(typeof(DateTimeOffset))] | ||
internal sealed partial class ProblemDetailsJsonContext : JsonSerializerContext | ||
{ } | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@eerhardt @jkotas @vitek-karas @davidfowl Like #45604 (comment), this is a place where dotnet/runtime#39806 would be useful.
Developers being able to run the "AOT path" while writing and debugging their app would help them find incompatible extension values during development instead of when they publish as AOT for production.
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.
Would it fail to serialize if we did that in "build"? Or would the reflection based serializer kick in?
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.
It would fail at runtime. This is what we're going to be doing manually today for AOT based JSON in ASP.NET Core. The reflection fallback would be disabled and our calls to resolve the JsonTypeInfo would return null.
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.
In that case - let's do dotnet/runtime#39806.
We just need to "hide" it enough so that people don't use it as the first option to solve AOT problems (still better than current behavior, but ideally we want them to really fix things)