如何为 SwaggerUi 中显示的路径提供路径参数?
How can I supply a path parameter to the displayed path in SwaggerUi?
我在我的 WebAPI 项目上设置了 Swagger/Swashbuckle。我遵循了 guide from Microsoft,其中介绍了如何使用 Swagger 设置 Aspnet.WebApi.Versioning。我的API有多个版本,所以在路由属性中设置了一个{version}
参数,像这样:
[ApiVersion("2")]
[RoutePrefix("api/{version:apiVersion}/values")]
public class AccountController : ApiController
{
[Route("UserInfo")]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
我的问题是,这在文档中显示的路径中显示了一个 {version}
属性,如下所示:
相反,我希望此路径属性实际上具有 ApiVersion
属性中的值,这样阅读文档的客户就不会感到困惑。理想情况下,假设 UrlDiscoverySelector 设置为 v2
,上面的路径应该是:
/api/2/account/userinfo
/api/2/account/externallogin
/api/2/account/manageinfo
我尝试简单地替换 ApiExplorer
的 RelativePath
中的 {version}
在 UI 中起作用,但破坏了测试功能,因为 {version}
已更改为 query
参数而不是 path
,这不是我的 API 的配置方式。
是否可以在 swagger 构建文档之前修改 ApiExplorer
中的值,同时仍保留测试功能?
我正在使用 Swashbuckle 并使用文档过滤器
public class VersionedOperationsFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
foreach (var apiDescriptionsGroup in context.ApiDescriptionsGroups.Items)
{
var version = apiDescriptionsGroup.GroupName;
foreach (ApiDescription apiDescription in apiDescriptionsGroup.Items)
{
apiDescription.RelativePath = apiDescription.RelativePath.Replace("{version}", version);
}
}
}
}
并在 Startup.cs 的 ConfigureServices 方法中添加此过滤器:
services.AddMvc();
var defaultApiVer = new ApiVersion(1, 0);
services.AddApiVersioning(option =>
{
option.ReportApiVersions = true;
option.AssumeDefaultVersionWhenUnspecified = true;
option.DefaultApiVersion = defaultApiVer;
});
services.AddMvcCore().AddVersionedApiExplorer(e=>e.DefaultApiVersion = defaultApiVer);
services.AddSwaggerGen(
options =>
{
var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();
options.DocumentFilter<VersionedOperationsFilter>();
//// add a swagger document for each discovered API version
//// note: you might choose to skip or document deprecated API versions differently
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(description.GroupName.ToString(),
CreateInfoForApiVersion(description));
}
//// integrate xml comments
options.IncludeXmlComments(Path.ChangeExtension(Assembly.GetEntryAssembly().Location, "xml"));
});
end result in Swagger UI
用于 API 版本控制的 API 资源管理器现在支持行为 out-of-the-box 使用:
options.SubstituteApiVersionInUrl = true
这将为您完成替换工作,并从操作描述符中删除 API 版本参数。您通常不需要更改应用于替换值的默认格式,但您可以使用以下方式更改它:
options.SubstitutionFormat = "VVV"; // this is the default
我在我的 WebAPI 项目上设置了 Swagger/Swashbuckle。我遵循了 guide from Microsoft,其中介绍了如何使用 Swagger 设置 Aspnet.WebApi.Versioning。我的API有多个版本,所以在路由属性中设置了一个{version}
参数,像这样:
[ApiVersion("2")]
[RoutePrefix("api/{version:apiVersion}/values")]
public class AccountController : ApiController
{
[Route("UserInfo")]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
我的问题是,这在文档中显示的路径中显示了一个 {version}
属性,如下所示:
相反,我希望此路径属性实际上具有 ApiVersion
属性中的值,这样阅读文档的客户就不会感到困惑。理想情况下,假设 UrlDiscoverySelector 设置为 v2
,上面的路径应该是:
/api/2/account/userinfo
/api/2/account/externallogin
/api/2/account/manageinfo
我尝试简单地替换 ApiExplorer
的 RelativePath
中的 {version}
在 UI 中起作用,但破坏了测试功能,因为 {version}
已更改为 query
参数而不是 path
,这不是我的 API 的配置方式。
是否可以在 swagger 构建文档之前修改 ApiExplorer
中的值,同时仍保留测试功能?
我正在使用 Swashbuckle 并使用文档过滤器
public class VersionedOperationsFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
foreach (var apiDescriptionsGroup in context.ApiDescriptionsGroups.Items)
{
var version = apiDescriptionsGroup.GroupName;
foreach (ApiDescription apiDescription in apiDescriptionsGroup.Items)
{
apiDescription.RelativePath = apiDescription.RelativePath.Replace("{version}", version);
}
}
}
}
并在 Startup.cs 的 ConfigureServices 方法中添加此过滤器:
services.AddMvc();
var defaultApiVer = new ApiVersion(1, 0);
services.AddApiVersioning(option =>
{
option.ReportApiVersions = true;
option.AssumeDefaultVersionWhenUnspecified = true;
option.DefaultApiVersion = defaultApiVer;
});
services.AddMvcCore().AddVersionedApiExplorer(e=>e.DefaultApiVersion = defaultApiVer);
services.AddSwaggerGen(
options =>
{
var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();
options.DocumentFilter<VersionedOperationsFilter>();
//// add a swagger document for each discovered API version
//// note: you might choose to skip or document deprecated API versions differently
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(description.GroupName.ToString(),
CreateInfoForApiVersion(description));
}
//// integrate xml comments
options.IncludeXmlComments(Path.ChangeExtension(Assembly.GetEntryAssembly().Location, "xml"));
});
end result in Swagger UI
用于 API 版本控制的 API 资源管理器现在支持行为 out-of-the-box 使用:
options.SubstituteApiVersionInUrl = true
这将为您完成替换工作,并从操作描述符中删除 API 版本参数。您通常不需要更改应用于替换值的默认格式,但您可以使用以下方式更改它:
options.SubstitutionFormat = "VVV"; // this is the default