Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix existing query values bug #1737

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading