2015-04-17 50 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可能是我正在寻找的机器人。 –

相关问题