Skip to content

Commit aab8c5d

Browse files
authored
Fix potential race condition inside RequestParams (#571)
1 parent 81245b2 commit aab8c5d

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

SpotifyAPI.Web/Models/Request/RequestParams.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ public JObject BuildBodyParams()
3434
.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
3535
.Where(prop => prop.GetCustomAttributes(typeof(BodyParamAttribute), true).Length > 0);
3636

37-
_bodyParamsCache[type] = new List<(PropertyInfo, BodyParamAttribute)>();
37+
var cachedParams = new List<(PropertyInfo, BodyParamAttribute)>();
3838
foreach (var prop in bodyProps)
3939
{
4040
var attribute = prop.GetCustomAttribute<BodyParamAttribute>();
4141
if (attribute != null)
4242
{
43-
_bodyParamsCache[type].Add((prop, attribute));
43+
cachedParams.Add((prop, attribute));
4444
AddBodyParam(body, prop, attribute);
4545
}
4646
}
47+
_bodyParamsCache[type] = cachedParams;
4748
}
4849

4950
return body;
@@ -81,16 +82,17 @@ public Dictionary<string, string> BuildQueryParams()
8182
var queryProps = GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
8283
.Where(prop => prop.GetCustomAttributes(typeof(QueryParamAttribute), true).Length > 0);
8384

84-
_queryParamsCache[type] = new List<(PropertyInfo, QueryParamAttribute)>();
85+
var cachedParams = new List<(PropertyInfo, QueryParamAttribute)>();
8586
foreach (var prop in queryProps)
8687
{
8788
var attribute = prop.GetCustomAttribute<QueryParamAttribute>();
8889
if (attribute != null)
8990
{
90-
_queryParamsCache[type].Add((prop, attribute));
91+
cachedParams.Add((prop, attribute));
9192
AddQueryParam(queryParams, prop, attribute);
9293
}
9394
}
95+
_queryParamsCache[type] = cachedParams;
9496
}
9597

9698
AddCustomQueryParams(queryParams);

0 commit comments

Comments
 (0)