Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 413
Albert Avatar asked Sep 25 '18 14:09

Albert


People also ask

What is OpenAPI in Azure?

According to Swagger – OpenAPI Document is a document (or set of documents) that defines or describes an API. An OpenAPI definition uses and conforms to the OpenAPI Specification.

How do I access swagger on Azure?

Browse to the Azure appNavigate to http://<app_name>.azurewebsites.net/swagger in a browser and play with the Swagger UI. Navigate to http://<app_name>.azurewebsites.net/swagger/v1/swagger.json to see the swagger. json for your deployed API.

What is swagger Azure?

SwaggerHub integrates with Azure API Management, which allows you to easily export your API definitions to Azure. This way you can design your APIs on SwaggerHub, then deploy your API design to your Azure API Management instance from SwaggerHub, keeping the APIs updated with any changes you make to the design.


2 Answers

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.

like image 104
justinyoo Avatar answered Oct 19 '22 20:10

justinyoo


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

like image 27
mdarefull Avatar answered Oct 19 '22 18:10

mdarefull