Skip to content

Commit

Permalink
Use ProblemDetailService in ValidationProblem
Browse files Browse the repository at this point in the history
  • Loading branch information
dnperfors committed Oct 18, 2023
1 parent d765d7b commit 9fd6ccc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/Http/Http.Results/src/ValidationProblem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,25 @@ internal ValidationProblem(HttpValidationProblemDetails problemDetails)
int? IStatusCodeHttpResult.StatusCode => StatusCode;

/// <inheritdoc/>
public Task ExecuteAsync(HttpContext httpContext)
public async Task ExecuteAsync(HttpContext httpContext)
{
ArgumentNullException.ThrowIfNull(httpContext);

var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger(typeof(ValidationProblem));
var problemDetailsService = httpContext.RequestServices.GetService<IProblemDetailsService>();

HttpResultsHelper.Log.WritingResultAsStatusCode(logger, StatusCode);
httpContext.Response.StatusCode = StatusCode;

return HttpResultsHelper.WriteResultAsJsonAsync(
if (problemDetailsService is null || !await problemDetailsService.TryWriteAsync(new() { HttpContext = httpContext, ProblemDetails = ProblemDetails }))
{
await HttpResultsHelper.WriteResultAsJsonAsync(
httpContext,
logger,
value: ProblemDetails,
ContentType);
}
}

/// <inheritdoc/>
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Http.Results/test/ProblemResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task ExecuteAsync_UsesDefaults_ForProblemDetails()
}

[Fact]
public async Task ExecuteAsync_UsesDefaultsFromProblemDetailsServoce_ForProblemDetails()
public async Task ExecuteAsync_UsesDefaultsFromProblemDetailsService_ForProblemDetails()
{
// Arrange
var details = new ProblemDetails();
Expand Down Expand Up @@ -232,7 +232,7 @@ public void ProblemResult_Implements_IValueHttpResult_Correctly()
[Fact]
public void ProblemResult_Implements_IValueHttpResultOfT_Correctly()
{
// Arrange
// Arrange
var value = new ProblemDetails();

// Act & Assert
Expand Down
41 changes: 40 additions & 1 deletion src/Http/Http.Results/test/ValidationProblemResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,38 @@ public async Task ExecuteAsync_UsesDefaults_ForProblemDetails()
Assert.Equal(StatusCodes.Status400BadRequest, responseDetails.Status);
}

[Fact]
public async Task ExecuteAsync_UsesDefaultsFromProblemDetailsService_ForProblemDetails()
{
// Arrange
var details = new HttpValidationProblemDetails();

var result = new ValidationProblem(details);
var stream = new MemoryStream();
var services = CreateServiceCollection()
.AddProblemDetails(options => options.CustomizeProblemDetails = x => x.ProblemDetails.Type = null)
.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 = JsonSerializer.Deserialize<ProblemDetails>(stream, SerializerOptions);
Assert.Null(responseDetails.Type);
Assert.Equal("One or more validation errors occurred.", responseDetails.Title);
Assert.Equal(StatusCodes.Status400BadRequest, responseDetails.Status);
}

[Fact]
public void ExecuteAsync_ThrowsArgumentNullException_ForNullProblemDetails()
{
Expand Down Expand Up @@ -141,11 +173,18 @@ public void ValidationProblemResult_Implements_IContentTypeHttpResult_Correctly(
private static void PopulateMetadata<TResult>(MethodInfo method, EndpointBuilder builder)
where TResult : IEndpointMetadataProvider => TResult.PopulateMetadata(method, builder);

private static IServiceProvider CreateServices()

private static IServiceCollection CreateServiceCollection()
{
var services = new ServiceCollection();
services.AddTransient(typeof(ILogger<>), typeof(NullLogger<>));
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
return services;
}

private static IServiceProvider CreateServices()
{
var services = CreateServiceCollection();

return services.BuildServiceProvider();
}
Expand Down

0 comments on commit 9fd6ccc

Please sign in to comment.