我的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
答案 0 :(得分:0)
看一下这个: http://swashbuckletest.azurewebsites.net/swagger/ui/index#!/InheritanceTest/InheritanceTest_Get
以下是该控制器背后的代码: https://github.com/heldersepu/SwashbuckleTest/blob/911bf68e0cf6af3ee5d8278e6dd988eda8c4dc8d/Swagger_Test/Controllers/InheritanceTestController.cs 使用System.Web.Http;
--files0-from
答案 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>();
});
希望有所帮助