如何在 azure 函数 (2.0) 的 swagger UI 中默认显示 post body 示例

问题描述

我是 swagger 开放 API 文档的新手。在 azure 函数应用 2.0 中集成 Aliencube.AzureFunctions.Extensions.OpenApi.Core

代码如下:

    [FunctionName("TestFunction")]
    [OpenApiOperation(operationId: "TestFunction",tags: new[] { "TestFunction" })]
    [OpenApiRequestBody("text/plain",typeof(string))]
    public static async Task<IActionResult> TestFunctionRun([HttpTrigger(AuthorizationLevel.Anonymous,"post",Route = null)] HttpRequest TestFunctionRequest,ExecutionContext executionContext)
    {
        {
           //deFinition of function
        }
    }

在 Swagger 中显示如下

enter image description here

我们希望显示以下示例值

{
  "username" : "testuser","userid" : "1"
}

如果在 azurefunction 2.0 中使用 AzureFunctions.Extensions.Swashbuckle 显示上述预期示例值。这对我们来说也很有帮助。

请帮我解决这个问题,因为我们的时间不多了。
提前致谢!

解决方法

你应该使用,Pet 是一个模型。

[OpenApiRequestBody(contentType: "application/json",bodyType: typeof(Pet),Required = true,Description = "Pet object that needs to be added to the store")]

enter image description here

重现您的问题:

enter image description here

enter image description here

解决方案:

enter image description here

enter image description here

参考

  1. 源代码AzureFunctions.Extensions

  2. 博客Introducing Swagger UI on Azure Functions

  3. 设置启动项目。

    enter image description here