@@ -14,7 +14,8 @@ class RequestBuilderImplementation<TApi>(RefitSettings? refitSettings = null)
1414
1515 partial class RequestBuilderImplementation : IRequestBuilder
1616 {
17- static readonly HashSet < HttpMethod > BodylessMethods = [ HttpMethod . Get , HttpMethod . Head ] ;
17+ static readonly QueryAttribute DefaultQueryAttribute = new ( ) ;
18+ static readonly Uri BaseUri = new Uri ( "http://api" ) ;
1819 readonly Dictionary < string , List < RestMethodInfoInternal > > interfaceHttpMethods ;
1920 readonly ConcurrentDictionary <
2021 CloseGenericMethodKey ,
@@ -803,13 +804,12 @@ await content
803804 //if header collection, add to request headers
804805 if ( restMethod . HeaderCollectionParameterMap . Contains ( i ) )
805806 {
806- var headerCollection =
807- param as IDictionary < string , string >
808- ?? new Dictionary < string , string > ( ) ;
809-
810- foreach ( var header in headerCollection )
807+ if ( param is IDictionary < string , string > headerCollection )
811808 {
812- headersToAdd [ header . Key ] = header . Value ;
809+ foreach ( var header in headerCollection )
810+ {
811+ headersToAdd [ header . Key ] = header . Value ;
812+ }
813813 }
814814
815815 isParameterMappedToRequest = true ;
@@ -850,7 +850,7 @@ param as IDictionary<string, string>
850850 || queryAttribute != null
851851 )
852852 {
853- var attr = queryAttribute ?? new QueryAttribute ( ) ;
853+ var attr = queryAttribute ?? DefaultQueryAttribute ;
854854 if ( DoNotConvertToQueryMap ( param ) )
855855 {
856856 queryParamsToAdd . AddRange (
@@ -924,7 +924,7 @@ param as IDictionary<string, string>
924924 // We could have content headers, so we need to make
925925 // sure we have an HttpContent object to add them to,
926926 // provided the HttpClient will allow it for the method
927- if ( ret . Content == null && ! BodylessMethods . Contains ( ret . Method ) )
927+ if ( ret . Content == null && ! IsBodyless ( ret . Method ) )
928928 ret . Content = new ByteArrayContent ( Array . Empty < byte > ( ) ) ;
929929
930930 foreach ( var header in headersToAdd )
@@ -979,7 +979,7 @@ param as IDictionary<string, string>
979979 // NB: The URI methods in .NET are dumb. Also, we do this
980980 // UriBuilder business so that we preserve any hardcoded query
981981 // parameters as well as add the parameterized ones.
982- var uri = new UriBuilder ( new Uri ( new Uri ( "http://api" ) , urlTarget ) ) ;
982+ var uri = new UriBuilder ( new Uri ( BaseUri , urlTarget ) ) ;
983983 ParseExistingQueryString ( uri , queryParamsToAdd ) ;
984984
985985 if ( queryParamsToAdd . Count != 0 )
@@ -991,11 +991,8 @@ param as IDictionary<string, string>
991991 uri . Query = null ;
992992 }
993993
994- var uriFormat =
995- restMethod . MethodInfo . GetCustomAttribute < QueryUriFormatAttribute > ( ) ? . UriFormat
996- ?? UriFormat . UriEscaped ;
997994 ret . RequestUri = new Uri (
998- uri . Uri . GetComponents ( UriComponents . PathAndQuery , uriFormat ) ,
995+ uri . Uri . GetComponents ( UriComponents . PathAndQuery , restMethod . QueryUriFormat ) ,
999996 UriKind . Relative
1000997 ) ;
1001998 return ret ;
@@ -1064,13 +1061,13 @@ var value in ParseEnumerableQueryParameterValue(
10641061
10651062 default :
10661063 var delimiter =
1067- collectionFormat == CollectionFormat . Ssv
1068- ? " "
1069- : collectionFormat == CollectionFormat . Tsv
1070- ? "\t "
1071- : collectionFormat == CollectionFormat . Pipes
1072- ? "| "
1073- : "," ;
1064+ collectionFormat switch
1065+ {
1066+ CollectionFormat . Ssv => " " ,
1067+ CollectionFormat . Tsv => "\t " ,
1068+ CollectionFormat . Pipes => "|" ,
1069+ _ => ", "
1070+ } ;
10741071
10751072 // Missing a "default" clause was preventing the collection from serializing at all, as it was hitting "continue" thus causing an off-by-one error
10761073 var formattedValues = paramValues
@@ -1304,5 +1301,7 @@ static void SetHeader(HttpRequestMessage request, string name, string? value)
13041301 request . Content . Headers . TryAddWithoutValidation ( name , value ) ;
13051302 }
13061303 }
1304+
1305+ static bool IsBodyless ( HttpMethod method ) => method == HttpMethod . Get || method == HttpMethod . Head ;
13071306 }
13081307}
0 commit comments