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: lazy initialize queryParamsToAdd #1907

Merged
merged 2 commits into from
Nov 3, 2024
Merged
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
10 changes: 6 additions & 4 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ bool paramsContainsCancellationToken
ret.Content = multiPartContent;
}

var queryParamsToAdd = new List<KeyValuePair<string, string?>>();
List<KeyValuePair<string, string?>>? queryParamsToAdd = null;
var headersToAdd = restMethod.Headers.Count > 0 ?
new Dictionary<string, string?>(restMethod.Headers)
: null;
Expand Down Expand Up @@ -737,6 +737,7 @@ bool paramsContainsCancellationToken
|| queryAttribute != null
)
{
queryParamsToAdd ??= [];
AddQueryParameters(restMethod, queryAttribute, param, queryParamsToAdd, i, parameterInfo);
continue;
}
Expand All @@ -756,9 +757,9 @@ bool paramsContainsCancellationToken
var urlTarget = BuildRelativePath(basePath, restMethod, paramList);

var uri = new UriBuilder(new Uri(BaseUri, urlTarget));
ParseExistingQueryString(uri, queryParamsToAdd);
ParseExistingQueryString(uri, ref queryParamsToAdd);

if (queryParamsToAdd.Count != 0)
if (queryParamsToAdd is not null && queryParamsToAdd.Count != 0)
{
uri.Query = CreateQueryString(queryParamsToAdd);;
}
Expand Down Expand Up @@ -1172,11 +1173,12 @@ var value in ParseEnumerableQueryParameterValue(
}
}

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

queryParamsToAdd ??= [];
var query = HttpUtility.ParseQueryString(uri.Query);
var index = 0;
foreach (var key in query.AllKeys)
Expand Down
Loading