Skip to content

Commit 7489bb0

Browse files
committed
Fixed issues comma separated GUID values
Until now, the thwo methods supported query strings like "guid1=a&guid2=b", but not "guids=a,b". With this commit, both scenarios are now supported.
1 parent 5b23697 commit 7489bb0

3 files changed

Lines changed: 28 additions & 19 deletions

File tree

src/Skybrud.Essentials.AspNetCore/QueryStringExtensions.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ public static Guid GetGuid(this IQueryCollection? query, string key, Guid fallba
405405
/// <param name="key">The key of the query string component.</param>
406406
/// <returns>An instance of <see cref="List{Guid}"/>.</returns>
407407
public static Guid[] GetGuidArray(this IQueryCollection? query, string key) {
408-
return query is null ? Array.Empty<Guid>() : query.GetGuidList(key).ToArray();
408+
return query?[key].ToGuidArray() ?? Array.Empty<Guid>();
409409
}
410410

411411
/// <summary>
@@ -415,18 +415,7 @@ public static Guid[] GetGuidArray(this IQueryCollection? query, string key) {
415415
/// <param name="key">The key of the query string component.</param>
416416
/// <returns>An instance of <see cref="List{Guid}"/>.</returns>
417417
public static List<Guid> GetGuidList(this IQueryCollection? query, string key) {
418-
419-
List<Guid> guids = new();
420-
421-
StringValues? values = query?[key];
422-
if (values is null) return guids;
423-
424-
foreach (string? value in values) {
425-
if (StringUtils.TryParseGuid(value, out Guid guid)) guids.Add(guid);
426-
}
427-
428-
return guids;
429-
418+
return query?[key].ToGuidList() ?? new List<Guid>();
430419
}
431420

432421
/// <summary>

src/Skybrud.Essentials.AspNetCore/StringValuesExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,24 @@ public static Guid ToGuid(this StringValues values, Guid fallback) {
189189
return StringUtils.TryParseGuid(str, out Guid? result) ? result : null;
190190
}
191191

192+
/// <summary>
193+
/// Parses the specified array of string <paramref name="values"/> into a <see cref="Guid"/> array.
194+
/// </summary>
195+
/// <param name="values">The string values.</param>
196+
/// <returns>An array of <see cref="Guid"/>.</returns>
197+
public static Guid[] ToGuidArray(this StringValues values) {
198+
return values.SelectMany(StringUtils.ParseGuidArray).ToArray();
199+
}
200+
201+
/// <summary>
202+
/// Parses the specified array of string <paramref name="values"/> into a <see cref="Guid"/> list.
203+
/// </summary>
204+
/// <param name="values">The string values.</param>
205+
/// <returns>A list of <see cref="Guid"/>.</returns>
206+
public static List<Guid> ToGuidList(this StringValues values) {
207+
return values.SelectMany(StringUtils.ParseGuidList).ToList();
208+
}
209+
192210
}
193211

194212
}

src/TestProject1/QueryStringTests.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,33 @@ public void GetGuidOrNull() {
6060
public void GetGuidArray() {
6161

6262
IQueryCollection query = new QueryCollection(new Dictionary<string, StringValues> {
63-
{"guids", new StringValues(new[] { "7ce565ca-3dfe-4bc8-9166-4c4a5d1a9cbb", "a11c5663-d025-49be-93d7-876226dfd9b1", "nope", null })}
63+
{"guids", new StringValues(new[] { "7ce565ca-3dfe-4bc8-9166-4c4a5d1a9cbb,8a2a3f66-898c-4b1e-8db7-efb8c08d4669", "a11c5663-d025-49be-93d7-876226dfd9b1", "nope", null })}
6464
});
6565

6666
var guids = query.GetGuidArray("guids");
6767

68-
Assert.AreEqual(2, guids.Length);
68+
Assert.AreEqual(3, guids.Length);
6969

7070
Assert.AreEqual("7ce565ca-3dfe-4bc8-9166-4c4a5d1a9cbb", guids[0].ToString());
71-
Assert.AreEqual("a11c5663-d025-49be-93d7-876226dfd9b1", guids[1].ToString());
71+
Assert.AreEqual("8a2a3f66-898c-4b1e-8db7-efb8c08d4669", guids[1].ToString());
72+
Assert.AreEqual("a11c5663-d025-49be-93d7-876226dfd9b1", guids[2].ToString());
7273

7374
}
7475

7576
[TestMethod]
7677
public void GetGuidList() {
7778

7879
IQueryCollection query = new QueryCollection(new Dictionary<string, StringValues> {
79-
{"guids", new StringValues(new[] { "7ce565ca-3dfe-4bc8-9166-4c4a5d1a9cbb", "a11c5663-d025-49be-93d7-876226dfd9b1", "nope", null })}
80+
{"guids", new StringValues(new[] { "7ce565ca-3dfe-4bc8-9166-4c4a5d1a9cbb,8a2a3f66-898c-4b1e-8db7-efb8c08d4669", "a11c5663-d025-49be-93d7-876226dfd9b1", "nope", null })}
8081
});
8182

8283
var guids = query.GetGuidList("guids");
8384

84-
Assert.AreEqual(2, guids.Count);
85+
Assert.AreEqual(3, guids.Count);
8586

8687
Assert.AreEqual("7ce565ca-3dfe-4bc8-9166-4c4a5d1a9cbb", guids[0].ToString());
87-
Assert.AreEqual("a11c5663-d025-49be-93d7-876226dfd9b1", guids[1].ToString());
88+
Assert.AreEqual("8a2a3f66-898c-4b1e-8db7-efb8c08d4669", guids[1].ToString());
89+
Assert.AreEqual("a11c5663-d025-49be-93d7-876226dfd9b1", guids[2].ToString());
8890

8991
}
9092

0 commit comments

Comments
 (0)