如何让Swagger同时展示同一个Azure Function的PUT和GET两种请求方式
这是Azure Functions OpenAPI扩展v0.9预览版的已知限制,默认仅会读取HttpTrigger属性中声明的第一个请求方法生成Swagger配置,你可以通过以下两种方式实现双请求方法的展示:
方法一:显式叠加多套OpenAPI注解
在函数方法上分别为GET、PUT请求声明独立的OpenAPI配置,注意两个OpenApiOperation的operationId不能重复,否则会触发Swagger生成错误,示例代码如下:
[Function("GetCases")] // GET请求OpenAPI配置 [OpenApiOperation(operationId: "GetCases_Get", verbs: new[] { "get" }, Summary = "分页获取案例数据")] [OpenApiParameter(name: "page", In = ParameterLocation.Path, Required = true, Type = typeof(int))] [OpenApiParameter(name: "pageSize", In = ParameterLocation.Path, Required = true, Type = typeof(int))] [OpenApiParameter(name: "sort", In = ParameterLocation.Path, Required = true, Type = typeof(string))] [OpenApiParameter(name: "filter", In = ParameterLocation.Path, Required = false, Type = typeof(string))] [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(CasePageResult), Summary = "分页案例数据集")] // PUT请求OpenAPI配置 [OpenApiOperation(operationId: "GetCases_Put", verbs: new[] { "put" }, Summary = "带自定义筛选条件分页获取案例数据")] [OpenApiParameter(name: "page", In = ParameterLocation.Path, Required = true, Type = typeof(int))] [OpenApiParameter(name: "pageSize", In = ParameterLocation.Path, Required = true, Type = typeof(int))] [OpenApiParameter(name: "sort", In = ParameterLocation.Path, Required = true, Type = typeof(string))] [OpenApiParameter(name: "filter", In = ParameterLocation.Path, Required = false, Type = typeof(string))] [OpenApiRequestBody(contentType: "application/json", bodyType: typeof(CaseFilter), Required = true, Description = "自定义检索条件配置")] [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(CasePageResult), Summary = "分页案例数据集")] public async Task<HttpResponseData> GetCases([HttpTrigger(AuthorizationLevel.User, "put", "get", Route = "Case/{page}/{pageSize}/{sort}/{filter?}")] HttpRequestData req, FunctionContext executionContext, int page, int pageSize, string sort, string filter) { var logger = executionContext.GetLogger("GetCourtCases"); logger.LogInformation("GetCourtCases function processed a request."); return await g.GetMany(req, page, pageSize, sort, filter); }
方法二:自定义OperationFilter批量处理
如果有多个接口需要适配双请求方法的场景,可以实现IOpenApiOperationFilter接口,在Swagger文档生成阶段动态追加请求方法配置,示例如下:
- 实现Filter逻辑:
public class DualMethodOperationFilter : IOpenApiOperationFilter { public void Apply(OpenApiOperation operation, OpenApiOperationFilterContext context) { // 匹配指定函数 if (context.FunctionName == "GetCases") { // 复制已有配置生成PUT请求的操作定义 var putOperation = JsonSerializer.Deserialize<OpenApiOperation>(JsonSerializer.Serialize(operation)); putOperation.OperationId = $"{operation.OperationId}_Put"; // 补充PUT请求的请求体配置 putOperation.RequestBody = new OpenApiRequestBody { Content = new Dictionary<string, OpenApiMediaType> { ["application/json"] = new OpenApiMediaType { Schema = context.SchemaGenerator.GenerateSchema(typeof(CaseFilter), context.SchemaRepository) } } }; // 追加到路径的操作集合中 var path = $"/{context.ApiDescription.RelativePath}"; if (context.OpenApiDocument.Paths.TryGetValue(path, out var pathItem)) { pathItem.AddOperation(OperationType.Put, putOperation); } } } }
- 在启动配置中注册Filter:
var host = new HostBuilder() .AddFunctionsOpenApi<OpenApiConfigurationOptions>(options => { options.OperationFilters.Add(new DualMethodOperationFilter()); }) .Build(); host.Run();
内容的提问来源于stack exchange,提问作者GilShalit
相关产品推荐
相关产品推荐

