Skip to content

Commit

Permalink
feat: fix existing query values bug (#1737)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Pulman <[email protected]>
  • Loading branch information
TimothyMakkison and ChrisPulman authored Jun 26, 2024
1 parent 9605c24 commit 151b1d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Refit.Tests/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2531,7 +2531,7 @@ public void ParametersShouldBePutAsExplicitQueryString()

var uri = new Uri(new Uri("http://api"), output.RequestUri);

Assert.Equal("/query?q2=value2&q1=value1", uri.PathAndQuery);
Assert.Equal("/query?q1=value1&q2=value2", uri.PathAndQuery);
}

[Fact]
Expand Down
31 changes: 20 additions & 11 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -980,17 +980,7 @@ param as IDictionary<string, string>
// UriBuilder business so that we preserve any hardcoded query
// parameters as well as add the parameterized ones.
var uri = new UriBuilder(new Uri(new Uri("http://api"), urlTarget));
var query = HttpUtility.ParseQueryString(uri.Query ?? "");
foreach (var key in query.AllKeys)
{
if (!string.IsNullOrWhiteSpace(key))
{
queryParamsToAdd.Insert(
0,
new KeyValuePair<string, string?>(key, query[key])
);
}
}
ParseExistingQueryString(uri, queryParamsToAdd);

if (queryParamsToAdd.Count != 0)
{
Expand Down Expand Up @@ -1100,6 +1090,25 @@ var value in ParseEnumerableQueryParameterValue(
}
}

static void ParseExistingQueryString(UriBuilder uri, List<KeyValuePair<string, string?>> queryParamsToAdd)
{
if (string.IsNullOrEmpty(uri.Query))
return;

var query = HttpUtility.ParseQueryString(uri.Query);
var index = 0;
foreach (var key in query.AllKeys)
{
if (!string.IsNullOrWhiteSpace(key))
{
queryParamsToAdd.Insert(
index++,
new KeyValuePair<string, string?>(key, query[key])
);
}
}
}

static string CreateQueryString(List<KeyValuePair<string, string?>> queryParamsToAdd)
{
// Suppress warning as ValueStringBuilder.ToString calls Dispose()
Expand Down

0 comments on commit 151b1d9

Please sign in to comment.