mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-23 14:46:26 +00:00
Only list enums when a flag attribute is pressent, fixes #478
This commit is contained in:
parent
7b2faee8cc
commit
6f0e1b6f80
@ -45,6 +45,32 @@ namespace SpotifyAPI.Web.Tests
|
||||
first.List.Add("hello_world");
|
||||
Assert.AreEqual(new Dictionary<string, string> { { "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<string> List { get; set; } = new List<string>();
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -114,19 +114,26 @@ namespace SpotifyAPI.Web
|
||||
var enumType = valueAsEnum.GetType();
|
||||
var valueList = new List<string>();
|
||||
|
||||
if (enumType.IsDefined(typeof(FlagsAttribute), false))
|
||||
{
|
||||
foreach (Enum enumVal in Enum.GetValues(valueAsEnum.GetType()))
|
||||
{
|
||||
if (valueAsEnum.HasFlag(enumVal))
|
||||
{
|
||||
if (enumType
|
||||
.GetMember(enumVal.ToString())[0]
|
||||
.GetCustomAttributes(typeof(StringAttribute))
|
||||
.FirstOrDefault() is StringAttribute stringAttr)
|
||||
if (StringAttribute.GetValue(enumType, enumVal, out var stringVal))
|
||||
{
|
||||
valueList.Add(stringAttr.Value);
|
||||
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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user