Azure / azure-functions-openapi-extension

This extension provides an Azure Functions app with Open API capability for better discoverability to consuming parties
https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.OpenApi/
MIT License
367 stars 191 forks source link

Parameters set to the type of `List<int>` do not bind properly #541

Open archubbuck opened 1 year ago

archubbuck commented 1 year ago

Describe the issue Parameters set to the type of List<int> do not bind properly. I suspect that I'm missing something?

Expected behavior I would expect the id parameter to map to an array without using the strategy demonstrated in the screenshots below.

Screenshots image image

archubbuck commented 1 year ago

Setting the Id property of NotificationListRequest results in the following: image

image

image

archubbuck commented 1 year ago

@justinyoo, is this something you might be able to advise on?

marcin-burak commented 1 year ago

Your issue comes from Azure Functions limitations and not from Open API document generation that is provided by this repository and NuGet package. Most developers who start working with Azure Functions and have some ASP.NET experience under their belt encounter this issue sooner or later because we are accustomed to advanced model binding features like [FromQuery], [FromPath] or [FromBody] attributes. Although Azure Functions .NET in-process model references and leverages few ASP.NET Core types like Microsoft.AspNetCore.Http.HttpRequest or Microsoft.AspNetCore.Http.HttpContext it does not provide the same rich web application features like ASP.NET does. Azure Functions is a much more generic, lightweight and not so deeply web focused technology, instead it focuses heavily on event driven Azure service integrations with plenty of triggers and bindings. The problem with differentiating what Azure Functions supports and what it does not was so prevalent that in the new out-of-process hosting model Azure Functions does not reference ASP.NET types and it provides its own HTTP related types such as Microsoft.Azure.Functions.Worker.Http.HttpRequestData or Microsoft.Azure.Functions.Worker.Http.HttpResponseData.

Simply put, you have encountered HttpTrigger model binding limitation and you'll have to some workaround stuff to keep your code clean and without boilerplate. From my point of view, you have three options from most advanced (but cleanest) to least advanced (but with lots of boilerplate):

  1. Write your own binding that supports your model binding scenarios, here's an example
  2. Do some manual string manipulation (that's what you did in your code, you've split a string into an array of integers)
  3. Work directly with Microsoft.AspNetCore.Http.HttpRequest.Query and parse your query string manually

You're not the first person to encounter this issue, you can read more in the Azure Functions host repository, here's a similar issue.