Only list enums when a flag attribute is pressent, fixes #478

This commit is contained in:
Jonas Dellinger 2020-07-13 15:49:23 +02:00
parent 7b2faee8cc
commit 6f0e1b6f80
4 changed files with 100 additions and 11 deletions

View File

@ -45,6 +45,32 @@ namespace SpotifyAPI.Web.Tests
first.List.Add("hello_world"); first.List.Add("hello_world");
Assert.AreEqual(new Dictionary<string, string> { { "list", "hello_world" } }, first.BuildQueryParams()); 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 public class FirstRequestModel : RequestParams
@ -66,4 +92,35 @@ namespace SpotifyAPI.Web.Tests
[QueryParam("list")] [QueryParam("list")]
public IList<string> List { get; set; } = new List<string>(); 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,
}
}
} }

View File

@ -41,17 +41,17 @@ namespace SpotifyAPI.Web
public enum IncludeGroups public enum IncludeGroups
{ {
[String("album")] [String("album")]
Album, Album = 1,
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720")]
[String("single")] [String("single")]
Single, Single = 2,
[String("appears_on")] [String("appears_on")]
AppearsOn, AppearsOn = 4,
[String("compilation")] [String("compilation")]
Compilation Compilation = 8,
} }
} }
} }

View File

@ -114,19 +114,26 @@ namespace SpotifyAPI.Web
var enumType = valueAsEnum.GetType(); var enumType = valueAsEnum.GetType();
var valueList = new List<string>(); var valueList = new List<string>();
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 if (valueAsEnum.HasFlag(enumVal))
.GetMember(enumVal.ToString())[0]
.GetCustomAttributes(typeof(StringAttribute))
.FirstOrDefault() is StringAttribute stringAttr)
{ {
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)); queryParams.Add(attribute.Key ?? prop.Name, string.Join(",", valueList));
} }
else else

View File

@ -1,4 +1,7 @@
using System; using System;
using System.Reflection;
using System.Linq;
using System.Diagnostics.CodeAnalysis;
namespace SpotifyAPI.Web namespace SpotifyAPI.Web
{ {
@ -11,5 +14,27 @@ namespace SpotifyAPI.Web
} }
public string Value { get; set; } 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;
}
} }
} }