Skip to content

Commit 6f0e1b6

Browse files
committed
Only list enums when a flag attribute is pressent, fixes #478
1 parent 7b2faee commit 6f0e1b6

File tree

4 files changed

+100
-11
lines changed

4 files changed

+100
-11
lines changed

SpotifyAPI.Web.Tests/Models/RequestParamsTest.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,32 @@ public void EmptyListIsSkippedInQueryParams()
4545
first.List.Add("hello_world");
4646
Assert.AreEqual(new Dictionary<string, string> { { "list", "hello_world" } }, first.BuildQueryParams());
4747
}
48+
49+
[Test]
50+
public void EnumWithoutFlagsDoesNotHaveMultipleValues()
51+
{
52+
var enumModel = new EnumWithoutFlagsRequestModel
53+
{
54+
AnEnumParam = EnumWithoutFlagsRequestModel.AnEnum.Two
55+
};
56+
57+
var result = enumModel.BuildQueryParams();
58+
Assert.AreEqual(1, result.Keys.Count);
59+
Assert.AreEqual("two", result["an_enum"]);
60+
}
61+
62+
[Test]
63+
public void EnumWithFlagsDoesHaveMultipleValues()
64+
{
65+
var enumModel = new EnumWitFlagsRequestModel
66+
{
67+
AnEnumParam = EnumWitFlagsRequestModel.AnEnum.Two | EnumWitFlagsRequestModel.AnEnum.One
68+
};
69+
70+
var result = enumModel.BuildQueryParams();
71+
Assert.AreEqual(1, result.Keys.Count);
72+
Assert.AreEqual("one,two", result["an_enum"]);
73+
}
4874
}
4975

5076
public class FirstRequestModel : RequestParams
@@ -66,4 +92,35 @@ public class EmptyListExampleRequestModel : RequestParams
6692
[QueryParam("list")]
6793
public IList<string> List { get; set; } = new List<string>();
6894
}
95+
96+
public class EnumWithoutFlagsRequestModel : RequestParams
97+
{
98+
[QueryParam("an_enum")]
99+
public AnEnum AnEnumParam { get; set; }
100+
101+
public enum AnEnum
102+
{
103+
[String("one")]
104+
One,
105+
106+
[String("two")]
107+
Two,
108+
}
109+
}
110+
111+
public class EnumWitFlagsRequestModel : RequestParams
112+
{
113+
[QueryParam("an_enum")]
114+
public AnEnum AnEnumParam { get; set; }
115+
116+
[Flags]
117+
public enum AnEnum
118+
{
119+
[String("one")]
120+
One = 1,
121+
122+
[String("two")]
123+
Two = 2,
124+
}
125+
}
69126
}

SpotifyAPI.Web/Models/Request/ArtistsAlbumsRequest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ public class ArtistsAlbumsRequest : RequestParams
4141
public enum IncludeGroups
4242
{
4343
[String("album")]
44-
Album,
44+
Album = 1,
4545

4646
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720")]
4747
[String("single")]
48-
Single,
48+
Single = 2,
4949

5050
[String("appears_on")]
51-
AppearsOn,
51+
AppearsOn = 4,
5252

5353
[String("compilation")]
54-
Compilation
54+
Compilation = 8,
5555
}
5656
}
5757
}

SpotifyAPI.Web/Models/Request/RequestParams.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,26 @@ private void AddQueryParam(Dictionary<string, string> queryParams, PropertyInfo
114114
var enumType = valueAsEnum.GetType();
115115
var valueList = new List<string>();
116116

117-
foreach (Enum enumVal in Enum.GetValues(valueAsEnum.GetType()))
117+
if (enumType.IsDefined(typeof(FlagsAttribute), false))
118118
{
119-
if (valueAsEnum.HasFlag(enumVal))
119+
foreach (Enum enumVal in Enum.GetValues(valueAsEnum.GetType()))
120120
{
121-
if (enumType
122-
.GetMember(enumVal.ToString())[0]
123-
.GetCustomAttributes(typeof(StringAttribute))
124-
.FirstOrDefault() is StringAttribute stringAttr)
121+
if (valueAsEnum.HasFlag(enumVal))
125122
{
126-
valueList.Add(stringAttr.Value);
123+
if (StringAttribute.GetValue(enumType, enumVal, out var stringVal))
124+
{
125+
valueList.Add(stringVal);
126+
}
127127
}
128128
}
129129
}
130+
else
131+
{
132+
if (StringAttribute.GetValue(enumType, valueAsEnum, out var stringVal))
133+
{
134+
valueList.Add(stringVal);
135+
}
136+
}
130137
queryParams.Add(attribute.Key ?? prop.Name, string.Join(",", valueList));
131138
}
132139
else

SpotifyAPI.Web/Util/StringAttribute.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Reflection;
3+
using System.Linq;
4+
using System.Diagnostics.CodeAnalysis;
25

36
namespace SpotifyAPI.Web
47
{
@@ -11,5 +14,27 @@ public StringAttribute(string value)
1114
}
1215

1316
public string Value { get; set; }
17+
18+
#if NETSTANDARD2_1
19+
public static bool GetValue(Type enumType, Enum enumValue, [NotNullWhen(true)] out string? result)
20+
#endif
21+
#if NETSTANDARD2_0
22+
public static bool GetValue(Type enumType, Enum enumValue, out string? result)
23+
#endif
24+
{
25+
Ensure.ArgumentNotNull(enumType, nameof(enumType));
26+
Ensure.ArgumentNotNull(enumValue, nameof(enumValue));
27+
28+
if (enumType
29+
.GetMember(enumValue.ToString())[0]
30+
.GetCustomAttributes(typeof(StringAttribute))
31+
.FirstOrDefault() is StringAttribute stringAttr)
32+
{
33+
result = stringAttr.Value;
34+
return true;
35+
}
36+
result = null;
37+
return false;
38+
}
1439
}
1540
}

0 commit comments

Comments
 (0)