RicoSuter / NSwag

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

Replace collection length checks for compatibility #4904

Closed jake-carpenter closed 3 weeks ago

jake-carpenter commented 1 month ago

Using the Length property assumes the collection will be a C# array, but that is based on the parameterArrayType setting for the C# client generator. By default, IEnumerable is used and the majority of other collection types won't accept Length.

For now, this fix utilizes the LINQ Count(), which theoretically should check for Length or Count properties on it's own before enumerating. This seems like it is still an improvement to the enumeration that was present before the linked changeset. I'm open to solving this a different way if there are ways to access the C# type during generation.

There are likely other templates where this is also a problem, but this fix is focused on path parameter generation.

jake-carpenter commented 3 weeks ago

Build failures seem to be based on new mac images with .NET 6 not installed, which is probably not something I should address with code here.

MoeHamdan commented 2 weeks ago

Hello

This works fine only if "Parameter Type" for "Generic Array Type" is set to IEnumerable, however if it set to ICollection it will fail. And by fail I mean it will generate Count() and ElementAt() which are not supported by ICollection

The only way I was able to fix this is by adding namespance System.Linq image