Skip to content

Commit d0de736

Browse files
huysentruitwpranavkm
authored andcommitted
ControllerBase.Problem should also set the StatusCode of the response (#14672)
* ControllerBase.Problem should also set the StatusCode of the response Fixes #14663
1 parent c3ccf5b commit d0de736

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/Mvc/Mvc.Core/src/ControllerBase.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,7 +1870,10 @@ public virtual ObjectResult Problem(
18701870
detail: detail,
18711871
instance: instance);
18721872

1873-
return new ObjectResult(problemDetails);
1873+
return new ObjectResult(problemDetails)
1874+
{
1875+
StatusCode = problemDetails.Status
1876+
};
18741877
}
18751878

18761879
/// <summary>
@@ -1946,7 +1949,10 @@ public virtual ActionResult ValidationProblem(
19461949
return new BadRequestObjectResult(validationProblem);
19471950
}
19481951

1949-
return new ObjectResult(validationProblem);
1952+
return new ObjectResult(validationProblem)
1953+
{
1954+
StatusCode = validationProblem.Status
1955+
};
19501956
}
19511957

19521958
/// <summary>

src/Mvc/Mvc.Core/test/ControllerBaseTest.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,7 @@ public void ValidationProblemDetails_Works()
23162316
// Assert
23172317
var badRequestResult = Assert.IsType<BadRequestObjectResult>(actionResult);
23182318
var problemDetails = Assert.IsType<ValidationProblemDetails>(badRequestResult.Value);
2319+
Assert.Equal(400, badRequestResult.StatusCode);
23192320
Assert.Equal(400, problemDetails.Status);
23202321
Assert.Equal("One or more validation errors occurred.", problemDetails.Title);
23212322
Assert.Equal("https://tools.ietf.org/html/rfc7231#section-6.5.1", problemDetails.Type);
@@ -2348,6 +2349,48 @@ public void ValidationProblemDetails_UsesSpecifiedTitle()
23482349
Assert.Equal(detail, problemDetails.Detail);
23492350
}
23502351

2352+
[Fact]
2353+
public void ValidationProblemDetails_UsesSpecifiedStatusCode()
2354+
{
2355+
// Arrange
2356+
var options = GetApiBehaviorOptions();
2357+
2358+
var controller = new TestableController
2359+
{
2360+
ProblemDetailsFactory = new DefaultProblemDetailsFactory(Options.Create(options)),
2361+
};
2362+
2363+
// Act
2364+
var actionResult = controller.ValidationProblem(statusCode: 405);
2365+
2366+
// Assert
2367+
var objectResult = Assert.IsType<ObjectResult>(actionResult);
2368+
var problemDetails = Assert.IsType<ValidationProblemDetails>(objectResult.Value);
2369+
Assert.Equal(405, objectResult.StatusCode);
2370+
Assert.Equal(405, problemDetails.Status);
2371+
}
2372+
2373+
[Fact]
2374+
public void ValidationProblemDetails_StatusCode400_ReturnsBadRequestObjectResultFor2xCompatibility()
2375+
{
2376+
// Arrange
2377+
var options = GetApiBehaviorOptions();
2378+
2379+
var controller = new TestableController
2380+
{
2381+
ProblemDetailsFactory = new DefaultProblemDetailsFactory(Options.Create(options)),
2382+
};
2383+
2384+
// Act
2385+
var actionResult = controller.ValidationProblem(statusCode: 400);
2386+
2387+
// Assert
2388+
var badRequestResult = Assert.IsType<BadRequestObjectResult>(actionResult);
2389+
var problemDetails = Assert.IsType<ValidationProblemDetails>(badRequestResult.Value);
2390+
Assert.Equal(400, badRequestResult.StatusCode);
2391+
Assert.Equal(400, problemDetails.Status);
2392+
}
2393+
23512394
[Fact]
23522395
public void ProblemDetails_Works()
23532396
{
@@ -2371,6 +2414,7 @@ public void ProblemDetails_Works()
23712414
// Assert
23722415
var badRequestResult = Assert.IsType<ObjectResult>(actionResult);
23732416
var problemDetails = Assert.IsType<ProblemDetails>(badRequestResult.Value);
2417+
Assert.Equal(500, actionResult.StatusCode);
23742418
Assert.Equal(500, problemDetails.Status);
23752419
Assert.Equal("An error occured while processing your request.", problemDetails.Title);
23762420
Assert.Equal("https://tools.ietf.org/html/rfc7231#section-6.6.1", problemDetails.Type);
@@ -2396,6 +2440,7 @@ public void ProblemDetails_UsesPassedInValues()
23962440
// Assert
23972441
var badRequestResult = Assert.IsType<ObjectResult>(actionResult);
23982442
var problemDetails = Assert.IsType<ProblemDetails>(badRequestResult.Value);
2443+
Assert.Equal(500, actionResult.StatusCode);
23992444
Assert.Equal(500, problemDetails.Status);
24002445
Assert.Equal(title, problemDetails.Title);
24012446
Assert.Equal("https://tools.ietf.org/html/rfc7231#section-6.6.1", problemDetails.Type);
@@ -2419,6 +2464,7 @@ public void ProblemDetails_UsesPassedInStatusCode()
24192464
// Assert
24202465
var badRequestResult = Assert.IsType<ObjectResult>(actionResult);
24212466
var problemDetails = Assert.IsType<ProblemDetails>(badRequestResult.Value);
2467+
Assert.Equal(422, actionResult.StatusCode);
24222468
Assert.Equal(422, problemDetails.Status);
24232469
Assert.Equal("Unprocessable entity.", problemDetails.Title);
24242470
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", problemDetails.Type);

0 commit comments

Comments
 (0)