Empty lists are correctly excluded from query params, fixes #471

This commit is contained in:
Jonas Dellinger 2020-06-29 22:42:35 +02:00
parent a15e71c815
commit 27773caa28
2 changed files with 21 additions and 3 deletions

View File

@ -36,6 +36,15 @@ namespace SpotifyAPI.Web.Tests
Assert.AreEqual("{\"first\":true}", firstParams.ToString(Formatting.None));
Assert.AreEqual("{\"second\":false}", secondParams.ToString(Formatting.None));
}
[Test]
public void EmptyListIsSkippedInQueryParams()
{
var first = new EmptyListExampleRequestModel();
Assert.AreEqual(new Dictionary<string, string> { }, first.BuildQueryParams());
first.List.Add("hello_world");
Assert.AreEqual(new Dictionary<string, string> { { "list", "hello_world" } }, first.BuildQueryParams());
}
}
public class FirstRequestModel : RequestParams
@ -51,4 +60,10 @@ namespace SpotifyAPI.Web.Tests
[QueryParam("second")]
public bool? Second { get; set; }
}
public class EmptyListExampleRequestModel : RequestParams
{
[QueryParam("list")]
public IList<string> List { get; set; } = new List<string>();
}
}

View File

@ -97,11 +97,14 @@ namespace SpotifyAPI.Web
object value = prop.GetValue(this);
if (value != null)
{
if (value is IList<string> list && list.Count > 0)
if (value is IList<string> list)
{
if (list.Count > 0)
{
var str = string.Join(",", list);
queryParams.Add(attribute.Key ?? prop.Name, str);
}
}
else if (value is bool valueAsBool)
{
queryParams.Add(attribute.Key ?? prop.Name, valueAsBool ? "true" : "false");