NeVeSpl / NTypewriter

File/code generator using Scriban text templates populated with C# code metadata from Roslyn API.
https://nevespl.github.io/NTypewriter/
MIT License
117 stars 24 forks source link

Action.Url does not support array-valued parameters #116

Open Ackhuman opened 1 month ago

Ackhuman commented 1 month ago

I have an API method with the following signature:

IEnumerable<DataSeries> GetDashboardChart(string stateAbbr, [FromUri]string[] varNames)

The URL GetDashboardChart?stateAbbr=&varNames=VAR1&varNames=VAR2 should result in a valid query to this method. However, there is no support for this in Action.Url. I have written a custom function to handle this case:

    public static string GetArrayQueryParams(IMethod method)
    {
        var arrayParameters = new List<string>();
        foreach (var parameter in method.Parameters)
        {
            if (parameter.Type.IsCollection && !parameter.HasAttribute("FromBody"))
            {
                arrayParameters.Add($"${{{parameter.Name}.map(item => `{parameter.Name}=${{item}}`).join('&')}}");
            }
        }
        return arrayParameters.Any() ? $"&{string.Join("&", arrayParameters)}" : string.Empty;
    }

Which results in the following TypeScript:

&${varNames.map(item => `varNames=${item}`).join('&')}

This works, but it would be better if this were built-in functionality.

NeVeSpl commented 1 month ago

Fell free to prepare PR with that functionality, just remember to include appropriate tests to it.