简体   繁体   English

描述REST API的动态响应类型

[英]Describing Dynamic Response Types For REST API

I use a standard APIResponse Object Interface for all of my REST APIs. 我为所有REST API使用标准的APIResponse对象接口。

public interface IAPIResponse
{
    IAPIError Error { get; set; }        
    IEnumerable<dynamic> Results { get; set; }
    void AddResult(string groupName, object data);

}

Here is how I use the standard APIResponse object. 这是我使用标准APIResponse对象的方式。

public async Task<IActionResult> Get()
{
        try
        {
           var response = new UsersGetResponse {
                   Users = await _someService.GetAll()};

            _apiResponse.AddResult("UsersGetResponse", response );

            return new ObjectResult(_apiResponse);
        }
        catch (Exception ex)
        {
            _apiResponse.Error = new APIError(ex.Message, 500);
            return new ObjectResult(_apiResponse);
        }            
}  

The call to _someSerice.GetAll() returns a list of the following User Objects. _someSerice.GetAll()的调用返回以下用户对象的列表。

public interface IUser
{

    string Id { get; set; }
    bool IsAdmin { get; }
    string Name { get; set; }
    string Email { get; set; }
}

Which is an attribute of the parent class UsersGetResponse 这是父类UsersGetResponse的属性

public class UsersGetResponse
{
     public IList<User> Users { get; set;}
}

This will produce the following JSON. 这将产生以下JSON。

{
"error": null,    
"results": [
{
  "UsersGetResponse": {

    "Users": [
      {
        "id": 171161,
        "isAdmin" : true,
        "name": "Dave Smith",
        "email": "Dave.Smith@gmail.com"
      },
      {
        "id": 171162,
        "isAdmin" : false,
        "name": "Jane Doe",
        "email": "Jane.Doe@gmail.com"            
      }
    ]
  }
}
]
}

If I use the ProducedResponseType annotation like so. 如果我像这样使用ProducedResponseType批注。

[ProducesResponseType(typeof(IAPIResponse), StatusCodes.Status200OK)]

I only get the following Example JSON in swagger. 我只能大张旗鼓地获得以下示例JSON。

{
  "error": {
    "message": "string",
    "number": 0
  },    
  "results": [
  {}
  ]
}

I'm trying to see if there is a way to properly communicate to the consumers of my API that I have a standard APIResponse Object with Dynamic results that have specific Objects like UsersGetResponse. 我正在尝试查看是否有一种方法可以正确地与我的API使用者进行通信,我有一个标准的APIResponse对象,并且具有带有特定结果(例如UsersGetResponse)的动态结果。

This is because you are using dynamic type. 这是因为您正在使用动态类型。 Swagger does not recognize any "properties" on that type. Swagger无法识别该类型的任何“属性”。 If you use a concrete type or an interface instead, swagger will show you the properties on that object. 如果您改用具体类型或接口,则swagger将为您显示该对象的属性。

Without fully understanding the scope of what you are trying to do, I will not be able to give you a complete solution. 如果不完全了解您要做什么的范围,我将无法为您提供完整的解决方案。 However, with the info that you have provided, this is what you should do: 但是,使用提供的信息,这是您应该做的:

public sealed class APIResponse<T>
{
    IAPIError Error { get; set; }        
    T Results { get; set; }
}

public async Task<IActionResult> Get()
{
    var response = new ApiResponse<<IEnumerable<User>>>();
    try
    {
        var data = await _someService.GetAll();
        response.Result = data;

        return Ok(response);//returns 200 status code
    }
    catch (Exception ex)
    {
        response.Error = new APIError(ex.Message, 500);
        return BadRequrest(response);
    }            
}  

You can then annotate like this: 然后,您可以像这样注释:

[ProducesResponseType(typeof(APIResponse<IEnumerable<User>>), StatusCodes.Status200OK)]

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM