SHARE:

ASP.NET Core 7: Beware of the Swagger bug when binding arrays in headers with Minimal APIs

Introduction

ASP.NET Core 7, recently released, has introduced a new feature on Minimals APIs: The possibility to bind arrays (string, integers etc…..) in headers, which was already available in ASP.NET Core MVC, ASP.NET Core Web APIs…. Although Minimal APIs tends to fill a gap there as some glitches, like for example wit Swagger. In this post I will show you how bindings arrays in headers work and show you what bug wuth Swagger you can expect.

Binding arrays in headers

Binding arrays is pretty simple, you have to decorate your minimal endpoint with the FromHeader attribute, and setup the name of the items in the headers to get binded as follow:

app.MapGet("/{YourEndpoint}", ([FromHeader(Name = "{ItemName}")] int[] {ItemName}) => {YourCode});

Concrete example here, with a collection of integers named id in the headers:

This code gives the following on Postman:

Internally headers are setup like this, a set of id with their Key/Value pair:

Bug with Swagger

Unfortunately, it does not work properly wit Swagger. There is a bug when Swagger try to pass the items in the Headers, item are not setup with their Key/Value pair but they are aggregated in a single entry separated with a comma instead:

It’s a bit disappointing, but if you want test and endpoint I suggest you to test it with Postman, which works fine.

If by any chance Swashbuckle for ASP.NET Core (name of the Nuget package) gets an update to fix this, I will notice you :). Thanks for reading!

Written by

anthonygiretti

Anthony is a specialist in Web technologies (14 years of experience), in particular Microsoft .NET and learns the Cloud Azure platform. He has received twice the Microsoft MVP award and he is also certified Microsoft MCSD and Azure Fundamentals.
%d bloggers like this: