diff --git a/SpotifyAPI.Web.Tests/Models/RequestParamsTest.cs b/SpotifyAPI.Web.Tests/Models/RequestParamsTest.cs index 767ab937..9e6285fd 100644 --- a/SpotifyAPI.Web.Tests/Models/RequestParamsTest.cs +++ b/SpotifyAPI.Web.Tests/Models/RequestParamsTest.cs @@ -45,6 +45,32 @@ namespace SpotifyAPI.Web.Tests first.List.Add("hello_world"); Assert.AreEqual(new Dictionary { { "list", "hello_world" } }, first.BuildQueryParams()); } + + [Test] + public void EnumWithoutFlagsDoesNotHaveMultipleValues() + { + var enumModel = new EnumWithoutFlagsRequestModel + { + AnEnumParam = EnumWithoutFlagsRequestModel.AnEnum.Two + }; + + var result = enumModel.BuildQueryParams(); + Assert.AreEqual(1, result.Keys.Count); + Assert.AreEqual("two", result["an_enum"]); + } + + [Test] + public void EnumWithFlagsDoesHaveMultipleValues() + { + var enumModel = new EnumWitFlagsRequestModel + { + AnEnumParam = EnumWitFlagsRequestModel.AnEnum.Two | EnumWitFlagsRequestModel.AnEnum.One + }; + + var result = enumModel.BuildQueryParams(); + Assert.AreEqual(1, result.Keys.Count); + Assert.AreEqual("one,two", result["an_enum"]); + } } public class FirstRequestModel : RequestParams @@ -66,4 +92,35 @@ namespace SpotifyAPI.Web.Tests [QueryParam("list")] public IList List { get; set; } = new List(); } + + public class EnumWithoutFlagsRequestModel : RequestParams + { + [QueryParam("an_enum")] + public AnEnum AnEnumParam { get; set; } + + public enum AnEnum + { + [String("one")] + One, + + [String("two")] + Two, + } + } + + public class EnumWitFlagsRequestModel : RequestParams + { + [QueryParam("an_enum")] + public AnEnum AnEnumParam { get; set; } + + [Flags] + public enum AnEnum + { + [String("one")] + One = 1, + + [String("two")] + Two = 2, + } + } } diff --git a/SpotifyAPI.Web/Models/Request/ArtistsAlbumsRequest.cs b/SpotifyAPI.Web/Models/Request/ArtistsAlbumsRequest.cs index 36cd2ee6..b1594278 100644 --- a/SpotifyAPI.Web/Models/Request/ArtistsAlbumsRequest.cs +++ b/SpotifyAPI.Web/Models/Request/ArtistsAlbumsRequest.cs @@ -41,17 +41,17 @@ namespace SpotifyAPI.Web public enum IncludeGroups { [String("album")] - Album, + Album = 1, [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720")] [String("single")] - Single, + Single = 2, [String("appears_on")] - AppearsOn, + AppearsOn = 4, [String("compilation")] - Compilation + Compilation = 8, } } } diff --git a/SpotifyAPI.Web/Models/Request/RequestParams.cs b/SpotifyAPI.Web/Models/Request/RequestParams.cs index 4cf451d9..bb7589ed 100644 --- a/SpotifyAPI.Web/Models/Request/RequestParams.cs +++ b/SpotifyAPI.Web/Models/Request/RequestParams.cs @@ -114,19 +114,26 @@ namespace SpotifyAPI.Web var enumType = valueAsEnum.GetType(); var valueList = new List(); - foreach (Enum enumVal in Enum.GetValues(valueAsEnum.GetType())) + if (enumType.IsDefined(typeof(FlagsAttribute), false)) { - if (valueAsEnum.HasFlag(enumVal)) + foreach (Enum enumVal in Enum.GetValues(valueAsEnum.GetType())) { - if (enumType - .GetMember(enumVal.ToString())[0] - .GetCustomAttributes(typeof(StringAttribute)) - .FirstOrDefault() is StringAttribute stringAttr) + if (valueAsEnum.HasFlag(enumVal)) { - valueList.Add(stringAttr.Value); + if (StringAttribute.GetValue(enumType, enumVal, out var stringVal)) + { + valueList.Add(stringVal); + } } } } + else + { + if (StringAttribute.GetValue(enumType, valueAsEnum, out var stringVal)) + { + valueList.Add(stringVal); + } + } queryParams.Add(attribute.Key ?? prop.Name, string.Join(",", valueList)); } else diff --git a/SpotifyAPI.Web/Util/StringAttribute.cs b/SpotifyAPI.Web/Util/StringAttribute.cs index 92fe5b84..d0d54873 100644 --- a/SpotifyAPI.Web/Util/StringAttribute.cs +++ b/SpotifyAPI.Web/Util/StringAttribute.cs @@ -1,4 +1,7 @@ using System; +using System.Reflection; +using System.Linq; +using System.Diagnostics.CodeAnalysis; namespace SpotifyAPI.Web { @@ -11,5 +14,27 @@ namespace SpotifyAPI.Web } public string Value { get; set; } + +#if NETSTANDARD2_1 + public static bool GetValue(Type enumType, Enum enumValue, [NotNullWhen(true)] out string? result) +#endif +#if NETSTANDARD2_0 + public static bool GetValue(Type enumType, Enum enumValue, out string? result) +#endif + { + Ensure.ArgumentNotNull(enumType, nameof(enumType)); + Ensure.ArgumentNotNull(enumValue, nameof(enumValue)); + + if (enumType + .GetMember(enumValue.ToString())[0] + .GetCustomAttributes(typeof(StringAttribute)) + .FirstOrDefault() is StringAttribute stringAttr) + { + result = stringAttr.Value; + return true; + } + result = null; + return false; + } } }