RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.62k stars 1.23k forks source link

Controllers generated by NSwag are not deserialising request bodies properly #4802

Open davidkeaveny opened 4 months ago

davidkeaveny commented 4 months ago

I'm using NSwag 14.0.2.0, and have noticed that when I use NSwag to generate a controller from my OpenAPI 3.1 specification, if the API consumer sends an invalid payroll (i.e. doesn't deserialise cleanly to the expected type) then .NET deserialises it to null instead, rather than returning a 400 Bad Request as would be expected. Further investigation indicates that the reason for this is that the controllers generated by NSwag to not have the ApiController attribute (see https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.1#annotate-class-with-apicontrollerattribute).

I appreciate that we can always create a custom Liquid template for the controller as a workaround, but would it be possible to add a configuration option to tell NSwag to add the attribute, or even do it automatically for supported versions of .NET Core? Otherwise it seems to be giving up a lot of built-in good stuff.