OpenAPI and Azure Functions Out-of-Process

2 minute read

Previously, I blogged about OpenAPI and Azure Functions showcasing the way to generate a Swagger definition from Functions (before official extension was available, which I would pretty much go ahead with in hosted in-process). With the out-of-process model available, the extension is not going to work due to its dependency on the in-process model. So I decided to upgrade my previous demo to showcase how it can be done in the out-of-process hosting model.

All I had to do to get this work, was to generate a function in .NET 5.0, create the model, respective endpoints and annotate them properly. Few more changes ahd to be done due to differences between HttpRequest and HttpRequestData and also HttpResponse and HttpResponseData (but that was fairly simple to deal with).

I have also added a nice feature - the Function now serves Swagger UI! It was easier to add than I thought, simply add a HTML page, add the Swagger UI styles and scripts from CDN, create a function to serve the static file and you are good to go!

In the demo, there are now two definition endpoints /api/Swagger (returns OpenAPI 3.0 definition) and /api/SwaggerV2 (returns Swagger - OpenAPI 2.0 definition) - both of them can be rendered in the UI (yes, the naming is kinda meh, but it’s just a demo).

One thing which is missing compared to the original (I have moved the in-process sample to its own branch), is the support for parsing of form data - ReadFormAsync. Due to the request being passed in as HttpRequestData (which has nothing to do with HttpRequest) you can’t use the ASP.NET Core one out of box, but you can leverage a similar one or tweak the ASP.NET Core implementation to work with HttpRequestData.

Full sample: https://github.com/hajekj/azure-functions-openapi-demo

Also, if you need to add Azure AD authentication to Functions running in out-of-process model, check out my previous post.

Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...