How to display post body example by default in swagger UI from azure functions(2.0)

2.2k views Asked by At

I'm new to swagger open API documentation. Integrating the Aliencube.AzureFunctions.Extensions.OpenApi.Core in azure function app 2.0.

Here is the code :

    [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
        }
    }

In Swagger it is displaying as below

enter image description here

we are expected to display below example value by default

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

If the above Expected example value is displayed by default by using AzureFunctions.Extensions.Swashbuckle in azurefunction 2.0. It is also help full for us.

Please help me out from this problem as we are running out of time.
Thanks in advance!

1

There are 1 answers

0
Jason Pan On

You should use, Pet is a model.

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

enter image description here

Reproduce your problem:

enter image description here

enter image description here

Solution:

enter image description here

enter image description here

Reference

  1. Source codeAzureFunctions.Extensions

  2. Blog : Introducing Swagger UI on Azure Functions

  3. set startup project.

    enter image description here