Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Set ProblemDetails status field during ObjectResult formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed Jul 19, 2018
1 parent 36d90c9 commit 6d458e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Microsoft.AspNetCore.Mvc.Core/ObjectResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public virtual void OnFormatting(ActionContext context)
if (StatusCode.HasValue)
{
context.HttpContext.Response.StatusCode = StatusCode.Value;

if (Value is ProblemDetails details && !details.Status.HasValue)
{
details.Status = StatusCode.Value;
}
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions test/Microsoft.AspNetCore.Mvc.Core.Test/ObjectResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,38 @@ public async Task ObjectResult_ExecuteResultAsync_SetsStatusCode()
Assert.Equal(404, actionContext.HttpContext.Response.StatusCode);
}

[Fact]
public async Task ObjectResult_ExecuteResultAsync_SetsProblemDetailsStatus()
{
// Arrange
var modelState = new ModelStateDictionary();

var details = new ValidationProblemDetails(modelState);

var result = new ObjectResult(details)
{
StatusCode = StatusCodes.Status422UnprocessableEntity,
Formatters = new FormatterCollection<IOutputFormatter>()
{
new NoOpOutputFormatter(),
},
};

var actionContext = new ActionContext()
{
HttpContext = new DefaultHttpContext()
{
RequestServices = CreateServices(),
}
};

// Act
await result.ExecuteResultAsync(actionContext);

// Assert
Assert.Equal(StatusCodes.Status422UnprocessableEntity, details.Status.Value);
}

private static IServiceProvider CreateServices()
{
var services = new ServiceCollection();
Expand Down

0 comments on commit 6d458e6

Please sign in to comment.