Throw if the type parameter for ActionResult<T> is an action result (#7942)
Fixes #7931
This commit is contained in:
parent
2599e0f5cc
commit
17d2545b55
|
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Mvc.Core;
|
||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc
|
||||
|
|
@ -18,6 +19,12 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
/// <param name="value">The value.</param>
|
||||
public ActionResult(TValue value)
|
||||
{
|
||||
if (typeof(IActionResult).IsAssignableFrom(typeof(TValue)))
|
||||
{
|
||||
var error = Resources.FormatInvalidTypeTForActionResultOfT(typeof(TValue), "ActionResult<T>");
|
||||
throw new ArgumentException(error);
|
||||
}
|
||||
|
||||
Value = value;
|
||||
}
|
||||
|
||||
|
|
@ -27,6 +34,12 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
/// <param name="result">The <see cref="ActionResult"/>.</param>
|
||||
public ActionResult(ActionResult result)
|
||||
{
|
||||
if (typeof(IActionResult).IsAssignableFrom(typeof(TValue)))
|
||||
{
|
||||
var error = Resources.FormatInvalidTypeTForActionResultOfT(typeof(TValue), "ActionResult<T>");
|
||||
throw new ArgumentException(error);
|
||||
}
|
||||
|
||||
Result = result ?? throw new ArgumentNullException(nameof(result));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1480,6 +1480,20 @@ namespace Microsoft.AspNetCore.Mvc.Core
|
|||
internal static string FormatApiConventionMustBeStatic(object p0)
|
||||
=> string.Format(CultureInfo.CurrentCulture, GetString("ApiConventionMustBeStatic"), p0);
|
||||
|
||||
/// <summary>
|
||||
/// Invalid type parameter '{0}' specified for '{1}'.
|
||||
/// </summary>
|
||||
internal static string InvalidTypeTForActionResultOfT
|
||||
{
|
||||
get => GetString("InvalidTypeTForActionResultOfT");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invalid type parameter '{0}' specified for '{1}'.
|
||||
/// </summary>
|
||||
internal static string FormatInvalidTypeTForActionResultOfT(object p0, object p1)
|
||||
=> string.Format(CultureInfo.CurrentCulture, GetString("InvalidTypeTForActionResultOfT"), p0, p1);
|
||||
|
||||
private static string GetString(string name, params string[] formatterNames)
|
||||
{
|
||||
var value = _resourceManager.GetString(name);
|
||||
|
|
|
|||
|
|
@ -445,4 +445,7 @@
|
|||
<data name="ApiConventionMustBeStatic" xml:space="preserve">
|
||||
<value>API convention type '{0}' must be a static type.</value>
|
||||
</data>
|
||||
<data name="InvalidTypeTForActionResultOfT" xml:space="preserve">
|
||||
<value>Invalid type parameter '{0}' specified for '{1}'.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||
using Xunit;
|
||||
|
||||
|
|
@ -8,6 +10,28 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
{
|
||||
public class ActionResultOfTTest
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WithValue_ThrowsForInvalidType()
|
||||
{
|
||||
// Arrange
|
||||
var input = new FileStreamResult(Stream.Null, "application/json");
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<ArgumentException>(() => new ActionResult<FileStreamResult>(value: input));
|
||||
Assert.Equal($"Invalid type parameter '{typeof(FileStreamResult)}' specified for 'ActionResult<T>'.", ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithActionResult_ThrowsForInvalidType()
|
||||
{
|
||||
// Arrange
|
||||
var actionResult = new OkResult();
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<ArgumentException>(() => new ActionResult<FileStreamResult>(result: actionResult));
|
||||
Assert.Equal($"Invalid type parameter '{typeof(FileStreamResult)}' specified for 'ActionResult<T>'.", ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Convert_ReturnsResultIfSet()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue