-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Adding ProblemDetailsService #42384
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
Merged
brunolins16
merged 65 commits into
dotnet:main
from
brunolins16:brunolins16/issues/32957
Jul 14, 2022
Merged
Adding ProblemDetailsService #42384
Changes from all commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
15126b5
MVC Changes
brunolins16 00e7615
ExceptionHandler changes
brunolins16 9adc269
Routing changes
brunolins16 c430ae2
Http.Extensions changes
brunolins16 a8939e5
Minimal APi draft changes
brunolins16 79690d8
HttpResults changes
brunolins16 c548e2b
New ProblemDetails project
brunolins16 7f368a8
Using ProblemDetailsOptions in mvc
brunolins16 acb590b
ProblemDetails project simplification
brunolins16 62eb53e
Using metadata
brunolins16 9618302
Using metadata
brunolins16 a747f04
Merge branch 'main' into brunolins16/issues/32957
brunolins16 8e25910
Latest version
brunolins16 22b0932
Removing changes
brunolins16 f6a5c37
Initial cleanup
brunolins16 b2fabfb
Merge branch 'main' into brunolins16/issues/32957
brunolins16 c13a1bd
Clean up
brunolins16 80aa845
Public api clean up
brunolins16 e0d4333
Updating public API
brunolins16 3fa6af0
More clean ups
brunolins16 89fd8ec
Adding initial unit tests
brunolins16 dca5c73
Updating unit tests
brunolins16 e885b3a
Adding more unit tests
brunolins16 a593c2b
Merge branch 'main' into brunolins16/issues/32957
brunolins16 9877120
Cleanup
brunolins16 2e0c98e
Clean up
brunolins16 e8f0e1a
clean up
brunolins16 6316af7
clean up
brunolins16 0ed099e
Removing nullable
brunolins16 a883706
Simplifying public api
brunolins16 1ecb0e0
API Review feedback
brunolins16 cfb8f88
Merge branch 'main' into brunolins16/issues/32957
brunolins16 6813d58
API review feedback
brunolins16 6b15f58
Clean up
brunolins16 a50ef62
Clean up
brunolins16 6565091
clean up
brunolins16 550dd9d
Reusing Endpoint & EndpointMetadataCollection
brunolins16 f9fd5f6
Fix build issues
brunolins16 804ccac
Updates based on docs/Trimming.md
brunolins16 54e369d
Adding Functional tests
brunolins16 1e695aa
Fix unittest
brunolins16 2a743db
Seal context
brunolins16 8bbe54c
Seal ProblemMetadata
brunolins16 a04b7ae
PR Feeback
brunolins16 cf188c2
API Review
brunolins16 5be8bd9
Clean up
brunolins16 86401b7
Fixing publicapi warnings
brunolins16 72b97a1
Merge branch 'main' into brunolins16/issues/32957
brunolins16 3f58002
Clean up
brunolins16 7dadaed
PR Feedback
brunolins16 5cb3286
Fixing build
brunolins16 e03c3e3
Fix unit test
brunolins16 988a20e
Fix unit tests
brunolins16 8c823a4
PR review
brunolins16 483f54e
Fixing JsonSerializationContext issues
brunolins16 35de3fe
Adding statuscode 405
brunolins16 1ba1f10
Update src/Http/Http.Extensions/src/ProblemDetailsDefaultWriter.cs
brunolins16 2455ccb
PR review
brunolins16 cd92e30
Apply suggestions from code review
brunolins16 d35ff2b
Merge branch 'main' into brunolins16/issues/32957
brunolins16 55cb7da
Fixing bad merge
brunolins16 8585070
Fix bad merge
brunolins16 2c12741
Adding analysis.NextMiddlewareName
brunolins16 4b84cf3
PR review
brunolins16 1fe3f3b
Changing to CanWrite/WriteAsync
brunolins16 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
File renamed without changes.
23 changes: 23 additions & 0 deletions
23
src/Http/Http.Abstractions/src/ProblemDetails/IProblemDetailsService.cs
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Http; | ||
|
||
/// <summary> | ||
/// Defines a type that provide functionality to | ||
/// create a <see cref="Mvc.ProblemDetails"/> response. | ||
/// </summary> | ||
public interface IProblemDetailsService | ||
{ | ||
/// <summary> | ||
/// Try to write a <see cref="Mvc.ProblemDetails"/> response to the current context, | ||
/// using the registered <see cref="IProblemDetailsWriter"/> services. | ||
/// </summary> | ||
/// <param name="context">The <see cref="ProblemDetailsContext"/> associated with the current request/response.</param> | ||
/// <remarks>The <see cref="IProblemDetailsWriter"/> registered services | ||
/// are processed in sequence and the processing is completed when: | ||
/// <list type="bullet">One of them reports that the response was written successfully, or.</list> | ||
/// <list type="bullet">All <see cref="IProblemDetailsWriter"/> were executed and none of them was able to write the response successfully.</list> | ||
/// </remarks> | ||
ValueTask WriteAsync(ProblemDetailsContext context); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Http/Http.Abstractions/src/ProblemDetails/IProblemDetailsWriter.cs
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Http; | ||
|
||
/// <summary> | ||
/// Defines a type that write a <see cref="Mvc.ProblemDetails"/> | ||
/// payload to the current <see cref="HttpContext.Response"/>. | ||
/// </summary> | ||
public interface IProblemDetailsWriter | ||
{ | ||
/// <summary> | ||
/// Write a <see cref="Mvc.ProblemDetails"/> response to the current context | ||
/// </summary> | ||
/// <param name="context">The <see cref="ProblemDetailsContext"/> associated with the current request/response.</param> | ||
ValueTask WriteAsync(ProblemDetailsContext context); | ||
|
||
/// <summary> | ||
/// Determines whether this instance can write a <see cref="Mvc.ProblemDetails"/> to the current context. | ||
/// </summary> | ||
/// <param name="context">The <see cref="ProblemDetailsContext"/> associated with the current request/response.</param> | ||
/// <returns>Flag that indicates if that the writer can write to the current <see cref="ProblemDetailsContext"/>.</returns> | ||
bool CanWrite(ProblemDetailsContext context); | ||
} |
File renamed without changes.
34 changes: 34 additions & 0 deletions
34
src/Http/Http.Abstractions/src/ProblemDetails/ProblemDetailsContext.cs
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Microsoft.AspNetCore.Http; | ||
|
||
/// <summary> | ||
/// Represent the current problem details context for the request. | ||
/// </summary> | ||
public sealed class ProblemDetailsContext | ||
{ | ||
private ProblemDetails? _problemDetails; | ||
|
||
/// <summary> | ||
/// The <see cref="HttpContext"/> associated with the current request being processed by the filter. | ||
/// </summary> | ||
public required HttpContext HttpContext { get; init; } | ||
|
||
/// <summary> | ||
/// A collection of additional arbitrary metadata associated with the current request endpoint. | ||
/// </summary> | ||
public EndpointMetadataCollection? AdditionalMetadata { get; init; } | ||
|
||
/// <summary> | ||
/// An instance of <see cref="ProblemDetails"/> that will be | ||
/// used during the response payload generation. | ||
/// </summary> | ||
public ProblemDetails ProblemDetails | ||
{ | ||
get => _problemDetails ??= new ProblemDetails(); | ||
init => _problemDetails = value; | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
src/Http/Http.Extensions/src/DefaultProblemDetailsWriter.cs
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace Microsoft.AspNetCore.Http; | ||
|
||
internal sealed partial class DefaultProblemDetailsWriter : IProblemDetailsWriter | ||
{ | ||
private static readonly MediaTypeHeaderValue _jsonMediaType = new("application/json"); | ||
private static readonly MediaTypeHeaderValue _problemDetailsJsonMediaType = new("application/problem+json"); | ||
private readonly ProblemDetailsOptions _options; | ||
|
||
public DefaultProblemDetailsWriter(IOptions<ProblemDetailsOptions> options) | ||
{ | ||
_options = options.Value; | ||
} | ||
|
||
public bool CanWrite(ProblemDetailsContext context) | ||
{ | ||
var httpContext = context.HttpContext; | ||
var acceptHeader = httpContext.Request.Headers.Accept.GetList<MediaTypeHeaderValue>(); | ||
|
||
if (acceptHeader?.Any(h => _jsonMediaType.IsSubsetOf(h) || _problemDetailsJsonMediaType.IsSubsetOf(h)) == true) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
[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.")] | ||
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 }) | ||
{ | ||
// We can use the source generation in this case | ||
return new ValueTask(httpContext.Response.WriteAsJsonAsync( | ||
context.ProblemDetails, | ||
ProblemDetailsJsonContext.Default.ProblemDetails, | ||
contentType: "application/problem+json")); | ||
} | ||
|
||
return new ValueTask(httpContext.Response.WriteAsJsonAsync( | ||
context.ProblemDetails, | ||
options: null, | ||
contentType: "application/problem+json")); | ||
} | ||
|
||
[JsonSerializable(typeof(ProblemDetails))] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Http; | ||
|
||
/// <summary> | ||
/// Options for controlling the behavior of <see cref="IProblemDetailsService.WriteAsync(ProblemDetailsContext)"/> | ||
/// and similar methods. | ||
/// </summary> | ||
public class ProblemDetailsOptions | ||
{ | ||
/// <summary> | ||
/// The operation that customizes the current <see cref="Mvc.ProblemDetails"/> instance. | ||
/// </summary> | ||
public Action<ProblemDetailsContext>? CustomizeProblemDetails { get; set; } | ||
} |
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.
This lambda probably allocates per call, we should hand write
.Any