@@ -14,7 +14,8 @@ class RequestBuilderImplementation<TApi>(RefitSettings? refitSettings = null)
14
14
15
15
partial class RequestBuilderImplementation : IRequestBuilder
16
16
{
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" ) ;
18
19
readonly Dictionary < string , List < RestMethodInfoInternal > > interfaceHttpMethods ;
19
20
readonly ConcurrentDictionary <
20
21
CloseGenericMethodKey ,
@@ -803,13 +804,12 @@ await content
803
804
//if header collection, add to request headers
804
805
if ( restMethod . HeaderCollectionParameterMap . Contains ( i ) )
805
806
{
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 )
811
808
{
812
- headersToAdd [ header . Key ] = header . Value ;
809
+ foreach ( var header in headerCollection )
810
+ {
811
+ headersToAdd [ header . Key ] = header . Value ;
812
+ }
813
813
}
814
814
815
815
isParameterMappedToRequest = true ;
@@ -850,7 +850,7 @@ param as IDictionary<string, string>
850
850
|| queryAttribute != null
851
851
)
852
852
{
853
- var attr = queryAttribute ?? new QueryAttribute ( ) ;
853
+ var attr = queryAttribute ?? DefaultQueryAttribute ;
854
854
if ( DoNotConvertToQueryMap ( param ) )
855
855
{
856
856
queryParamsToAdd . AddRange (
@@ -924,7 +924,7 @@ param as IDictionary<string, string>
924
924
// We could have content headers, so we need to make
925
925
// sure we have an HttpContent object to add them to,
926
926
// 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 ) )
928
928
ret . Content = new ByteArrayContent ( Array . Empty < byte > ( ) ) ;
929
929
930
930
foreach ( var header in headersToAdd )
@@ -979,7 +979,7 @@ param as IDictionary<string, string>
979
979
// NB: The URI methods in .NET are dumb. Also, we do this
980
980
// UriBuilder business so that we preserve any hardcoded query
981
981
// 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 ) ) ;
983
983
ParseExistingQueryString ( uri , queryParamsToAdd ) ;
984
984
985
985
if ( queryParamsToAdd . Count != 0 )
@@ -991,11 +991,8 @@ param as IDictionary<string, string>
991
991
uri . Query = null ;
992
992
}
993
993
994
- var uriFormat =
995
- restMethod . MethodInfo . GetCustomAttribute < QueryUriFormatAttribute > ( ) ? . UriFormat
996
- ?? UriFormat . UriEscaped ;
997
994
ret . RequestUri = new Uri (
998
- uri . Uri . GetComponents ( UriComponents . PathAndQuery , uriFormat ) ,
995
+ uri . Uri . GetComponents ( UriComponents . PathAndQuery , restMethod . QueryUriFormat ) ,
999
996
UriKind . Relative
1000
997
) ;
1001
998
return ret ;
@@ -1064,13 +1061,13 @@ var value in ParseEnumerableQueryParameterValue(
1064
1061
1065
1062
default :
1066
1063
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
+ } ;
1074
1071
1075
1072
// Missing a "default" clause was preventing the collection from serializing at all, as it was hitting "continue" thus causing an off-by-one error
1076
1073
var formattedValues = paramValues
@@ -1304,5 +1301,7 @@ static void SetHeader(HttpRequestMessage request, string name, string? value)
1304
1301
request . Content . Headers . TryAddWithoutValidation ( name , value ) ;
1305
1302
}
1306
1303
}
1304
+
1305
+ static bool IsBodyless ( HttpMethod method ) => method == HttpMethod . Get || method == HttpMethod . Head ;
1307
1306
}
1308
1307
}
0 commit comments