Albert
Albert

Reputation: 255

OpenAPI (aka Swagger) in Azure Functions V2

I'm creating a V2 Function App and want to use Swagger/Open API for docs, however it is not yet supported in the Azure Portal for V2 Functions.

Any suggestions on how I can use Swagger with V2 Functions in VSTS to create the docs on each build?

Upvotes: 22

Views: 22383

Answers (4)

justinyoo
justinyoo

Reputation: 2133

TL;DR - Use the NuGet package to render Open API document and Swagger UI through Azure Functions.


UPDATE (2021-06-04)

Microsoft recently announced the OpenAPI support on Azure Functions during the //Build event.

The Aliencube extension has now been archived and no longer supported. Please use this official extension.

As of today, it's in preview. Although it's in preview, it has more features than the Aliencube one.

Acknowledgement 2: I am still maintaining the official one.


Microsoft hasn’t officially started supporting Open API (or Swagger) yet. But there is a community-driven NuGet package currently available:

Nuget > Aliencube.AzureFunctions.Extensions.OpenApi

And here’s the blog post for it:

Introducing Swagger UI on Azure Functions

Basically its usage is similar to Swashbuckle — using decorators. And it supports both Azure Functions V1 and V2.

Acknowledgement 1: I am the owner of the NuGet package.

Upvotes: 31

Miroslav Adamec
Miroslav Adamec

Reputation: 1138

There is official library Azure Functions OpenAPI Extension. It is still in preview, but looks great.

Upvotes: 7

mdarefull
mdarefull

Reputation: 1039

For anyone looking into this, Microsoft still hasn't added Open API support for Azure Functions +v2 and there hasn't been any major movement toward it.

I faced this same issue recently and I found a pretty good solution. If you aren't aware yet, check this out: https://github.com/RicoSuter/NSwag

NSwag is a toolchain that integrates with .NET to produce the Open API documentation and UI, but also generates the respective API clients for you. I've used this tool for the past 2 years when working with Angular and .NET apps, it saves a lot of time and seamlessly integrates with my whole development workflow.

For Azure Functions +v2, a contributor created a generator that in less than 10 lines of code, allow us to expose the Swagger endpoint for an Azure Function class: https://github.com/Jusas/NSwag.AzureFunctionsV2

Now, if you design HTTP-Triggered Functions using DI, grouping related operations under the same class and leveraging model binding, you might not even need to apply any custom decorator, it just works! Here's an example of how one real API would look like: API + Swagger example

(ignore the base class. It is part of a personal pattern I follow, unrelated to Swagger)

And this is how it looks like using a swagger UI: Swagger UI example

Upvotes: 6

ΩmegaMan
ΩmegaMan

Reputation: 31576

Can you drop your V2 Function to run on v1 of the runtime by starting over with a blank function app first?

The MSDN documents for Create an OpenAPI definition for a function (with a date of 11/2018 interestingly enough) example states:

By default, the function app you create uses version 2.x of the runtime. You must set the runtime version back to 1.x before you create your function.

But one can't just move the setting to 1:

To pin your function app to the version 1.x runtime, choose ~1 under Runtime version. This switch is disabled when you have functions in your app.

Which implies one must create the Function app, publish/create it, set it to V1 and then put in a function before adding a function app.

Upvotes: 0

Related Questions