Skip to content

Commit

Permalink
feat: fix existing query values bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Jun 25, 2024
1 parent 9435295 commit c8a3fcf
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 c8a3fcf

Please sign in to comment.