【问题标题】:Swagger Swashbuckle abstract class documentationSwagger Swashbuckle 抽象类文档
【发布时间】:2017-06-22 17:11:06
【问题描述】:

我有带有属性的 Web API 请求模型:

public List<Feature> Features { get; set; }

Feature 是一个抽象类。我会从它派生出很多类:

public abstract class Feature
{
    public string Title { get; set; }
}

public class ImageFeature : Feature
{
    public string ImageUrl { get; set; }
}

显然 Swashbuckle 仅识别 Feature 属性并相应地生成文档。如何明确声明 Feature 类的可能实现,以便 Swashbuckle 生成正确的文档?是否有一些我可以使用的属性,例如:

[SwaggerResponseType(typeof(ImageFeature))]
[SwaggerResponseType(typeof(AnotherFeature))]
public abstract class Feature

【问题讨论】:

标签: c# asp.net-web-api asp.net-core swagger swashbuckle


【解决方案1】:

看看这个: http://swashbuckletest.azurewebsites.net/swagger/ui/index#!/InheritanceTest/InheritanceTest_Get

这是该控制器背后的代码: https://github.com/heldersepu/SwashbuckleTest/blob/911bf68e0cf6af3ee5d8278e6dd988eda8c4dc8d/Swagger_Test/Controllers/InheritanceTestController.cs

using System.Web.Http;

namespace Swagger_Test.Controllers
{
    public abstract class Feature
    {
        /// <summary>We all need a Title</summary>
        public string Title { get; set; }
    }

    public class ImageFeature : Feature
    {
        /// <summary>Here goes your image URL</summary>
        public string ImageUrl { get; set; }
    }

    public class InheritanceTestController : ApiController
    {
        public ImageFeature Get([FromUri]ImageFeature imageFeature)
        {
            return imageFeature;
        }

        public ImageFeature Post(ImageFeature imageFeature)
        {
            return imageFeature;
        }
    }
}

【讨论】:

    【解决方案2】:

    请求模型没有什么特别的。可能的选择是编写一个操作过滤器

    这是伪代码

        public class RequestModelExtentionOperator: IOperationFilter    
            {                 
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            if (operation.operationId == "Controller_ActionName")  // controller and action name
            {
               var refSchema = schemaRegistry.GetOrRegister(typeof(List<ImageFeature>));
                    //here you can create a new Parameter of type Array
    var param=new Parameter 
                        {
                            name = "Features",
                            @in = "formData",
                            required = true,
                            type = "array"
                        };
                param.PopulateFrom(schema);
    operation.parameters = new[]{ param };
            }
        }            
                }
        }
    

    然后我们可以设置 OperationFilter

    httpConfiguration
         .EnableSwagger(c => c.SingleApiVersion("v1", "A title for your API"))
             {
                 c.OperationFilter<RequestModelExtentionOperator>();
             });
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多