Monday, October 27, 2025

.NET Isolated Azure Functions: Enabling Open API Support

In this post let's see how to enable Open API Support in .NET Isolated Azure Functions. Long time ago I blogged about Introducing In-Process Azure Functions OpenAPI Extension. And that's for .NET In-Process functions.

A lot has changed since then.

I have created a simple .NET Isolated Azure Function with a Http Trigger targeting .NET 9.

.NET Isolated Http Trigger
Now let's add the Open API support.

First step is installing the following NuGet package.

Install-Package Microsoft.Azure.Functions.Worker.Extensions.OpenApi -Version 1.6.0
This version is the latest as of today, it will change as we go on.

Now if you run the project (note: you don't have to do any changes in Program.cs), you will see the new endpoints for Open API.
Console
If you open up the Swagger UI URL in a browser, you can see something like following.
Swagger UI
Now let's decorate the HTTP Function with Open API attributes.
[Function(nameof(Function1))]
[OpenApiOperation(operationId: nameof(Function1))]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK,
    contentType: "application/json",
    bodyType: typeof(object),
    Description = "The OK response message.")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req)
{
    return new OkObjectResult(new
    {
        message = "Welcome to Azure Functions!"
    });
}
And to make the Open API spec looks nice, override DefaultOpenApiConfigurationOptions with something like this:
internal class OpenApiConfigurationOptions : DefaultOpenApiConfigurationOptions
{
    public override OpenApiInfo Info { get; set; } = new OpenApiInfo
    {
        Version = "1.0.0",
        Title = "Hello World",
        Description = "A sample API for my Azure Function.",
        License = new OpenApiLicense
        {
            Name = "MIT",
            Url = new Uri("http://opensource.org/licenses/MIT"),
        }
    };
    public override OpenApiVersionType OpenApiVersion { get; set; } = OpenApiVersionType.V3;
}
And now after doing these changes and if you look at the Swagger UI, 
Swagger UI
Open API Spec:
{
  "openapi": "3.0.1",
  "info": {
    "title": "Hello World",
    "description": "A sample API for my Azure Function.",
    "license": {
      "name": "MIT",
      "url": "http://opensource.org/licenses/MIT"
    },
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "http://localhost:7154/api"
    }
  ],
  "paths": {
    "/Function1": {
      "get": {
        "operationId": "Function1",
        "responses": {
          "200": {
            "description": "The OK response message.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {}
}
Hope this helps.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment