Skip to content

Commit

Permalink
#370 SetQueryParams bug with duplicate keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Todd committed Sep 15, 2023
1 parent 29df951 commit 69b3b1c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Test/Flurl.Test/UrlBuilder/UrlBuildingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void can_set_query_params_from_dictionary() {
Assert.AreEqual("http://www.mysite.com?1=x&2=y", url.ToString());
}

[Test, Ignore("tricky to do while maintaining param order. deferring until append param w/o overwriting is fully supported.")]
[Test]
public void can_set_query_params_from_kv_pairs() {
var url = "http://foo.com".SetQueryParams(new[] {
new { key = "x", value = 1 },
Expand Down
9 changes: 7 additions & 2 deletions src/Flurl/Url.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,13 @@ public Url SetQueryParams(object values, NullValueHandling nullValueHandling = N
if (values is string s)
return SetQueryParam(s);

foreach (var kv in values.ToKeyValuePairs())
SetQueryParam(kv.Key, kv.Value, nullValueHandling);
var visited = new HashSet<string>();
foreach (var kv in values.ToKeyValuePairs()) {
if (visited.Add(kv.Key))
SetQueryParam(kv.Key, kv.Value, nullValueHandling); // overwrite existing key(s)
else
AppendQueryParam(kv.Key, kv.Value, nullValueHandling); // unless they're in this same collection (#370)
}

return this;
}
Expand Down

0 comments on commit 69b3b1c

Please sign in to comment.