2

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

 1 year ago
source link: https://anthonygiretti.com/2023/03/18/asp-net-core-7-beware-of-the-swagger-bug-when-binding-arrays-in-headers-with-minimal-apis/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

2023-03-18 by anthonygiretti

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:

image.png

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

image-1.png

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:

image-2-1024x895.png

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!

Like this:

Loading...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK