Skip to content

Commit bd4d965

Browse files
authored
Set StatusCode to 200 for ActionResult<T> that wraps an object value (#28267)
Summary of the changes - For `ActionResult<T>` that wraps an object value, set status code to 200 when converting to `IActionResult` - Special handling for `ProblemDetails` (honors `StatusCode` in `ProblemDetails` if it's set) Addresses #27165
1 parent d2e85be commit bd4d965

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/Mvc/Mvc.Core/src/ActionResultOfT.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using Microsoft.AspNetCore.Http;
56
using Microsoft.AspNetCore.Mvc.Core;
67
using Microsoft.AspNetCore.Mvc.Infrastructure;
78

@@ -13,6 +14,8 @@ namespace Microsoft.AspNetCore.Mvc
1314
/// <typeparam name="TValue">The type of the result.</typeparam>
1415
public sealed class ActionResult<TValue> : IConvertToActionResult
1516
{
17+
private const int DefaultStatusCode = StatusCodes.Status200OK;
18+
1619
/// <summary>
1720
/// Initializes a new instance of <see cref="ActionResult{TValue}"/> using the specified <paramref name="value"/>.
1821
/// </summary>
@@ -73,9 +76,25 @@ public static implicit operator ActionResult<TValue>(ActionResult result)
7376

7477
IActionResult IConvertToActionResult.Convert()
7578
{
76-
return Result ?? new ObjectResult(Value)
79+
if (Result != null)
80+
{
81+
return Result;
82+
}
83+
84+
int statusCode;
85+
if (Value is ProblemDetails problemDetails && problemDetails.Status != null)
86+
{
87+
statusCode = problemDetails.Status.Value;
88+
}
89+
else
90+
{
91+
statusCode = DefaultStatusCode;
92+
}
93+
94+
return new ObjectResult(Value)
7795
{
7896
DeclaredType = typeof(TValue),
97+
StatusCode = statusCode
7998
};
8099
}
81100
}

src/Mvc/Mvc.Core/test/ActionResultOfTTest.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
55
using System.IO;
6+
using Microsoft.AspNetCore.Http;
67
using Microsoft.AspNetCore.Mvc.Infrastructure;
78
using Xunit;
89

@@ -62,6 +63,25 @@ public void Convert_ReturnsObjectResultWrappingValue()
6263
var objectResult = Assert.IsType<ObjectResult>(result);
6364
Assert.Same(value, objectResult.Value);
6465
Assert.Equal(typeof(BaseItem), objectResult.DeclaredType);
66+
Assert.Equal(StatusCodes.Status200OK, objectResult.StatusCode);
67+
}
68+
69+
[Fact]
70+
public void Convert_ReturnsObjectResultWrappingValue_SetsStatusCodeFromProblemDetails()
71+
{
72+
// Arrange
73+
var value = new ProblemDetails { Status = StatusCodes.Status400BadRequest };
74+
var actionResultOfT = new ActionResult<ProblemDetails>(value);
75+
var convertToActionResult = (IConvertToActionResult)actionResultOfT;
76+
77+
// Act
78+
var result = convertToActionResult.Convert();
79+
80+
// Assert
81+
var objectResult = Assert.IsType<ObjectResult>(result);
82+
Assert.Same(value, objectResult.Value);
83+
Assert.Equal(typeof(ProblemDetails), objectResult.DeclaredType);
84+
Assert.Equal(StatusCodes.Status400BadRequest, objectResult.StatusCode);
6585
}
6686

6787
[Fact]
@@ -79,6 +99,7 @@ public void Convert_InfersDeclaredTypeFromActionResultTypeParameter()
7999
var objectResult = Assert.IsType<ObjectResult>(result);
80100
Assert.Same(value, objectResult.Value);
81101
Assert.Equal(typeof(BaseItem), objectResult.DeclaredType);
102+
Assert.Equal(StatusCodes.Status200OK, objectResult.StatusCode);
82103
}
83104

84105
private class BaseItem

0 commit comments

Comments
 (0)