diff --git a/Refit/RequestBuilderImplementation.cs b/Refit/RequestBuilderImplementation.cs index c24422b61..63756273f 100644 --- a/Refit/RequestBuilderImplementation.cs +++ b/Refit/RequestBuilderImplementation.cs @@ -646,7 +646,7 @@ bool paramsContainsCancellationToken ret.Content = multiPartContent; } - var queryParamsToAdd = new List>(); + List>? queryParamsToAdd = null; var headersToAdd = restMethod.Headers.Count > 0 ? new Dictionary(restMethod.Headers) : null; @@ -737,6 +737,7 @@ bool paramsContainsCancellationToken || queryAttribute != null ) { + queryParamsToAdd ??= []; AddQueryParameters(restMethod, queryAttribute, param, queryParamsToAdd, i, parameterInfo); continue; } @@ -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);; } @@ -1172,11 +1173,12 @@ var value in ParseEnumerableQueryParameterValue( } } - static void ParseExistingQueryString(UriBuilder uri, List> queryParamsToAdd) + static void ParseExistingQueryString(UriBuilder uri, ref List>? queryParamsToAdd) { if (string.IsNullOrEmpty(uri.Query)) return; + queryParamsToAdd ??= []; var query = HttpUtility.ParseQueryString(uri.Query); var index = 0; foreach (var key in query.AllKeys)