[英]ASP.net Core API returning ActionResult<T> not enforcing return type
Where an API is returning an ActionResult<T>
there is no compile or run-time checks to ensure that the data type being returned is as declared.如果 API 返回
ActionResult<T>
,则不会进行编译或运行时检查以确保返回的数据类型与声明的相同。
For example:例如:
public ActionResult<Dog> GetCat()
{
var cat = new Cat("Moggy");
return Ok(cat);
}
This compiles, runs and returns a Cat object.这将编译、运行并返回 Cat object。
The problem I have is that the OpenAPI defintion (Swagger/Swashbuckle) is being derived from the type declared on the method.我遇到的问题是 OpenAPI 定义(Swagger/Swashbuckle)是从方法上声明的类型派生的。 This means the OpenAPI defintion can be out of sync with the code and there is no obvious way to find these instances.
这意味着 OpenAPI 定义可能与代码不同步,并且没有明显的方法可以找到这些实例。
In the example given, my API documentation would tell people that this API returns a Dog when really it returns a Cat.在给出的示例中,我的 API 文档会告诉人们,这个 API 返回的是一只狗,而实际上它返回的是一只猫。
Is there any way to enforce the return type at compile time, or failing that at run-time?有没有办法在编译时强制执行返回类型,或者在运行时失败?
That is because your returned type OkObjectResult
inherits the ActionResult
.那是因为您返回的类型
OkObjectResult
继承了ActionResult
。 The returned child class does not impact the Parent class compilation.This is by design.返回的子 class 不会影响父 class 编译。这是设计使然。
More details about the two class you could refer to the following source code:有关这两个 class 的更多详细信息,您可以参考以下源代码:
1.The Ok
method returns type of OkObjectResult
: 1.
Ok
方法返回OkObjectResult
的类型:
// Summary:
// A base class for an MVC controller without view support.
[Controller]
public abstract class ControllerBase
{
// Summary:
// Creates an Microsoft.AspNetCore.Mvc.OkObjectResult object that produces an Microsoft.AspNetCore.Http.StatusCodes.Status200OK
// response.
//
// Parameters:
// value:
// The content value to format in the entity body.
//
// Returns:
// The created Microsoft.AspNetCore.Mvc.OkObjectResult for the response.
[NonAction]
public virtual OkObjectResult Ok([ActionResultObjectValueAttribute] object value);
2.The OkObjectResult
inherits ObjectResult
: 2.
OkObjectResult
继承ObjectResult
:
//
// Summary:
// An Microsoft.AspNetCore.Mvc.ObjectResult that when executed performs content
// negotiation, formats the entity body, and will produce a Microsoft.AspNetCore.Http.StatusCodes.Status200OK
// response if negotiation and formatting succeed.
[DefaultStatusCode(200)]
public class OkObjectResult : ObjectResult
{
//
// Summary:
// Initializes a new instance of the Microsoft.AspNetCore.Mvc.OkObjectResult class.
//
// Parameters:
// value:
// The content to format into the entity body.
public OkObjectResult(object value);
}
3.The ObjectResult
inherits ActionResult
: ObjectResult
继承ActionResult
:
public class ObjectResult : ActionResult, IStatusCodeActionResult, IActionResult
{
public ObjectResult(object value);
//..
}
Is there any way to enforce the return type at compile time, or failing that at run-time?
有没有办法在编译时强制执行返回类型,或者在运行时失败?
ActionResult<T>
enables you to return a type deriving from ActionResult
or return a specific type. ActionResult<T>
使您能够返回派生自ActionResult
的类型或返回特定类型。
To enforce compile error,you could just return cat which is a specific type.And you could see the error message disappear when you change Dog
to Cat
:要强制编译错误,您可以只返回特定类型的 cat。当您将
Dog
更改为Cat
时,您会看到错误消息消失:
public ActionResult<Dog> GetCat()
{
var cat = new Cat("Moggy");
return cat;
}
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.