为不同API组生成多个swagger JSON文件
我在解决方案中配置了swagger,它正确地显示了API文档。最近,我在同一个解决方案中开发了一些新的API,这些API也出现在API文档中,这些项目也遵循相同的命名约定。 现在我的要求是我想把新的API文档与旧的分离开来,所以基本上我希望生成两个JSON文件,一个是旧的API,一个是新的API 我的招摇过市配置如下所示为不同API组生成多个swagger JSON文件,swagger,swashbuckle,Swagger,Swashbuckle,我在解决方案中配置了swagger,它正确地显示了API文档。最近,我在同一个解决方案中开发了一些新的API,这些API也出现在API文档中,这些项目也遵循相同的命名约定。 现在我的要求是我想把新的API文档与旧的分离开来,所以基本上我希望生成两个JSON文件,一个是旧的API,一个是新的API 我的招摇过市配置如下所示 Config.EnableSwagger(@"api-docs/{apiVersion}", c =>
Config.EnableSwagger(@"api-docs/{apiVersion}",
c =>
{
c.SingleApiVersion("v1", "SAMPLE API");
c.UseFullTypeNameInSchemaIds();
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
foreach (String x in Directory.GetFiles(Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path)), "*.dll")
.Where(f => Path.GetFileNameWithoutExtension(f).ToLower().Contains("sample") ****&& !Path.GetFileNameWithoutExtension(f).ToLower().Contains("sample.api"))****
.Select(f => String.Format(@"{0}\{1}.xml", Path.GetDirectoryName(f), Path.GetFileNameWithoutExtension(f)))
.Where(File.Exists))
{
c.IncludeXmlComments(x);
}
c.OperationFilter<AddRequiredHeaderParameter>();
});
Config.EnableSwagger(@“api文档/{apiVersion}),
c=>
{
c、 单一API版本(“v1”,“示例API”);
c、 UseFullTypeNameInSchemaIds();
c、 ResolveConflictingActions(apisdescriptions=>apisdescriptions.First());
foreach(Directory.GetFiles(Path.GetDirectoryName(Uri.UnescapeDataString(new-UriBuilder(Assembly.GetExecutingAssembly().CodeBase.Path))中的字符串x),“*.dll”)
.Where(f=>Path.GetFileNameWithoutExtension(f).ToLower()包含(“sample”)***&!Path.GetFileNameWithoutExtension(f).ToLower()包含(“sample.api”))****
.Select(f=>String.Format(@“{0}\{1}.xml”、Path.GetDirectoryName(f)、Path.GetFileNameWithoutExtension(f)))
.Where(File.Exists))
{
c、 includexml注释(x);
}
c、 操作过滤器();
});
我的新API项目名为sample.API.test,旧API项目名为sample.web.test。
我在where子句中添加了&&部分,以忽略在第一次生成JSON文档时拾取新文件,但运气不佳。我是新手,真的不知道根据项目名称是否可能有两个JSON。非常感谢您的帮助。我使用IDocumentFilter修复了此问题,如下所示
internal class SwaggerFilterOutControllers : IDocumentFilter
{
void IDocumentFilter.Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
var apiKeys = swaggerDoc.paths.Select(s => s.Key).ToList();
foreach (var apiKey in apiKeys)
{
if (swaggerDoc.info.title == "old API" && apiKey.StartsWith("/latapi"))
swaggerDoc.paths.Remove(apiKey);
else if (swaggerDoc.info.title == "New Public API" && !apiKey.StartsWith("/latapi"))
swaggerDoc.paths.Remove(apiKey);
}
}
}
然后在enableswagger()方法中,我将此过滤器称为
c.DocumentFilter<SwaggerFilterOutControllers>();
c.DocumentFilter();
您有没有研究过多个分支?我认为这是您应该使用的,而不是单一版本,下面是一个示例: