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("{\"first\":true}", firstParams.ToString(Formatting.None));
Assert.AreEqual("{\"second\":false}", secondParams.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 public class FirstRequestModel : RequestParams
@ -51,4 +60,10 @@ namespace SpotifyAPI.Web.Tests
[QueryParam("second")] [QueryParam("second")]
public bool? Second { get; set; } public bool? Second { get; set; }
} }
public class EmptyListExampleRequestModel : RequestParams
{
[QueryParam("list")]
public IList<string> List { get; set; } = new List<string>();
}
} }

View File

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