Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace collection length checks for compatibility #4904

Conversation

jake-carpenter
Copy link
Contributor

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
Copy link
Contributor Author

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.

@RicoSuter RicoSuter merged commit 413c17c into RicoSuter:master Jun 12, 2024
2 of 3 checks passed
lahma pushed a commit to lahma/NSwag that referenced this pull request Jun 13, 2024
@MoeHamdan
Copy link

MoeHamdan commented Jun 20, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compiler errors in C# client generated code when request has a List<T> as path parameter
3 participants