-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Enable ProblemDetailsService for TypedResults with ProblemDetails #64967
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,6 +173,163 @@ public void BadRequestObjectResult_Implements_IValueHttpResultOfT_Correctly() | |
| Assert.Equal(value, result.Value); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task BadRequestObjectResult_WithProblemDetails_UsesDefaultsFromProblemDetailsService() | ||
| { | ||
| // Arrange | ||
| var details = new ProblemDetails(); | ||
| var result = new BadRequest<ProblemDetails>(details); | ||
| var stream = new MemoryStream(); | ||
| var services = CreateServiceCollection() | ||
| .AddProblemDetails(options => options.CustomizeProblemDetails = context => context.ProblemDetails.Extensions["customProperty"] = "customValue") | ||
| .BuildServiceProvider(); | ||
| var httpContext = new DefaultHttpContext() | ||
| { | ||
| RequestServices = services, | ||
| Response = | ||
| { | ||
| Body = stream, | ||
| }, | ||
| }; | ||
|
|
||
| // Act | ||
| await result.ExecuteAsync(httpContext); | ||
|
|
||
| // Assert | ||
| Assert.Equal(StatusCodes.Status400BadRequest, httpContext.Response.StatusCode); | ||
| stream.Position = 0; | ||
| var responseDetails = System.Text.Json.JsonSerializer.Deserialize<ProblemDetails>(stream, new System.Text.Json.JsonSerializerOptions(System.Text.Json.JsonSerializerDefaults.Web)); | ||
| Assert.NotNull(responseDetails); | ||
| Assert.Equal(StatusCodes.Status400BadRequest, responseDetails.Status); | ||
| Assert.True(responseDetails.Extensions.ContainsKey("customProperty")); | ||
| Assert.Equal("customValue", responseDetails.Extensions["customProperty"]?.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task BadRequestObjectResult_WithProblemDetails_AppliesTraceIdFromService() | ||
| { | ||
| // Arrange | ||
| var details = new ProblemDetails(); | ||
| var result = new BadRequest<ProblemDetails>(details); | ||
| var stream = new MemoryStream(); | ||
| var services = CreateServiceCollection() | ||
| .AddProblemDetails() | ||
| .BuildServiceProvider(); | ||
| var httpContext = new DefaultHttpContext() | ||
| { | ||
| RequestServices = services, | ||
| Response = | ||
| { | ||
| Body = stream, | ||
| }, | ||
| }; | ||
|
|
||
| // Act | ||
| await result.ExecuteAsync(httpContext); | ||
|
|
||
| // Assert | ||
| Assert.Equal(StatusCodes.Status400BadRequest, httpContext.Response.StatusCode); | ||
| stream.Position = 0; | ||
| var responseDetails = System.Text.Json.JsonSerializer.Deserialize<ProblemDetails>(stream, new System.Text.Json.JsonSerializerOptions(System.Text.Json.JsonSerializerDefaults.Web)); | ||
| Assert.NotNull(responseDetails); | ||
| Assert.True(responseDetails.Extensions.ContainsKey("traceId")); | ||
| Assert.NotNull(responseDetails.Extensions["traceId"]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task BadRequestObjectResult_WithProblemDetails_FallsBackWhenServiceNotRegistered() | ||
| { | ||
| // Arrange | ||
| var details = new ProblemDetails { Title = "Test Error" }; | ||
| var result = new BadRequest<ProblemDetails>(details); | ||
| var stream = new MemoryStream(); | ||
| var httpContext = new DefaultHttpContext() | ||
| { | ||
| RequestServices = CreateServices(), | ||
| Response = | ||
| { | ||
| Body = stream, | ||
| }, | ||
| }; | ||
|
|
||
| // Act | ||
| await result.ExecuteAsync(httpContext); | ||
|
|
||
| // Assert | ||
| Assert.Equal(StatusCodes.Status400BadRequest, httpContext.Response.StatusCode); | ||
| stream.Position = 0; | ||
| var responseDetails = System.Text.Json.JsonSerializer.Deserialize<ProblemDetails>(stream, new System.Text.Json.JsonSerializerOptions(System.Text.Json.JsonSerializerDefaults.Web)); | ||
| Assert.NotNull(responseDetails); | ||
| Assert.Equal("Test Error", responseDetails.Title); | ||
| Assert.Equal(StatusCodes.Status400BadRequest, responseDetails.Status); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task BadRequestObjectResult_WithHttpValidationProblemDetails_UsesDefaultsFromProblemDetailsService() | ||
| { | ||
| // Arrange | ||
| var details = new HttpValidationProblemDetails(); | ||
| var result = new BadRequest<HttpValidationProblemDetails>(details); | ||
| var stream = new MemoryStream(); | ||
| var services = CreateServiceCollection() | ||
| .AddProblemDetails(options => options.CustomizeProblemDetails = context => context.ProblemDetails.Extensions["customValidation"] = "applied") | ||
| .BuildServiceProvider(); | ||
| var httpContext = new DefaultHttpContext() | ||
| { | ||
| RequestServices = services, | ||
| Response = | ||
| { | ||
| Body = stream, | ||
| }, | ||
| }; | ||
|
|
||
| // Act | ||
| await result.ExecuteAsync(httpContext); | ||
|
|
||
| // Assert | ||
| Assert.Equal(StatusCodes.Status400BadRequest, httpContext.Response.StatusCode); | ||
| stream.Position = 0; | ||
| var responseDetails = System.Text.Json.JsonSerializer.Deserialize<HttpValidationProblemDetails>(stream, new System.Text.Json.JsonSerializerOptions(System.Text.Json.JsonSerializerDefaults.Web)); | ||
| Assert.NotNull(responseDetails); | ||
| Assert.True(responseDetails.Extensions.ContainsKey("customValidation")); | ||
| Assert.Equal("applied", responseDetails.Extensions["customValidation"]?.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task BadRequestObjectResult_WithNonProblemDetails_DoesNotUseProblemDetailsService() | ||
| { | ||
| // Arrange | ||
| var details = new { error = "test error" }; | ||
| var result = new BadRequest<object>(details); | ||
| var stream = new MemoryStream(); | ||
| var customizationCalled = false; | ||
| var services = CreateServiceCollection() | ||
| .AddProblemDetails(options => options.CustomizeProblemDetails = context => customizationCalled = true) | ||
| .BuildServiceProvider(); | ||
| var httpContext = new DefaultHttpContext() | ||
| { | ||
| RequestServices = services, | ||
| Response = | ||
| { | ||
| Body = stream, | ||
| }, | ||
| }; | ||
|
|
||
| // Act | ||
| await result.ExecuteAsync(httpContext); | ||
|
|
||
| // Assert | ||
| Assert.False(customizationCalled, "CustomizeProblemDetails should not be called for non-ProblemDetails types"); | ||
| Assert.Equal(StatusCodes.Status400BadRequest, httpContext.Response.StatusCode); | ||
| } | ||
|
|
||
| private static ServiceCollection CreateServiceCollection() | ||
| { | ||
| var services = new ServiceCollection(); | ||
| services.AddSingleton<ILoggerFactory, NullLoggerFactory>(); | ||
| return services; | ||
| } | ||
|
|
||
|
Comment on lines
+176
to
+332
|
||
| private static void PopulateMetadata<TResult>(MethodInfo method, EndpointBuilder builder) | ||
| where TResult : IEndpointMetadataProvider => TResult.PopulateMetadata(method, builder); | ||
|
|
||
|
|
||
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.
The typical pattern we use here is something like:
See https://github.com/dotnet/aspnetcore/blob/main/src/Http/Http.Results/src/ProblemHttpResult.cs for an example.
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.
@copilot Please adopt the patterm that @captainsafia shows here.