【问题标题】:How to display post body example by default in swagger UI from azure functions(2.0)如何在 azure 函数的 swagger UI 中默认显示帖子正文示例(2.0)
【发布时间】:2021-09-02 23:17:55
【问题描述】:

我是大摇大摆的开放 API 文档的新手。在 azure function app 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 中显示如下

默认情况下,我们应该在示例值下方显示

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

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

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

【问题讨论】:

  • 如何将属性更改为[OpenApiRequestBody("application/json", typeof(User))]。确保在typeof 中添加正确的类。
  • 我的回答对你有用吗?

标签: c# azure-functions asp.net-core-2.0 swagger-ui swagger-2.0


【解决方案1】:

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

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

重现您的问题:

解决方案:

参考

  1. 源码AzureFunctions.Extensions

  2. 博客Introducing Swagger UI on Azure Functions

  3. 设置启动项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多