2017-07-18 49 views
1

#216,Swashbuckle會自動生成一個200個成功響應作爲默認行爲,否則預計將指定的所有響應類型。自動生成默認的200 OK響應,同時指定其他響應類型

我希望能夠將四百分之四百零四錯誤響應添加到一些端點在XML註釋,同時保持所有端點,包括那些具有四百分之四百零四錯誤響應200個成功響應。

是否有可能使Swashbuckle繼續自動生成指定的錯誤響應,即使所有端點200個成功響應?

編輯:我添加了一個操作過濾器,以編程方式添加缺少的200個響應,它解決了缺少的模型模式,但仍然沒有真正回答我的原始問題:是否有辦法讓Swashbuckle的自動生成200次成功響應

class CustomOperationFilter : IOperationFilter 
{ 
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 
    { 
     var responses = operation.responses; 
     if (responses.ContainsKey("404") || responses.ContainsKey("400")) 
     { 
      responses.Add("200", new Response() 
      { 
       description = "OK", 
       schema = (schemaRegistry.GetOrRegister(apiDescription.ActionDescriptor.ReturnType)) 
      }); 
     } 
    } 
} 
+0

也許不無Swashbuckle本身noodling。對於它的價值,您的解決方案非常方便,您應該考慮將其作爲自我回答/解決方法。也許更一般地只檢查響應不包含密鑰「200」? –

+0

謝謝!只是改變了檢查,看看答案是否包含密鑰「200」。我必須添加一個額外的檢查以確保在添加200響應之前ReturnType不爲空以避免異常,因爲某些端點(如Delete)根本不返回任何內容 –

回答

1

這裏是添加缺少的200個成功響應到指定的錯誤響應端點解決方法:

class CustomOperationFilter : IOperationFilter 
{ 
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 
    { 
     var responses = operation.responses; 
     if (!responses.ContainsKey("200")) 
     { 
      if (apiDescription.ActionDescriptor.ReturnType != null) 
      { 
       responses.Add("200", new Response() 
       { 
        description = "OK", 
        schema = (schemaRegistry.GetOrRegister(apiDescription.ActionDescriptor.ReturnType)) 
       }); 
      } 
     } 
    } 
} 
相關問題