Add Problem(ProblemDetails) to allow configuring ProblemDetails.Extensions property

This commit is contained in:
Mariusz Stępień 2023-08-19 17:15:21 +02:00
Родитель 376b6b0c86
Коммит 77413ee6c5
3 изменённых файлов: 58 добавлений и 0 удалений

Просмотреть файл

@ -1850,6 +1850,24 @@ public abstract class ControllerBase
public virtual ConflictObjectResult Conflict([ActionResultObjectValue] ModelStateDictionary modelState) public virtual ConflictObjectResult Conflict([ActionResultObjectValue] ModelStateDictionary modelState)
=> new ConflictObjectResult(modelState); => new ConflictObjectResult(modelState);
/// <summary>
/// Creates an <see cref="ObjectResult"/> that produces a <see cref="ProblemDetails"/> response.
/// </summary>
/// <param name="problemDetails">The <see cref="ProblemDetails"/> object to produce a response from.</param>
/// <returns>The created <see cref="ObjectResult"/> for the response.</returns>
[NonAction]
public virtual ObjectResult Problem(ProblemDetails problemDetails)
{
ArgumentNullException.ThrowIfNull(problemDetails);
problemDetails.Status ??= 500;
return new ObjectResult(problemDetails)
{
StatusCode = problemDetails.Status
};
}
/// <summary> /// <summary>
/// Creates an <see cref="ObjectResult"/> that produces a <see cref="ProblemDetails"/> response. /// Creates an <see cref="ObjectResult"/> that produces a <see cref="ProblemDetails"/> response.
/// </summary> /// </summary>

Просмотреть файл

@ -29,4 +29,5 @@ virtual Microsoft.AspNetCore.Mvc.ControllerBase.Created() -> Microsoft.AspNetCor
*REMOVED*virtual Microsoft.AspNetCore.Mvc.ControllerBase.Created(System.Uri! uri, object? value) -> Microsoft.AspNetCore.Mvc.CreatedResult! *REMOVED*virtual Microsoft.AspNetCore.Mvc.ControllerBase.Created(System.Uri! uri, object? value) -> Microsoft.AspNetCore.Mvc.CreatedResult!
virtual Microsoft.AspNetCore.Mvc.ControllerBase.Created(string? uri, object? value) -> Microsoft.AspNetCore.Mvc.CreatedResult! virtual Microsoft.AspNetCore.Mvc.ControllerBase.Created(string? uri, object? value) -> Microsoft.AspNetCore.Mvc.CreatedResult!
virtual Microsoft.AspNetCore.Mvc.ControllerBase.Created(System.Uri? uri, object? value) -> Microsoft.AspNetCore.Mvc.CreatedResult! virtual Microsoft.AspNetCore.Mvc.ControllerBase.Created(System.Uri? uri, object? value) -> Microsoft.AspNetCore.Mvc.CreatedResult!
virtual Microsoft.AspNetCore.Mvc.ControllerBase.Problem(Microsoft.AspNetCore.Mvc.ProblemDetails! problemDetails) -> Microsoft.AspNetCore.Mvc.ObjectResult!
Microsoft.AspNetCore.Mvc.ProblemDetails.Extensions.set -> void (forwarded, contained in Microsoft.AspNetCore.Http.Abstractions) Microsoft.AspNetCore.Mvc.ProblemDetails.Extensions.set -> void (forwarded, contained in Microsoft.AspNetCore.Http.Abstractions)

Просмотреть файл

@ -2415,6 +2415,45 @@ public class ControllerBaseTest
Assert.Equal(400, problemDetails.Status); Assert.Equal(400, problemDetails.Status);
} }
[Fact]
public void ProblemDetails_Object_Works()
{
// Arrange
var problemDetails = new ProblemDetails();
var controller = new TestableController();
// Act
var actionResult = controller.Problem(problemDetails);
// Assert
var badRequestResult = Assert.IsType<ObjectResult>(actionResult);
Assert.Same(problemDetails, Assert.IsType<ProblemDetails>(badRequestResult.Value));
Assert.Equal(500, actionResult.StatusCode);
Assert.Equal(500, problemDetails.Status);
}
[Fact]
public void ProblemDetails_Object_UsesPassedInStatusCode()
{
// Arrange
var problemDetails = new ProblemDetails
{
Status = 503
};
var controller = new TestableController();
// Act
var actionResult = controller.Problem(problemDetails);
// Assert
var badRequestResult = Assert.IsType<ObjectResult>(actionResult);
Assert.Same(problemDetails, Assert.IsType<ProblemDetails>(badRequestResult.Value));
Assert.Equal(503, actionResult.StatusCode);
Assert.Equal(503, problemDetails.Status);
}
[Fact] [Fact]
public void ProblemDetails_Works() public void ProblemDetails_Works()
{ {