Skip to content

C# Client Querystring compilation error #5260

@SkipperRoo

Description

@SkipperRoo

Generated code has compile errors

The fix for #5194 has issues with it if you've got nullable & non nullable properties. The code that's generated uses a flag named isAfterFirst that's used for removal of the last comma. The issue is that this flag is defined at different scopes and causes the compiler to fail.

Version of NSwag 14.5.0 toolchain, computer and .NET 8

To Reproduce
Generate a client form the example .yaml and you'll see the code below fails because the first isAfterFirst is at a higher scope level than the one later on.

    bool isAfterFirst = false;
    foreach (var item_ in destinationFolderId)
    {
        if (!isAfterFirst)
        {
            urlBuilder_.Append(System.Uri.EscapeDataString("destinationFolderId")).Append('=');
        }
        urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(item_, System.Globalization.CultureInfo.InvariantCulture))).Append(',');
        isAfterFirst = true;
    }
    if (isAfterFirst)
    {
        urlBuilder_.Length--;
        urlBuilder_.Append('&');
    }
    if (orderStatus != null)
    {
        bool isAfterFirst = false;
        foreach (var item_ in orderStatus)

Expected behavior

A clear and concise description of what you expected to happen.

Query string example

The yaml below can be used to demonstrate the issues with the C# client that's generated

querystring-example.yaml

A different approach
I changed the liquid template file Client.Class.QueryParameter.liquid to use .Any() and left everything as it was in 14.4.0

{% elsif parameter.IsArray -%}
// dotConnected Modified to test for an empty array
if({{ parameter.VariableName }}.Any())
{
  urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}") + "=");
  foreach (var item_ in {{ parameter.VariableName }})
  {
      urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(item_, System.Globalization.CultureInfo.InvariantCulture))).Append(",");
  }
  urlBuilder_.Length--;
  urlBuilder_.Append("&");
}
{% elsif parameter.IsDictionary -%}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions