Skip to content

Commit 27773ca

Browse files
committed
Empty lists are correctly excluded from query params, fixes JohnnyCrazy#471
1 parent a15e71c commit 27773ca

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎SpotifyAPI.Web.Tests/Models/RequestParamsTest.cs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public void CacheDoesNotInterfereBody()
3636
Assert.AreEqual("{\"first\":true}", firstParams.ToString(Formatting.None));
3737
Assert.AreEqual("{\"second\":false}", secondParams.ToString(Formatting.None));
3838
}
39+
40+
[Test]
41+
public void EmptyListIsSkippedInQueryParams()
42+
{
43+
var first = new EmptyListExampleRequestModel();
44+
Assert.AreEqual(new Dictionary<string, string> { }, first.BuildQueryParams());
45+
first.List.Add("hello_world");
46+
Assert.AreEqual(new Dictionary<string, string> { { "list", "hello_world" } }, first.BuildQueryParams());
47+
}
3948
}
4049

4150
public class FirstRequestModel : RequestParams
@@ -51,4 +60,10 @@ public class SecondRequestModel : RequestParams
5160
[QueryParam("second")]
5261
public bool? Second { get; set; }
5362
}
63+
64+
public class EmptyListExampleRequestModel : RequestParams
65+
{
66+
[QueryParam("list")]
67+
public IList<string> List { get; set; } = new List<string>();
68+
}
5469
}

‎SpotifyAPI.Web/Models/Request/RequestParams.cs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,13 @@ private void AddQueryParam(Dictionary<string, string> queryParams, PropertyInfo
9797
object value = prop.GetValue(this);
9898
if (value != null)
9999
{
100-
if (value is IList<string> list && list.Count > 0)
100+
if (value is IList<string> list)
101101
{
102-
var str = string.Join(",", list);
103-
queryParams.Add(attribute.Key ?? prop.Name, str);
102+
if (list.Count > 0)
103+
{
104+
var str = string.Join(",", list);
105+
queryParams.Add(attribute.Key ?? prop.Name, str);
106+
}
104107
}
105108
else if (value is bool valueAsBool)
106109
{

0 commit comments

Comments
 (0)