From 27773caa2830ea569ae9fdf1385cd450fb35dcce Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Mon, 29 Jun 2020 22:42:35 +0200 Subject: [PATCH] Empty lists are correctly excluded from query params, fixes #471 --- SpotifyAPI.Web.Tests/Models/RequestParamsTest.cs | 15 +++++++++++++++ SpotifyAPI.Web/Models/Request/RequestParams.cs | 9 ++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/SpotifyAPI.Web.Tests/Models/RequestParamsTest.cs b/SpotifyAPI.Web.Tests/Models/RequestParamsTest.cs index bad04d30..767ab937 100644 --- a/SpotifyAPI.Web.Tests/Models/RequestParamsTest.cs +++ b/SpotifyAPI.Web.Tests/Models/RequestParamsTest.cs @@ -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 { }, first.BuildQueryParams()); + first.List.Add("hello_world"); + Assert.AreEqual(new Dictionary { { "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 List { get; set; } = new List(); + } } diff --git a/SpotifyAPI.Web/Models/Request/RequestParams.cs b/SpotifyAPI.Web/Models/Request/RequestParams.cs index da3c52d1..4cf451d9 100644 --- a/SpotifyAPI.Web/Models/Request/RequestParams.cs +++ b/SpotifyAPI.Web/Models/Request/RequestParams.cs @@ -97,10 +97,13 @@ namespace SpotifyAPI.Web object value = prop.GetValue(this); if (value != null) { - if (value is IList list && list.Count > 0) + if (value is IList list) { - var str = string.Join(",", list); - queryParams.Add(attribute.Key ?? prop.Name, str); + if (list.Count > 0) + { + var str = string.Join(",", list); + queryParams.Add(attribute.Key ?? prop.Name, str); + } } else if (value is bool valueAsBool) {