如何在ProducesResponseType Swagger上返回泛型类型?

时间:2017-10-18 18:50:49

标签: generics asp.net-core swagger swagger-ui swashbuckle

在下面的代码中,您会在 ProducesResponseType 上看到T类型(通用),但我无法使其有效,因为它不是特定类型:

 public class ApiController<T> : ApiBaseController where T : class, IDocument
  {    
    protected IDataService<T> data = null;

    [HttpGet("{id}")]
    **[ProducesResponseType(typeof(T), 201)]**
    [ProducesResponseType(typeof(void), 500)]         
    public async Task<IActionResult> Get(string id)
    {
        var result = await data.Get(id);

        return Ok(result);
    }
 }

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

仔细观察后,似乎可以(并且更容易)使用操作过滤器。

有些内容应该可行(未经测试,只是确保它不会产生编译错误)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Controllers;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace MyCompany.Common.Web.Swagger.OperationFilters
{
    public class GenericOperationFilter : IOperationFilter
    {
        public void Apply(Operation operation, OperationFilterContext context)
        {
            if (context.ApiDescription.ActionDescriptor is ControllerActionDescriptor controllerDescriptor)
            {
                var baseType = controllerDescriptor.ControllerTypeInfo.BaseType?.GetTypeInfo();
                // Get type and see if its a generic controller with a single type parameter
                if (baseType == null || (!baseType.IsGenericType && baseType.GenericTypeParameters.Length == 1))
                    return;

                if (context.ApiDescription.HttpMethod == "GET" && !operation.Responses.ContainsKey("200"))
                {
                    var typeParam = baseType.GenericTypeParameters[0];

                    // Get the schema of the generic type. In case it's not there, you will have to create a schema for that model
                    // yourself, because Swagger may not have added it, because the type was not declared on any of the models
                    string typeParamFriendlyId = typeParam.FriendlyId();

                    if (!context.SchemaRegistry.Definitions.TryGetValue(typeParamFriendlyId, out Schema typeParamSchema))
                    {
                        // Schema doesn't exist, you need to create it yourself, i.e. add properties for each property of your model.
                        // See OpenAPI/Swagger Specifications
                        typeParamSchema = context.SchemaRegistry.GetOrRegister(typeParam);

                        // add properties here, without it you won't have a model description for this type
                    }

                    // for any get operation for which no 200 response exist yet in the document
                    operation.Responses.Add("200", new Response
                    {
                        Description = "Success",
                        Schema = new Schema { Ref = typeParamFriendlyId }
                    });
                }
            }
        }
    }
}

它做什么?为每个操作(Get,Post,Put等)调用IOperationFilter。在其中,您检查它是否为ControllerActionDescriptor,如果是,请检查控制器类型。

如果您愿意,可以将其缩小到一种特定类型。我刚刚接受了从另一个类继承的每个控制器,它的基类型是通用的一个泛型参数。

然后,它最后检查它是否为“Get”操作(post,put,delete通常不返回模型,只返回状态代码/错误响应),然后检查该类型是否已经在Swagger / OpenAPI模式中定义。如果模型在那里,请读取它的模式并在响应中引用它。

如果模型未在架构注册表中注册,则会变得更复杂。您需要使用反射并构建模式文件,将其添加到存储库(已在context.SchemaRegistry.GetOrRegister(typeParam)调用期间发生),然后像上面那样引用它。

当模型未在任何其他控制器中用作响应或操作参数时,可能会发生这种情况。

您可以获得有关OpenAPI 2.0规范的更多信息。