2017-06-22 106 views
0

我的Web API請求模型屬性:揚鞭Swashbuckle抽象類文檔

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

可能的重複:[swagger繼承](https://stackoverflow.com/questions/27862407/swagger-inheritance-and-composition) – Troopers

+0

我使用這種方法 - https://stackoverflow.com/questions/34397349/how- DO-I-包括亞類功能於招搖-API的文檔,使用,swashbuckle –

回答

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;

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; 
     } 
    } 
} 
0

沒有什麼具體的請求模型.Possible選擇是編寫一個操作過濾

這裏是僞代碼

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

希望它有幫助