Skip to content

Commit

Permalink
feat: lazy initialize queryParamsToAdd (#1907)
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 Nov 3, 2024
1 parent e13386f commit e45c5f7
Showing 1 changed file with 6 additions and 4 deletions.
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

0 comments on commit e45c5f7

Please sign in to comment.