Swagger Swashbuckle抽象类文档

时间:2017-06-22 17:11:06

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

我的Web API请求模型包含属性:

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

功能是一个抽象类。我将从中衍生出许多类:

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

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

请求模型没有特定的内容。可能的选项是编写操作过滤器

这里是伪代码

    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>();
         });

希望有所帮助