@@ -16,7 +16,7 @@ public RequestBuilderImplementation(RefitSettings? refitSettings = null)
16
16
17
17
partial class RequestBuilderImplementation : IRequestBuilder
18
18
{
19
- static readonly ISet < HttpMethod > BodylessMethods = new HashSet < HttpMethod >
19
+ static readonly HashSet < HttpMethod > BodylessMethods = new HashSet < HttpMethod >
20
20
{
21
21
HttpMethod . Get ,
22
22
HttpMethod . Head
@@ -74,16 +74,17 @@ Dictionary<string, List<RestMethodInfoInternal>> methods
74
74
{
75
75
var attrs = methodInfo . GetCustomAttributes ( true ) ;
76
76
var hasHttpMethod = attrs . OfType < HttpMethodAttribute > ( ) . Any ( ) ;
77
- if ( hasHttpMethod )
78
- {
79
- if ( ! methods . ContainsKey ( methodInfo . Name ) )
80
- {
81
- methods . Add ( methodInfo . Name , new List < RestMethodInfoInternal > ( ) ) ;
82
- }
77
+ if ( ! hasHttpMethod )
78
+ continue ;
83
79
84
- var restinfo = new RestMethodInfoInternal ( interfaceType , methodInfo , settings ) ;
85
- methods [ methodInfo . Name ] . Add ( restinfo ) ;
80
+ if ( ! methods . TryGetValue ( methodInfo . Name , out var methodInfoInternals ) )
81
+ {
82
+ methodInfoInternals = new List < RestMethodInfoInternal > ( ) ;
83
+ methods . Add ( methodInfo . Name , methodInfoInternals ) ;
86
84
}
85
+
86
+ var restinfo = new RestMethodInfoInternal ( interfaceType , methodInfo , settings ) ;
87
+ methodInfoInternals . Add ( restinfo ) ;
87
88
}
88
89
}
89
90
@@ -93,64 +94,62 @@ RestMethodInfoInternal FindMatchingRestMethodInfo(
93
94
Type [ ] ? genericArgumentTypes
94
95
)
95
96
{
96
- if ( interfaceHttpMethods . TryGetValue ( key , out var httpMethods ) )
97
+ if ( ! interfaceHttpMethods . TryGetValue ( key , out var httpMethods ) )
98
+ {
99
+ throw new ArgumentException (
100
+ "Method must be defined and have an HTTP Method attribute"
101
+ ) ;
102
+ }
103
+
104
+ if ( parameterTypes == null )
97
105
{
98
- if ( parameterTypes == null )
106
+ if ( httpMethods . Count > 1 )
99
107
{
100
- if ( httpMethods . Count > 1 )
101
- {
102
- throw new ArgumentException (
103
- $ "MethodName exists more than once, '{ nameof ( parameterTypes ) } ' mut be defined"
104
- ) ;
105
- }
106
- return CloseGenericMethodIfNeeded ( httpMethods [ 0 ] , genericArgumentTypes ) ;
108
+ throw new ArgumentException (
109
+ $ "MethodName exists more than once, '{ nameof ( parameterTypes ) } ' mut be defined"
110
+ ) ;
107
111
}
108
112
109
- var isGeneric = genericArgumentTypes ? . Length > 0 ;
113
+ return CloseGenericMethodIfNeeded ( httpMethods [ 0 ] , genericArgumentTypes ) ;
114
+ }
110
115
111
- var possibleMethodsList = httpMethods . Where (
112
- method => method . MethodInfo . GetParameters ( ) . Length == parameterTypes . Length
113
- ) ;
116
+ var isGeneric = genericArgumentTypes ? . Length > 0 ;
114
117
115
- // If it's a generic method, add that filter
116
- if ( isGeneric )
117
- possibleMethodsList = possibleMethodsList . Where (
118
- method =>
119
- method . MethodInfo . IsGenericMethod
120
- && method . MethodInfo . GetGenericArguments ( ) . Length
121
- == genericArgumentTypes ! . Length
122
- ) ;
123
- else // exclude generic methods
124
- possibleMethodsList = possibleMethodsList . Where (
125
- method => ! method . MethodInfo . IsGenericMethod
126
- ) ;
118
+ var possibleMethodsList = httpMethods . Where (
119
+ method => method . MethodInfo . GetParameters ( ) . Length == parameterTypes . Length
120
+ ) ;
127
121
128
- var possibleMethods = possibleMethodsList . ToList ( ) ;
122
+ // If it's a generic method, add that filter
123
+ if ( isGeneric )
124
+ possibleMethodsList = possibleMethodsList . Where (
125
+ method =>
126
+ method . MethodInfo . IsGenericMethod
127
+ && method . MethodInfo . GetGenericArguments ( ) . Length
128
+ == genericArgumentTypes ! . Length
129
+ ) ;
130
+ else // exclude generic methods
131
+ possibleMethodsList = possibleMethodsList . Where (
132
+ method => ! method . MethodInfo . IsGenericMethod
133
+ ) ;
129
134
130
- if ( possibleMethods . Count == 1 )
131
- return CloseGenericMethodIfNeeded ( possibleMethods [ 0 ] , genericArgumentTypes ) ;
135
+ var possibleMethods = possibleMethodsList . ToList ( ) ;
132
136
133
- var parameterTypesArray = parameterTypes . ToArray ( ) ;
134
- foreach ( var method in possibleMethods )
135
- {
136
- var match = method . MethodInfo
137
- . GetParameters ( )
138
- . Select ( p => p . ParameterType )
139
- . SequenceEqual ( parameterTypesArray ) ;
140
- if ( match )
141
- {
142
- return CloseGenericMethodIfNeeded ( method , genericArgumentTypes ) ;
143
- }
144
- }
137
+ if ( possibleMethods . Count == 1 )
138
+ return CloseGenericMethodIfNeeded ( possibleMethods [ 0 ] , genericArgumentTypes ) ;
145
139
146
- throw new Exception ( "No suitable Method found..." ) ;
147
- }
148
- else
140
+ foreach ( var method in possibleMethods )
149
141
{
150
- throw new ArgumentException (
151
- "Method must be defined and have an HTTP Method attribute"
152
- ) ;
142
+ var match = method . MethodInfo
143
+ . GetParameters ( )
144
+ . Select ( p => p . ParameterType )
145
+ . SequenceEqual ( parameterTypes ) ;
146
+ if ( match )
147
+ {
148
+ return CloseGenericMethodIfNeeded ( method , genericArgumentTypes ) ;
149
+ }
153
150
}
151
+
152
+ throw new Exception ( "No suitable Method found..." ) ;
154
153
}
155
154
156
155
RestMethodInfoInternal CloseGenericMethodIfNeeded (
@@ -487,19 +486,12 @@ CancellationToken cancellationToken
487
486
if ( obj == null )
488
487
continue ;
489
488
490
- if ( parameterInfo != null )
489
+ //if we have a parameter info lets check it to make sure it isn't bound to the path
490
+ if ( parameterInfo is { IsObjectPropertyParameter : true } )
491
491
{
492
- //if we have a parameter info lets check it to make sure it isn't bound to the path
493
- if ( parameterInfo . IsObjectPropertyParameter )
492
+ if ( parameterInfo . ParameterProperties . Any ( x => x . PropertyInfo == propertyInfo ) )
494
493
{
495
- if (
496
- parameterInfo . ParameterProperties . Any (
497
- x => x . PropertyInfo == propertyInfo
498
- )
499
- )
500
- {
501
- continue ;
502
- }
494
+ continue ;
503
495
}
504
496
}
505
497
@@ -511,7 +503,7 @@ CancellationToken cancellationToken
511
503
512
504
// Look to see if the property has a Query attribute, and if so, format it accordingly
513
505
var queryAttribute = propertyInfo . GetCustomAttribute < QueryAttribute > ( ) ;
514
- if ( queryAttribute != null && queryAttribute . Format != null )
506
+ if ( queryAttribute is { Format : not null } )
515
507
{
516
508
obj = settings . FormUrlEncodedParameterFormatter . Format (
517
509
obj ,
@@ -657,9 +649,9 @@ bool paramsContainsCancellationToken
657
649
var isParameterMappedToRequest = false ;
658
650
var param = paramList [ i ] ;
659
651
// if part of REST resource URL, substitute it in
660
- if ( restMethod . ParameterMap . ContainsKey ( i ) )
652
+ if ( restMethod . ParameterMap . TryGetValue ( i , out var parameterMapValue ) )
661
653
{
662
- parameterInfo = restMethod . ParameterMap [ i ] ;
654
+ parameterInfo = parameterMapValue ;
663
655
if ( parameterInfo . IsObjectPropertyParameter )
664
656
{
665
657
foreach ( var propertyInfo in parameterInfo . ParameterProperties )
@@ -684,9 +676,9 @@ bool paramsContainsCancellationToken
684
676
{
685
677
string pattern ;
686
678
string replacement ;
687
- if ( restMethod . ParameterMap [ i ] . Type == ParameterType . RoundTripping )
679
+ if ( parameterMapValue . Type == ParameterType . RoundTripping )
688
680
{
689
- pattern = $@ "{{\*\*{ restMethod . ParameterMap [ i ] . Name } }}";
681
+ pattern = $@ "{{\*\*{ parameterMapValue . Name } }}";
690
682
var paramValue = ( string ) param ;
691
683
replacement = string . Join (
692
684
"/" ,
@@ -706,7 +698,7 @@ bool paramsContainsCancellationToken
706
698
}
707
699
else
708
700
{
709
- pattern = "{" + restMethod . ParameterMap [ i ] . Name + "}" ;
701
+ pattern = "{" + parameterMapValue . Name + "}" ;
710
702
replacement = Uri . EscapeDataString (
711
703
settings . UrlParameterFormatter . Format (
712
704
param ,
@@ -802,9 +794,9 @@ await content
802
794
}
803
795
804
796
// if header, add to request headers
805
- if ( restMethod . HeaderParameterMap . ContainsKey ( i ) )
797
+ if ( restMethod . HeaderParameterMap . TryGetValue ( i , out var headerParameterValue ) )
806
798
{
807
- headersToAdd [ restMethod . HeaderParameterMap [ i ] ] = param ? . ToString ( ) ;
799
+ headersToAdd [ headerParameterValue ] = param ? . ToString ( ) ;
808
800
isParameterMappedToRequest = true ;
809
801
}
810
802
@@ -1000,7 +992,7 @@ param as IDictionary<string, string>
1000
992
}
1001
993
}
1002
994
1003
- if ( queryParamsToAdd . Any ( ) )
995
+ if ( queryParamsToAdd . Count != 0 )
1004
996
{
1005
997
var pairs = queryParamsToAdd
1006
998
. Where ( x => x . Key != null && x . Value != null )
0 commit comments