2015-04-17 103 views
33

我有一個C#ASP.NET WebAPI應用程序,其API文檔使用Swashbuckle自動生成。我希望能夠從文檔中省略某些方法,但我似乎無法弄清楚如何告訴Swagger不要將它們包含在Swagger UI輸出中。如何使用Swashbuckle忽略WebAPI上的Swagger文檔的方法

我覺得這是添加模型或架構過濾器,但它不是很明顯該做什麼,而且文檔似乎只提供瞭如何修改方法的輸出的示例,而不是將其刪除完全來自輸出。

在此先感謝。

+2

誰降低了這個問題,爲什麼?你能否有禮貌來解釋推理。 –

回答

10

它與文件過濾器生成後,您可以從招搖文檔中刪除「操作」 - 剛剛成立的動詞null(雖然可能還有其他的方式來做到這一點爲好)

下面的示例允許只有GET動詞 - 並取自this issue

class RemoveVerbsFilter : IDocumentFilter 
{ 
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer) 
    { 
     foreach (PathItem path in swaggerDoc.paths.Values) 
     { 
      path.delete = null; 
      //path.get = null; // leaving GET in 
      path.head = null; 
      path.options = null; 
      path.patch = null; 
      path.post = null; 
      path.put = null; 
     } 
    } 
} 

,並在你的招搖配置:

...EnableSwagger(conf => 
{ 
    // ... 

    conf.DocumentFilter<RemoveVerbsFilter>(); 
}); 
+0

注意:即使取消註釋'path.get = null;' - 這樣也不會刪除路徑,因此這些路徑仍將包含在Swagger文件中,但僅包含詳細信息。在您的答案中包含'ApiExplorerSettingsAttribute'可能會更好,正如您在GitHub上的原始答覆中所述。 使用ApiExplorerSettings可能也會避免將類型信息添加到Swagger文件的'schemes'列表中。 – JBert

+0

非常感謝分享這個。它是一個很好的例子。 – user2768132

0

我寧願消除對完全路徑項目的字典entires:

var pathsToRemove = swaggerDoc.Paths 
       .Where(pathItem => !pathItem.Key.Contains("api/")) 
       .ToList(); 

foreach (var item in pathsToRemove) 
{ 
    swaggerDoc.Paths.Remove(item.Key); 
} 

通過這種方法,你不會得到「空「生成的swagger.json定義中的項目。

44

您可以將以下屬性添加到控制器和行動,從生成的文檔排除它們:[ApiExplorerSettings(IgnoreApi = true)]

+1

工程就像一個魅力! :) – msk

+1

工作很好,這應該是答案 – JohnC

+0

有沒有辦法做到這一點編程?根據配置設置,我想在某些環境中公開API,但不在其他環境中公開API。 –

6

有人張貼在GitHub上的解決方案,所以我要把它貼在這裏。所有學分都歸他所有。 https://github.com/domaindrivendev/Swashbuckle/issues/153#issuecomment-213342771

創建第一屬性類

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] 
    public class HideInDocsAttribute:Attribute 
    { 
    } 

然後創建一個文檔Filter類

public class HideInDocsFilter:IDocumentFilter 
    { 
     public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer) 
     { 
      foreach (var apiDescription in apiExplorer.ApiDescriptions) 
      { 
       if (!apiDescription.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<HideInDocsAttribute>().Any() && !apiDescription.ActionDescriptor.GetCustomAttributes<HideInDocsAttribute>().Any()) continue; 
       var route = "/" + apiDescription.Route.RouteTemplate.TrimEnd('/'); 
       swaggerDoc.paths.Remove(route); 
      } 
     } 
    } 

然後在揚鞭配置類,添加文件濾波器

public class SwaggerConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      var thisAssembly = typeof(SwaggerConfig).Assembly; 

      config 
       .EnableSwagger(c => 
        { 
         ...      
         c.DocumentFilter<HideInDocsFilter>(); 
         ... 
        }) 
       .EnableSwaggerUi(c => 
        { 
         ... 
        }); 
     } 
    } 

最後一步是在控制器或方法y上添加[HideInDocsAttribute]屬性你不希望Swashbuckle生成文檔。

+0

我認爲RemoveRoute可能是我正在尋找的機器人。 –

相關問題